I am writing test cases for redux toolkit query but it is not giving me expected results.
This is my api.ts
export const notificationApi = createApi({
reducerPath: 'notificationApi',
baseQuery: fetchBaseQuery({ baseUrl: config.BASE_API_URL }),
endpoints: (builder) => ({
getNotifications: builder.query({
query: ({ name, page, limit }) => ({
url: `/api/getNotificationList/${name}/${page + 1}/${limit}`,
}),
}),
}),
});
export const { useGetNotificationsQuery } = notificationApi;
And This is the test case that I have written
import { waitFor } from '@testing-library/react';
import { renderHookWithProviders } from 'common/tests/renderWithProviders';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { useGetNotificationsQuery } from './api';
const server = setupServer(
http.get('/api/admin/getNotifications/New_Ingested_Data_Available/2/10', () => {
return HttpResponse.json({
data: 'mockedData',
});
}),
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test('useGetNotificationsQuery performs the API call and returns the data', async () => {
const { result } = renderHookWithProviders(() =>
useGetNotificationsQuery({ name: 'New_Ingested_Data_Available', page: 1, limit: 10 }),
);
expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getNotifications',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
console.log('result-->', result);
});
result.current.isSuccess is not coming out to be true. It is always false. Hence I am not able to proceed further. Can anyone help here ?