|
| 1 | +import fetchFromAPI from 'react-storefront/props/fetchFromAPI' |
| 2 | + |
1 | 3 | describe('fetchFromAPI', () => { |
2 | | - const headers = { |
3 | | - headers: { 'x-rsf-api-version': '1' }, |
4 | | - } |
| 4 | + let headers |
5 | 5 |
|
6 | 6 | fetchMock.mockResponse(JSON.stringify({})) |
7 | 7 |
|
8 | | - let fetchFromAPI, |
9 | | - isBrowser = true |
10 | | - |
11 | | - beforeEach(() => { |
12 | | - jest.isolateModules(() => { |
13 | | - jest.doMock('react-storefront/utils/isBrowser', () => () => isBrowser) |
14 | | - fetchFromAPI = require('react-storefront/props/fetchFromAPI').default |
15 | | - }) |
16 | | - }) |
17 | | - |
18 | | - afterAll(() => { |
19 | | - jest.resetAllMocks() |
20 | | - }) |
21 | | - |
22 | 8 | describe('in the browser', () => { |
23 | 9 | beforeEach(() => { |
24 | | - isBrowser = true |
| 10 | + headers = { |
| 11 | + 'x-rsf-api-version': '1', |
| 12 | + } |
25 | 13 | }) |
26 | 14 |
|
27 | 15 | it('should prepend api to the path', () => { |
28 | 16 | fetchFromAPI({ |
29 | 17 | asPath: '/p/1', |
30 | 18 | }) |
31 | | - expect(fetchMock).toHaveBeenCalledWith('/api/p/1', headers) |
| 19 | + expect(fetchMock).toHaveBeenCalledWith('/api/p/1', { headers }) |
32 | 20 | }) |
33 | 21 |
|
34 | 22 | it('should call /api when the path is /', () => { |
35 | 23 | fetchFromAPI({ |
36 | 24 | asPath: '/', |
37 | 25 | }) |
38 | | - expect(fetchMock).toHaveBeenCalledWith('/api', headers) |
| 26 | + expect(fetchMock).toHaveBeenCalledWith('/api', { headers }) |
39 | 27 | }) |
40 | 28 |
|
41 | 29 | it('should append query params directly to api if the root with query params is called', () => { |
42 | 30 | fetchFromAPI({ |
43 | 31 | asPath: '/?test=1', |
44 | 32 | }) |
45 | | - expect(fetchMock).toHaveBeenCalledWith('/api?test=1', headers) |
| 33 | + expect(fetchMock).toHaveBeenCalledWith('/api?test=1', { headers }) |
46 | 34 | }) |
47 | 35 | }) |
48 | 36 |
|
49 | 37 | describe('on the server', () => { |
50 | 38 | beforeEach(() => { |
51 | | - isBrowser = false |
| 39 | + headers = { |
| 40 | + 'x-rsf-api-version': '1', |
| 41 | + host: 'www.domain.com', |
| 42 | + } |
52 | 43 | }) |
53 | 44 |
|
54 | 45 | it('should include the protocol, domain, and ?_includeAppData=1', () => { |
55 | 46 | fetchFromAPI({ |
56 | 47 | asPath: '/p/1', |
| 48 | + pathname: '/p/[productId]', |
57 | 49 | req: { |
58 | 50 | headers: { |
59 | 51 | host: 'www.domain.com', |
60 | 52 | }, |
61 | 53 | }, |
62 | 54 | }) |
63 | | - expect(fetchMock).toHaveBeenCalledWith( |
64 | | - 'https://www.domain.com/api/p/1?_includeAppData=1', |
65 | | - headers, |
66 | | - ) |
| 55 | + expect(fetchMock).toHaveBeenCalledWith('https://www.domain.com/api/p/1?_includeAppData=1', { |
| 56 | + headers: { |
| 57 | + ...headers, |
| 58 | + 'x-next-page': '/api/p/[productId]', |
| 59 | + }, |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should use API_HOST when provided', () => { |
| 64 | + process.env.API_HOST = 'localhost:3001' |
| 65 | + |
| 66 | + fetchFromAPI({ |
| 67 | + asPath: '/p/1', |
| 68 | + pathname: '/p/[productId]', |
| 69 | + req: { |
| 70 | + headers: { |
| 71 | + host: 'www.domain.com', |
| 72 | + }, |
| 73 | + }, |
| 74 | + }) |
| 75 | + |
| 76 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost:3001/api/p/1?_includeAppData=1', { |
| 77 | + headers: { |
| 78 | + ...headers, |
| 79 | + 'x-next-page': '/api/p/[productId]', |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + delete process.env.API_HOST |
67 | 84 | }) |
68 | 85 |
|
69 | 86 | it('should use http:// for localhost', () => { |
70 | 87 | fetchFromAPI({ |
71 | 88 | asPath: '/p/1', |
| 89 | + pathname: '/p/[productId]', |
72 | 90 | req: { |
73 | 91 | headers: { |
| 92 | + ...headers, |
74 | 93 | host: 'localhost', |
75 | 94 | }, |
76 | 95 | }, |
77 | 96 | }) |
78 | | - expect(fetchMock).toHaveBeenCalledWith('http://localhost/api/p/1?_includeAppData=1', headers) |
| 97 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost/api/p/1?_includeAppData=1', { |
| 98 | + headers: { |
| 99 | + ...headers, |
| 100 | + host: 'localhost', |
| 101 | + 'x-next-page': '/api/p/[productId]', |
| 102 | + }, |
| 103 | + }) |
79 | 104 | }) |
80 | 105 |
|
81 | 106 | it('should append _includeAppData to the existing query string', () => { |
82 | 107 | fetchFromAPI({ |
83 | 108 | asPath: '/foo?x=1', |
| 109 | + pathname: '/foo', |
84 | 110 | req: { |
85 | | - headers: { |
86 | | - host: 'localhost', |
87 | | - }, |
| 111 | + headers, |
88 | 112 | }, |
89 | 113 | }) |
90 | 114 | expect(fetchMock).toHaveBeenCalledWith( |
91 | | - 'http://localhost/api/foo?x=1&_includeAppData=1', |
92 | | - headers, |
| 115 | + 'https://www.domain.com/api/foo?x=1&_includeAppData=1', |
| 116 | + { |
| 117 | + headers: { |
| 118 | + ...headers, |
| 119 | + 'x-next-page': '/api/foo', |
| 120 | + }, |
| 121 | + }, |
93 | 122 | ) |
94 | 123 | }) |
95 | 124 | }) |
|
0 commit comments