|
1 | 1 | describe('fetch', () => { |
2 | | - let f, fetch |
| 2 | + let f, fetch, originalOpen, originalFetch |
3 | 3 |
|
4 | 4 | beforeEach(() => { |
5 | | - f = jest.fn() |
6 | | - jest.doMock('isomorphic-unfetch', () => f) |
7 | | - fetch = require('../src/fetch').default |
| 5 | + jest.isolateModules(() => { |
| 6 | + f = jest.fn() |
| 7 | + jest.doMock('isomorphic-unfetch', () => f) |
| 8 | + originalOpen = jest.spyOn(XMLHttpRequest.prototype, 'open') |
| 9 | + originalFetch = jest.spyOn(window, 'fetch').mockImplementation() |
| 10 | + fetch = require('../src/fetch').default |
| 11 | + window.__NEXT_DATA__ = { |
| 12 | + buildId: 'development', |
| 13 | + } |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + delete window.__NEXT_DATA__ |
8 | 19 | }) |
9 | 20 |
|
10 | 21 | it('should export isomorphic-unfetch for backwards compatibility', () => { |
11 | 22 | fetch('/foo') |
12 | 23 | expect(f).toHaveBeenCalledWith('/foo') |
13 | 24 | }) |
| 25 | + |
| 26 | + it('should patch window.fetch', () => { |
| 27 | + window.fetch('/foo') |
| 28 | + expect(originalFetch).toHaveBeenCalledWith('http://localhost/foo?__v__=development', undefined) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should accept a request object as the first parameter', () => { |
| 32 | + window.fetch(new Request('/foo')) |
| 33 | + expect(originalFetch.mock.calls[0][0].url).toBe('http://localhost/foo?__v__=development') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should add the version to XMLHttpRequest.open', () => { |
| 37 | + const req = new XMLHttpRequest() |
| 38 | + req.open('GET', '/foo') |
| 39 | + expect(originalOpen).toHaveBeenCalledWith('GET', 'http://localhost/foo?__v__=development') |
| 40 | + }) |
14 | 41 | }) |
0 commit comments