Testing Component that uses UseQuery hook
Hello !
I don't know if I'm following the right logic on this, do not hesitate to tell me i'm wrong.
I have a component that uses the data from a custom useQuery to conditionally render the content of my page.
I want to test the different outputs of my component which depends on the result of my useQuery.
Right now, I have my QueryProvider setup, my axios call is mocked, but when I try to assert on what my component render, I get an error as data is considered undefined.
I'm considering mocking the useQuery as I can already verify the hook itself in another test. Would that be correct, and if so, how can I do that in Typescript ?
The error:
I'll put the code samples in the thread. (i'll check if I can quickly reproduce a codesandbox)
Thanks
2 Replies
unwilling-turquoiseOP•3y ago
Test:
Content of the component is kinda fancy due to business logic but I'll try to resume it here:
Content of the custom useQuery:
I tried to reproduce here https://codesandbox.io/s/admiring-hermann-r32h8g, but getting an issue in test with the queryclientprovider 😮
deep-jade•3y ago
I'm stuck with the same problem. And also struggle with it. This way is very questionable because how bloated it is, but works.
Probably should be possible to mock queryFn, but I'm not figured out yet why variant with mocking queryFn not working for me.