I have a function component that used react query useInfiniteQuery to get list data, now I try to test the useInfiniteQuery with error test case get API but still did not cover the onError object function. Does anyone know how to reach onError test with RTL and jest?
useFetchListUsers.ts
export const useFetchListUsers = (params) => {
const { users,
error,
isLoading,
isFetching,
fetchNextPage,
hasNextPage } = useInfiniteQuery(['users', ...params], getAllUsers, {
getNextPageParam: (lastPage) => {
return ...lastPage;
},
onError: (error: AxiosError) => {
return "Error get data Users from API"
},
});
return { users, error, isLoading, isFetching, fetchNextPage, hasNextPage }
}
usersAPI.ts
export const getAllUsers = async () => {
const {data} = await axios.get('/v1/users');
return data;
}
displayUsers.test.tsx
describe('test display list users', () => {
it("should handle failed get list users", () => {
(useFetchListUsers as any).mockRejectedValueOnce({
users: undefined,
});
render(
<QueryClientProvider client={queryClient}>
<ListUSers />
</QueryClientProvider>,
{ store }
);
expect(await screen.findByTestId("noUsersAlert")).toBeTruthy();
})
})