|
| 1 | +import { FetchMock } from 'jest-fetch-mock'; |
| 2 | +import preloadData from '../preloadData'; |
| 3 | +import { createDataClient } from '../Client'; |
| 4 | + |
| 5 | +const fetchMock = fetch as FetchMock; |
| 6 | + |
| 7 | +describe('preloadData tests', () => { |
| 8 | + beforeEach(() => { |
| 9 | + fetchMock.resetMocks(); |
| 10 | + jest.useFakeTimers(); |
| 11 | + }); |
| 12 | + afterEach(() => { |
| 13 | + jest.useRealTimers(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('Should throw a promise when data is not ready yet', async () => { |
| 17 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 18 | + const client = createDataClient({ ssr: false }); |
| 19 | + const url = 'http://localhost:3000/some-rest-api-raw'; |
| 20 | + |
| 21 | + const resource = preloadData(client, url, {}, {}, { raw: true }); |
| 22 | + |
| 23 | + expect(resource.read).toThrow(); |
| 24 | + |
| 25 | + try { |
| 26 | + resource.read(); |
| 27 | + } catch (promise) { |
| 28 | + await promise; |
| 29 | + |
| 30 | + expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('Should throw an error the fetch is rejected', async () => { |
| 35 | + fetchMock.mockReject(() => Promise.reject('Fabricated error')); |
| 36 | + |
| 37 | + const client = createDataClient({ ssr: false }); |
| 38 | + const url = 'http://localhost:3000/some-rest-api-raw/2'; |
| 39 | + |
| 40 | + const resource = preloadData(client, url, {}, undefined, { raw: true }); |
| 41 | + |
| 42 | + expect(resource.read).toThrow(); |
| 43 | + |
| 44 | + try { |
| 45 | + resource.read(); |
| 46 | + } catch (promise) { |
| 47 | + await promise; |
| 48 | + |
| 49 | + expect(resource.read).toThrow(); |
| 50 | + expect(true).toBe(true); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('Should use data from cache if available', async () => { |
| 55 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 56 | + const client = createDataClient({ ssr: false }); |
| 57 | + const url = 'http://localhost:3000/some-rest-api-raw/3'; |
| 58 | + |
| 59 | + const resource = preloadData(client, url, {}, {}, { raw: true }); |
| 60 | + |
| 61 | + expect(resource.read).toThrow(); |
| 62 | + |
| 63 | + try { |
| 64 | + resource.read(); |
| 65 | + } catch (promise) { |
| 66 | + await promise; |
| 67 | + |
| 68 | + expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); |
| 69 | + |
| 70 | + const resource2 = preloadData(client, url, {}, {}, { raw: true }); |
| 71 | + |
| 72 | + expect(resource2.read()).toStrictEqual(resource.read()); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('Should not store data to cache when fetchPolicy is network-only', async () => { |
| 77 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 78 | + const fabricatedError = new Error('Fabricated error'); |
| 79 | + const client = createDataClient({ ssr: false }); |
| 80 | + const url = 'http://localhost:3000/some-rest-api-raw/4'; |
| 81 | + |
| 82 | + const resource = preloadData(client, url, {}, {}, { |
| 83 | + fetchPolicy: 'network-only', |
| 84 | + raw: true, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(resource.read).toThrow(); |
| 88 | + |
| 89 | + fetchMock.mockReject(fabricatedError); |
| 90 | + |
| 91 | + try { |
| 92 | + resource.read(); |
| 93 | + } catch (promise) { |
| 94 | + await promise; |
| 95 | + |
| 96 | + expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); |
| 97 | + |
| 98 | + const resource2 = preloadData(client, url); |
| 99 | + |
| 100 | + expect(resource2.read).toThrowError(); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments