RTK Query: Unit testing shared features #2125
-
Hey 👋 , First, thank you for RTK + RTK Query! I love working with it! Question: Example: const Page = () => {
const {error, isLoading} = useDataQuery();
if (isLoading) { return <Loading/> }
if (error) { return <Alert>{error.message}</Alert>}
return <DataTable/>
}
const DataTable = () => {
const {data} = useDataQuery();
return /* Table with data */
} Problem: const DataTable = () => {
const {data} = useDataQuery();
if (data == null) return null;
return /* Table with data */
}
describe('Feature: DataTable', () => {
it('handles displaying data in a table', async () => {
render(<DataTable/>);
await screen.findByText('something in data table');
expect(screen.getByText('something else in data table')).toBeInTheDocument();
})
}) However, I dislike this boilerplate almost as much as handling loading/error states. I'd love to hear your thoughts on the matter and if I could be thinking about or organizing this differently. FWIW I'm using MSW and React Testing Library. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can snapshot a store that has all the data you want using the Redux devtools and then use a hydration action together with |
Beta Was this translation helpful? Give feedback.
-
Thanks! Is it also possible to use |
Beta Was this translation helpful? Give feedback.
You can snapshot a store that has all the data you want using the Redux devtools and then use a hydration action together with
extractRehydrationInfo
to get that data into the store in your test.