Testing optimistic update in react query.
I am trying to write test case for optimistic update mutation. But it's not workin for some reason. Can anyone please have a look here. I am providing the stack overflow link. https://stackoverflow.com/questions/74209718/testing-optimistic-update-in-react-query
Stack Overflow
Testing Optimistic update in react query
I am trying to write the test case for an optimistic update in react query. But it's not working. Here is the code that I wrote to test it. Hope someone could help me. Thanks in advance. When I just
9 Replies
rival-blackOP•4y ago
It's getting to onError due to this line
const previousColorData = queryClient.getQueryData(['colors', page])
Maybe i need to mock this getQueryData but don't know how to do that
adverse-sapphire•4y ago
answered
rival-blackOP•4y ago
Thanks but I have one more query. Can we somehow, mimic the cached data for a specific key and get the oldQueryData?
I was able to get the previousData by mocking it but the other methods are not working well.
Suppose I just want to mock getQueryData for a particular test case and leave the other functions like invalidateQueries, cancelQuery, and setQueryData, as it is, then how, can modify this mock function?
This is what I wrote. But getting this
adverse-sapphire•4y ago
if you just put data in your cache with
queryClient.setQueryData for the right key in your test (just after the creation of the queryClient), it will be returned from getQueryData. I wouldn't mock anything except the network itself 🙂rival-blackOP•4y ago
I tried that as well. But got some error.
TypeError: Cannot read property 'useContext' of null
7 |
8 | describe('Testing custom hooks of react query', () => {
9 | const queryClient = useQueryClient()Sorry for the constant message but I really am stuck with the test case. If you please tell me what wrong am I doing here. I tried all the possible appraoch.
adverse-sapphire•4y ago
You can't useQueryClient outside of react components....
Do the seeding right in the wrapper where you create the client and have access to it
rival-blackOP•4y ago
I have followed your approach for testing. Don't know how can I modify the wrapper to fit my condition. Below is the code for wrapper. Can you make the changes here as per my requirement.
adverse-sapphire•4y ago
I mean:
???
rival-blackOP•4y ago
Thanks a lot. It finally worked