I have the following test file:
import { renderHook, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactPressProvider } from './ReactPressProvider';
import usePost from './usePost';
import { server } from './test/server'; // Update the path if necessary
const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
<ReactPressProvider
apiConfig={{ apiUrl: 'https://your-wordpress-site.com' }}
>
{children}
</ReactPressProvider>
</QueryClientProvider>
);
describe('usePost', () => {
beforeAll(() => {
server.listen();
});
afterEach(() => {
queryClient.clear();
server.resetHandlers();
});
afterAll(() => {
server.close();
});
it('should fetch a post by ID', async () => {
const postId = 1;
const { result } = renderHook(() => usePost(postId), {
wrapper,
});
await waitFor(() => {
console.log(result.current);
return expect(result.current.isSuccess).toBeTruthy();
});
expect(result.current.data).toHaveProperty('id', postId);
});
it('should handle fetch errors', async () => {
const postId = 'nonexistent-post';
const { result } = renderHook(() => usePost(postId), {
wrapper,
});
await waitFor(() => {
console.log(result.current);
expect(result.current.isLoading).toBeFalsy(); // <--- FAILS!
expect(result.current.isError).toBeTruthy();
});
expect(result.current.error).toBeInstanceOf(Error);
});
});
and while the first test works, the second one does not.
This is my handler:
import { rest } from 'msw';
const apiUrl = 'https://your-wordpress-site.com/wp-json/wp/v2';
const handlers = [
rest.get(`${apiUrl}/posts/:id`, (req, res, ctx) => {
const { id } = req.params;
if (id === '1')
return res(
ctx.status(200),
ctx.json({ id: 1, title: 'Test Post' })
);
return res(ctx.status(404), ctx.json({ message: 'Post not found' }));
}),
];
export { handlers };
If the return code is a 404, it gets stuck indefinitely in "isLoading".
Why could this be happening?