TanStackT
TanStack2y ago
2 replies
technical-ivory

Mocking react query data for react-testing-library

Hello all! Huge fan of ReactQuery and starting to introduce testing into our app. My use case is that I have a banner, which has a custom ReactQuery hook that fetches data to show in the banner (an object of string values).

I'd like to mock a test response and verify that the data is rendered in the component. I'm using react-testing-library and jest to run the test.

I've properly wrapped my render method with a QueryProvider

 <QueryClientProvider client={queryClient}>
     <Banner />
   </QueryClientProvider>,


In my test case, I have a static mock data object, which I'd like to test against. However, I'm having trouble figuring out how to provide that as test data for the react-query query that runs inside the component. Any help would be appreciated. Thank you! Happy to provide any additional code samples if helpful
Was this page helpful?