-
reproduceBackgroundI refer to this link - Using Hydration and use import React from 'react'
import { dehydrate, QueryClient } from 'react-query'
import { Layout, Header, InfoBox, Name } from '../components'
import { fetchName } from '../hooks'
const Home = () => {
return (
<Layout>
<Header />
<InfoBox>ℹ️ This page shows how to use SSG with React-Query.</InfoBox>
<Name />
</Layout>
)
}
export async function getStaticProps() {
const queryClient = new QueryClient()
await queryClient.prefetchQuery('name', () => fetchName())
return {
props: {
dehydratedState: dehydrate(queryClient),
},
}
}
export default Home I tested the above Home component in a js-dom environment using the reaction testing library. the code is as follows. /**
* @jest-environment jsdom
*/
import { render, screen, queryClient } from '../test-utils';
import { server } from '../../mocks/server';
import Home from '../../pages';
beforeAll(() => {
server.listen();
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
jest.mock('next/router', () => require('next-router-mock'))
it('SSR prefetch test' ,() => {
// `queryClient.setQueryData()` was used instead of hydration.
queryClient.setQueryData('name', {data: {name: 'John Doe'}})
render(<Home/>)
expect(screen.getByText('John Doe')).toBeInTheDocument();
}) Since the initial data was set through However, the test failed with a message that John Doe could not be found in dom. Perhaps the initial data setting through 'queryClient.setQueryData()' does not work as I intended. Question
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
you're not showing how you use the Also, are you sure that the It's always hard to help with just code snippets and screenshots. Best would be a minimum codesandbox reproduction. |
Beta Was this translation helpful? Give feedback.
you're not showing how you use the
queryClient
in the testing code. Are you sure that the queryClient where you set the data to is the one that is used to render the component?Also, are you sure that the
data
nesting is necessary? Again, can't tell because I'm not seeing the actual useQuery call.It's always hard to help with just code snippets and screenshots. Best would be a minimum codesandbox reproduction.