Skip to content

Commit

Permalink
test: Tests for API and selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Oct 20, 2023
1 parent 2070743 commit 60b4359
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
56 changes: 51 additions & 5 deletions src/taxonomy/api/hooks/api.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
import { useQuery } from '@tanstack/react-query';
import { useTaxonomyListData } from './api';
import { useQuery, useMutation } from '@tanstack/react-query';
import { useTaxonomyListData, useExportTaxonomy } from './api';
import { downloadDataAsFile } from '../../../utils';

const mockHttpClient = {
get: jest.fn(),
};

jest.mock('@tanstack/react-query', () => ({
useQuery: jest.fn(),
useMutation: jest.fn(),
}));

jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedHttpClient: jest.fn(),
getAuthenticatedHttpClient: jest.fn(() => mockHttpClient),
}));

jest.mock('../../../utils', () => ({
downloadDataAsFile: jest.fn(),
}));

describe('taxonomy API: useTaxonomyListData', () => {
describe('taxonomy API', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should call useQuery with the correct parameters', () => {
it('useTaxonomyListData should call useQuery with the correct parameters', () => {
useTaxonomyListData();

expect(useQuery).toHaveBeenCalledWith({
queryKey: ['taxonomyList'],
queryFn: expect.any(Function),
});
});

it('useExportTaxonomy should export data correctly', async () => {
useMutation.mockImplementation((exportFunc) => exportFunc);

const mockResponseJson = {
headers: {
'content-type': 'application/json',
},
data: { tags: 'tags' },
};
const mockResponseCsv = {
headers: {
'content-type': 'text',
},
data: 'This is a CSV',
};

const exportTaxonomy = useExportTaxonomy();

mockHttpClient.get.mockResolvedValue(mockResponseJson);
await exportTaxonomy({ pk: 1, format: 'json', name: 'testFile' });

expect(downloadDataAsFile).toHaveBeenCalledWith(
JSON.stringify(mockResponseJson.data, null, 2),
'application/json',
'testFile.json',
);

mockHttpClient.get.mockResolvedValue(mockResponseCsv);
await exportTaxonomy({ pk: 1, format: 'csv', name: 'testFile' });
expect(downloadDataAsFile).toHaveBeenCalledWith(
mockResponseCsv.data,
'text',
'testFile.csv',
);
});
});
17 changes: 15 additions & 2 deletions src/taxonomy/api/hooks/selectors.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './selectors';
import { useTaxonomyListData } from './api';
import {
useTaxonomyListDataResponse,
useIsTaxonomyListDataLoaded,
useExportTaxonomyMutation,
} from './selectors';
import { useTaxonomyListData, useExportTaxonomy } from './api';

jest.mock('./api', () => ({
__esModule: true,
useTaxonomyListData: jest.fn(),
useExportTaxonomy: jest.fn(),
}));

describe('useTaxonomyListDataResponse', () => {
Expand Down Expand Up @@ -41,3 +46,11 @@ describe('useIsTaxonomyListDataLoaded', () => {
expect(result).toBe(false);
});
});

describe('useExportTaxonomyMutation', () => {
it('should trigger useExportTaxonomy', () => {
useExportTaxonomyMutation();

expect(useExportTaxonomy).toHaveBeenCalled();
});
});

0 comments on commit 60b4359

Please sign in to comment.