(mockResponses)
| 423 | } |
| 424 | |
| 425 | function mockFetch(mockResponses) { |
| 426 | const spy = jasmine.createSpy('fetch'); |
| 427 | fetchWasMocked = true; // Track that fetch was mocked for cleanup |
| 428 | |
| 429 | global.fetch = (url, options = {}) => { |
| 430 | // Allow requests to the Parse Server to pass through WITHOUT recording in spy |
| 431 | // This prevents tests from failing when they check that fetch wasn't called |
| 432 | // but the Parse SDK makes internal requests to the Parse Server |
| 433 | if (typeof url === 'string' && url.includes(serverURL)) { |
| 434 | return originalFetch(url, options); |
| 435 | } |
| 436 | |
| 437 | // Record non-Parse-Server calls in the spy |
| 438 | spy(url, options); |
| 439 | |
| 440 | options.method ||= 'GET'; |
| 441 | const mockResponse = mockResponses?.find( |
| 442 | (mock) => mock.url === url && mock.method === options.method |
| 443 | ); |
| 444 | |
| 445 | if (mockResponse) { |
| 446 | return Promise.resolve(mockResponse.response); |
| 447 | } |
| 448 | |
| 449 | return Promise.resolve({ |
| 450 | ok: false, |
| 451 | statusText: 'Unknown URL or method', |
| 452 | }); |
| 453 | }; |
| 454 | |
| 455 | // Expose spy methods for test assertions |
| 456 | global.fetch.calls = spy.calls; |
| 457 | global.fetch.and = spy.and; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | // This is polluting, but, it makes it way easier to directly port old tests. |
no test coverage detected