-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for preloading raw data
Showing
3 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/react-isomorphic-data/src/common/__tests__/preloadData.raw.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { FetchMock } from 'jest-fetch-mock'; | ||
import preloadData from '../preloadData'; | ||
import { createDataClient } from '../Client'; | ||
|
||
const fetchMock = fetch as FetchMock; | ||
|
||
describe('preloadData tests', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
jest.useFakeTimers(); | ||
}); | ||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('Should throw a promise when data is not ready yet', async () => { | ||
fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); | ||
const client = createDataClient({ ssr: false }); | ||
const url = 'http://localhost:3000/some-rest-api-raw'; | ||
|
||
const resource = preloadData(client, url, {}, {}, { raw: true }); | ||
|
||
expect(resource.read).toThrow(); | ||
|
||
try { | ||
resource.read(); | ||
} catch (promise) { | ||
await promise; | ||
|
||
expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); | ||
} | ||
}); | ||
|
||
it('Should throw an error the fetch is rejected', async () => { | ||
fetchMock.mockReject(() => Promise.reject('Fabricated error')); | ||
|
||
const client = createDataClient({ ssr: false }); | ||
const url = 'http://localhost:3000/some-rest-api-raw/2'; | ||
|
||
const resource = preloadData(client, url, {}, undefined, { raw: true }); | ||
|
||
expect(resource.read).toThrow(); | ||
|
||
try { | ||
resource.read(); | ||
} catch (promise) { | ||
await promise; | ||
|
||
expect(resource.read).toThrow(); | ||
expect(true).toBe(true); | ||
} | ||
}); | ||
|
||
it('Should use data from cache if available', async () => { | ||
fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); | ||
const client = createDataClient({ ssr: false }); | ||
const url = 'http://localhost:3000/some-rest-api-raw/3'; | ||
|
||
const resource = preloadData(client, url, {}, {}, { raw: true }); | ||
|
||
expect(resource.read).toThrow(); | ||
|
||
try { | ||
resource.read(); | ||
} catch (promise) { | ||
await promise; | ||
|
||
expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); | ||
|
||
const resource2 = preloadData(client, url, {}, {}, { raw: true }); | ||
|
||
expect(resource2.read()).toStrictEqual(resource.read()); | ||
} | ||
}); | ||
|
||
it('Should not store data to cache when fetchPolicy is network-only', async () => { | ||
fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); | ||
const fabricatedError = new Error('Fabricated error'); | ||
const client = createDataClient({ ssr: false }); | ||
const url = 'http://localhost:3000/some-rest-api-raw/4'; | ||
|
||
const resource = preloadData(client, url, {}, {}, { | ||
fetchPolicy: 'network-only', | ||
raw: true, | ||
}); | ||
|
||
expect(resource.read).toThrow(); | ||
|
||
fetchMock.mockReject(fabricatedError); | ||
|
||
try { | ||
resource.read(); | ||
} catch (promise) { | ||
await promise; | ||
|
||
expect(resource.read()).toStrictEqual('{\"message\":\"Hello world!\"}'); | ||
|
||
const resource2 = preloadData(client, url); | ||
|
||
expect(resource2.read).toThrowError(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters