-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to using Redux integration tests and remove mock-redux-store (#…
…127) Also adds a containerized-test target to re-run JS tests w/o rebuilding (mounts in changes)
- Loading branch information
Showing
19 changed files
with
499 additions
and
549 deletions.
There are no files selected for viewing
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
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
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 was deleted.
Oops, something went wrong.
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,98 @@ | ||
import { cleanup, renderHook } from '@testing-library/react-hooks'; | ||
import { waitFor } from '@testing-library/react'; | ||
import fetchMock from 'fetch-mock-jest'; | ||
import { Provider } from 'react-redux' | ||
import React, { PropsWithChildren } from 'react'; | ||
|
||
import { useDataLoader } from './hooks'; | ||
import { setupStore } from './redux/store'; | ||
|
||
// Pretend not a test so useDataLoader doesn't bail on requests | ||
jest.mock('expo-constants', () => ({ | ||
expoConfig: { | ||
extra: { | ||
ENVIRONMENT: 'not_test', | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useDataLoader', function () { | ||
afterEach(function () { | ||
fetchMock.restore(); | ||
}); | ||
|
||
it('should setup loading labels and todos', async function () { | ||
const labelEntries = [{ | ||
id: 1, | ||
name: "home" | ||
}, { | ||
id: 2, | ||
name: "work" | ||
}]; | ||
fetchMock.getOnce('http://chalk-dev.flipperkid.com/api/todos/labels/', { | ||
body: labelEntries, | ||
}); | ||
|
||
const firstTodo = { | ||
id: 256, | ||
description: "First Todo", | ||
}; | ||
const secondTodo = Object.assign({}, firstTodo, { | ||
id: 257, | ||
description: "Second Todo" | ||
}); | ||
const todosRoute = 'http://chalk-dev.flipperkid.com/api/todos/todos/'; | ||
fetchMock.getOnce(todosRoute, { | ||
body: [firstTodo], | ||
}); | ||
|
||
jest.useFakeTimers(); | ||
|
||
const store = setupStore(); | ||
const wrapper = ({ children }: PropsWithChildren<object>): JSX.Element => <Provider store={store}>{children}</Provider>; | ||
renderHook(useDataLoader, { wrapper }); | ||
|
||
// Verify loading labels started | ||
expect(store.getState().labelsApi.loading).toEqual(true); | ||
|
||
// Verify loading labels completed | ||
await waitFor(() => { | ||
expect(store.getState().labelsApi.loading).toEqual(false); | ||
expect(store.getState().labelsApi.entries).toEqual(labelEntries); | ||
}); | ||
|
||
// Move forward long enough to load Todos | ||
jest.advanceTimersByTime(10000); | ||
|
||
// Verify loading todos completed | ||
expect(store.getState().todosApi.loading).toEqual(true); | ||
await waitFor(() => { | ||
expect(store.getState().todosApi.loading).toEqual(false); | ||
expect(store.getState().todosApi.entries).toEqual([firstTodo]); | ||
}); | ||
|
||
// Move forward long enough to load Todos again | ||
fetchMock.get(todosRoute, { | ||
body: [firstTodo, secondTodo], | ||
}, { overwriteRoutes: true }); | ||
jest.advanceTimersByTime(10000); | ||
|
||
// Verify todos updated with a 2nd after loading again | ||
expect(store.getState().todosApi.loading).toEqual(true); | ||
await waitFor(() => { | ||
expect(store.getState().todosApi.loading).toEqual(false); | ||
expect(store.getState().todosApi.entries).toEqual([firstTodo, secondTodo]); | ||
}); | ||
|
||
// Ensure no more Todos are loaded after cleanup | ||
cleanup(); | ||
jest.advanceTimersByTime(30000); | ||
await fetchMock.flush(); | ||
expect(fetchMock.calls().length).toEqual(3); | ||
|
||
// Verify we make the server requests | ||
expect(fetchMock).toBeDone(); | ||
}, 20000); | ||
}); | ||
|
||
export {}; |
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
Oops, something went wrong.