-
Hi, I'm fairly new user of the library so I may be misunderstanding some concepts. I have a mutation which deletes and item, and when it successfully completes, it calls const myQueryFn = jest.fn();
const observer = new QueryObserver(queryClient, { queryKey: MY_LIST_QUERY_KEY, enabled: false })
const unsubscribeQuery = observer.subscribe(myQueryFn);
... call my hook which does the mutation and invalidates the cache...
expect(myQueryFn).toHaveBeenCalledTimes(1);
unsubscribeQuery(); From what I understand, when you subscribe it calls the function straight away so I set it to be disabled. I expected that a subsequent refetch would trigger it once. This is the behaviour I experienced when I was using version 2 of the library with I was messing around and came up with the following solution. I don't know why it works, so this could be wrong. I suspect const myQueryFn = jest.fn();
new QueryObserver(queryClient, {
queryKey: MY_LIST_QUERY_KEY
});
const unsubscribeQuery = queryClient.getQueryCache().subscribe(query => {
if (query?.queryKey === MY_LIST_QUERY_KEY) {
myQueryFn();
}
});
... call my hook which does the mutation and invalidates the cache...
expect(myQueryFn).toHaveBeenCalledTimes(1);
unsubscribeQuery(); Am I missing something obvious here? Any help would be much appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I would just mock the actual fetchFn and then see if it contains the data i want, or if the mock has been called the amount of times i want it to be called. E.g. with something like https://jestjs.io/docs/en/mock-function-api#mockfnmockresolvedvalueoncevalue |
Beta Was this translation helpful? Give feedback.
I would just mock the actual fetchFn and then see if it contains the data i want, or if the mock has been called the amount of times i want it to be called. E.g. with something like https://jestjs.io/docs/en/mock-function-api#mockfnmockresolvedvalueoncevalue