|
1 | 1 | /* eslint-disable import/first */
|
2 | 2 | /* eslint-disable import/newline-after-import */
|
3 |
| -/* globals describe,test,expect,jest,afterEach,beforeAll */ |
| 3 | +/* globals describe,test,expect,jest,afterEach,beforeAll,beforeEach */ |
4 | 4 | import '@babel/runtime/regenerator';
|
5 | 5 | jest.mock('cross-fetch');
|
6 | 6 | import fetch from 'cross-fetch';
|
@@ -114,4 +114,178 @@ describe('HttpTransport', () => {
|
114 | 114 | body: largeQueryJson
|
115 | 115 | });
|
116 | 116 | });
|
| 117 | + |
| 118 | + // Signal tests from src/tests/HttpTransport.test.js |
| 119 | + describe('Signal functionality', () => { |
| 120 | + beforeEach(() => { |
| 121 | + fetch.mockClear(); |
| 122 | + // Default mock implementation for signal tests |
| 123 | + fetch.mockImplementation(() => Promise.resolve({ |
| 124 | + json: () => Promise.resolve({ data: 'test data' }), |
| 125 | + ok: true, |
| 126 | + status: 200 |
| 127 | + })); |
| 128 | + }); |
| 129 | + |
| 130 | + test('should pass the signal to fetch when provided in constructor', async () => { |
| 131 | + const controller = new AbortController(); |
| 132 | + const { signal } = controller; |
| 133 | + |
| 134 | + const transport = new HttpTransport({ |
| 135 | + authorization: 'token', |
| 136 | + apiUrl: 'http://localhost:4000/cubejs-api/v1', |
| 137 | + signal |
| 138 | + }); |
| 139 | + |
| 140 | + const request = transport.request('load', { query: { measures: ['Orders.count'] } }); |
| 141 | + |
| 142 | + // Start the request |
| 143 | + const promise = request.subscribe((result) => result); |
| 144 | + |
| 145 | + // Wait for fetch to be called |
| 146 | + await Promise.resolve(); |
| 147 | + |
| 148 | + // Ensure fetch was called with the signal |
| 149 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 150 | + expect(fetch.mock.calls[0][1].signal).toBe(signal); |
| 151 | + |
| 152 | + await promise; |
| 153 | + }); |
| 154 | + |
| 155 | + test('should pass the signal to fetch when provided in request method', async () => { |
| 156 | + const controller = new AbortController(); |
| 157 | + const { signal } = controller; |
| 158 | + |
| 159 | + const transport = new HttpTransport({ |
| 160 | + authorization: 'token', |
| 161 | + apiUrl: 'http://localhost:4000/cubejs-api/v1' |
| 162 | + }); |
| 163 | + |
| 164 | + const request = transport.request('load', { |
| 165 | + query: { measures: ['Orders.count'] }, |
| 166 | + signal |
| 167 | + }); |
| 168 | + |
| 169 | + // Start the request |
| 170 | + const promise = request.subscribe((result) => result); |
| 171 | + |
| 172 | + // Wait for fetch to be called |
| 173 | + await Promise.resolve(); |
| 174 | + |
| 175 | + // Ensure fetch was called with the signal |
| 176 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 177 | + expect(fetch.mock.calls[0][1].signal).toBe(signal); |
| 178 | + |
| 179 | + await promise; |
| 180 | + }); |
| 181 | + |
| 182 | + test('should prioritize request signal over constructor signal', async () => { |
| 183 | + const controller1 = new AbortController(); |
| 184 | + const controller2 = new AbortController(); |
| 185 | + |
| 186 | + const transport = new HttpTransport({ |
| 187 | + authorization: 'token', |
| 188 | + apiUrl: 'http://localhost:4000/cubejs-api/v1', |
| 189 | + signal: controller1.signal |
| 190 | + }); |
| 191 | + |
| 192 | + const request = transport.request('load', { |
| 193 | + query: { measures: ['Orders.count'] }, |
| 194 | + signal: controller2.signal |
| 195 | + }); |
| 196 | + |
| 197 | + // Start the request |
| 198 | + const promise = request.subscribe((result) => result); |
| 199 | + |
| 200 | + // Wait for fetch to be called |
| 201 | + await Promise.resolve(); |
| 202 | + |
| 203 | + // Ensure fetch was called with the request signal, not the constructor signal |
| 204 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 205 | + expect(fetch.mock.calls[0][1].signal).toBe(controller2.signal); |
| 206 | + expect(fetch.mock.calls[0][1].signal).not.toBe(controller1.signal); |
| 207 | + |
| 208 | + await promise; |
| 209 | + }); |
| 210 | + |
| 211 | + test('should create AbortSignal.timeout from fetchTimeout if signal not provided', async () => { |
| 212 | + // Mock AbortSignal.timeout |
| 213 | + const originalTimeout = AbortSignal.timeout; |
| 214 | + const mockTimeoutSignal = {}; |
| 215 | + AbortSignal.timeout = jest.fn().mockReturnValue(mockTimeoutSignal); |
| 216 | + |
| 217 | + const transport = new HttpTransport({ |
| 218 | + authorization: 'token', |
| 219 | + apiUrl: 'http://localhost:4000/cubejs-api/v1', |
| 220 | + fetchTimeout: 5000 |
| 221 | + }); |
| 222 | + |
| 223 | + const request = transport.request('load', { |
| 224 | + query: { measures: ['Orders.count'] } |
| 225 | + }); |
| 226 | + |
| 227 | + // Start the request |
| 228 | + const promise = request.subscribe((result) => result); |
| 229 | + |
| 230 | + // Wait for fetch to be called |
| 231 | + await Promise.resolve(); |
| 232 | + |
| 233 | + // Ensure fetch was called with the timeout signal |
| 234 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 235 | + expect(fetch.mock.calls[0][1].signal).toBe(mockTimeoutSignal); |
| 236 | + expect(AbortSignal.timeout).toHaveBeenCalledWith(5000); |
| 237 | + |
| 238 | + // Restore original implementation |
| 239 | + AbortSignal.timeout = originalTimeout; |
| 240 | + |
| 241 | + await promise; |
| 242 | + }); |
| 243 | + |
| 244 | + test('should handle request abortion', async () => { |
| 245 | + // Create a mock Promise and resolver function to control Promise completion |
| 246 | + let resolveFetch; |
| 247 | + const fetchPromise = new Promise(resolve => { |
| 248 | + resolveFetch = resolve; |
| 249 | + }); |
| 250 | + |
| 251 | + // Mock fetch to return our controlled Promise |
| 252 | + fetch.mockImplementationOnce(() => fetchPromise); |
| 253 | + |
| 254 | + const controller = new AbortController(); |
| 255 | + const { signal } = controller; |
| 256 | + |
| 257 | + const transport = new HttpTransport({ |
| 258 | + authorization: 'token', |
| 259 | + apiUrl: 'http://localhost:4000/cubejs-api/v1' |
| 260 | + }); |
| 261 | + |
| 262 | + const request = transport.request('load', { |
| 263 | + query: { measures: ['Orders.count'] }, |
| 264 | + signal |
| 265 | + }); |
| 266 | + |
| 267 | + // Start the request but don't wait for it to complete |
| 268 | + const requestPromise = request.subscribe((result) => result); |
| 269 | + |
| 270 | + // Wait for fetch to be called |
| 271 | + await Promise.resolve(); |
| 272 | + |
| 273 | + // Ensure fetch was called with the signal |
| 274 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 275 | + expect(fetch.mock.calls[0][1].signal).toBe(signal); |
| 276 | + |
| 277 | + // Abort the request |
| 278 | + controller.abort(); |
| 279 | + |
| 280 | + // Resolve the fetch Promise, simulating request completion |
| 281 | + resolveFetch({ |
| 282 | + json: () => Promise.resolve({ data: 'aborted data' }), |
| 283 | + ok: true, |
| 284 | + status: 200 |
| 285 | + }); |
| 286 | + |
| 287 | + // Wait for the request Promise to complete |
| 288 | + await requestPromise; |
| 289 | + }, 10000); // Set 10-second timeout |
| 290 | + }); |
117 | 291 | });
|
0 commit comments