Skip to content

Commit

Permalink
test: check invalidateQueries
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Dec 8, 2023
1 parent 94f1bf7 commit 8d2c437
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/taxonomy/import-tags/data/api.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { waitFor } from '@testing-library/react';

Check failure on line 3 in src/taxonomy/import-tags/data/api.test.jsx

View workflow job for this annotation

GitHub Actions / tests

'waitFor' is defined but never used
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

Expand All @@ -26,16 +27,6 @@ const queryClient = new QueryClient({
},
});

const mockInvalidateQueries = jest.fn();

jest.mock('@tanstack/react-query', () => ({
...jest.requireActual('@tanstack/react-query'),
useQueryClient: () => ({
...jest.requireActual('@tanstack/react-query').useQueryClient(),
invalidateQueries: mockInvalidateQueries,
}),
}));

const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
Expand Down Expand Up @@ -69,16 +60,12 @@ describe('import taxonomy api calls', () => {

it('should call import tags', async () => {
axiosMock.onPut(getTagsImportApiUrl(1)).reply(200);
const mockInvalidateQueries = jest.spyOn(queryClient, 'invalidateQueries');

const { result } = renderHook(() => useImportTags(), { wrapper });

await result.current.mutateAsync({ taxonomyId: 1 });
expect(axiosMock.history.put[0].url).toEqual(getTagsImportApiUrl(1));
});

it('should call plan import tags', async () => {
axiosMock.onPut(getTagsPlanImportApiUrl(1)).reply(200, { plan: 'plan' });
await planImportTags(1);
expect(axiosMock.history.put[0].url).toEqual(getTagsPlanImportApiUrl(1));
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: ['tagList', 1],
});
Expand All @@ -87,8 +74,16 @@ describe('import taxonomy api calls', () => {
});
});

it('should call plan import tags', async () => {
axiosMock.onPut(getTagsPlanImportApiUrl(1)).reply(200, { plan: 'plan' });
await planImportTags(1);
expect(axiosMock.history.put[0].url).toEqual(getTagsPlanImportApiUrl(1));
});

it('should handle errors in plan import tags', async () => {
axiosMock.onPut(getTagsPlanImportApiUrl(1)).reply(400, { error: 'test error' });
const mockInvalidateQueries = jest.spyOn(queryClient, 'invalidateQueries');

expect(planImportTags(1)).rejects.toEqual(Error('test error'));
expect(axiosMock.history.put[0].url).toEqual(getTagsPlanImportApiUrl(1));
expect(mockInvalidateQueries).not.toHaveBeenCalled();
Expand Down

0 comments on commit 8d2c437

Please sign in to comment.