Skip to content

Commit

Permalink
reafactor: typings and return of taxonomy import api
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Dec 9, 2023
1 parent 71a5c0b commit 32af5db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/taxonomy/import-tags/data/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { getConfig } from '@edx/frontend-platform';
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { useQueryClient, useMutation } from '@tanstack/react-query';

Expand Down Expand Up @@ -33,18 +33,21 @@ export const getTagsPlanImportApiUrl = (taxonomyId) => new URL(
* @param {string} taxonomyName
* @param {string} taxonomyDescription
* @param {File} file
* @returns {Promise<void>}
* @returns {Promise<import(../../taxonomy-detail/data/types).TaxonomyData}

Check failure on line 36 in src/taxonomy/import-tags/data/api.js

View workflow job for this annotation

GitHub Actions / tests

Type expected.

Check failure on line 36 in src/taxonomy/import-tags/data/api.js

View workflow job for this annotation

GitHub Actions / tests

String literal expected.

Check failure on line 36 in src/taxonomy/import-tags/data/api.js

View workflow job for this annotation

GitHub Actions / tests

Identifier expected.

Check failure on line 36 in src/taxonomy/import-tags/data/api.js

View workflow job for this annotation

GitHub Actions / tests

Identifier expected.
*/
export async function importNewTaxonomy(taxonomyName, taxonomyDescription, file) {
// ToDo: transform this to use react-query like useImportTags
const formData = new FormData();
formData.append('taxonomy_name', taxonomyName);
formData.append('taxonomy_description', taxonomyDescription);
formData.append('file', file);

await getAuthenticatedHttpClient().post(
const { data } = await getAuthenticatedHttpClient().post(
getTaxonomyImportNewApiUrl(),
formData,
);

return camelCaseObject(data);
}

/**
Expand All @@ -67,18 +70,18 @@ export const useImportTags = () => {
const formData = new FormData();
formData.append('file', file);

await getAuthenticatedHttpClient().put(
const { data } = await getAuthenticatedHttpClient().put(
getTagsImportApiUrl(taxonomyId),
formData,
);

return camelCaseObject(data);
},
onSuccess: (_data, variables) => {
onSuccess: (data, variables) => {
queryClient.invalidateQueries({
queryKey: ['tagList', variables.taxonomyId],
});
queryClient.invalidateQueries({
queryKey: ['taxonomyDetail', variables.taxonomyId],
});
queryClient.setQueryData(['taxonomyDetail', variables.taxonomyId], data);
},
});
};
Expand Down
10 changes: 4 additions & 6 deletions src/taxonomy/import-tags/data/api.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ describe('import taxonomy api calls', () => {
});

it('should call import tags', async () => {
axiosMock.onPut(getTagsImportApiUrl(1)).reply(200);
const taxonomy = { id: 1, name: 'taxonomy name' };
axiosMock.onPut(getTagsImportApiUrl(1)).reply(200, taxonomy);
const mockInvalidateQueries = jest.spyOn(queryClient, 'invalidateQueries');
const mockSetQueryData = jest.spyOn(queryClient, 'setQueryData');

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

Expand All @@ -68,9 +70,7 @@ describe('import taxonomy api calls', () => {
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: ['tagList', 1],
});
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: ['taxonomyDetail', 1],
});
expect(mockSetQueryData).toHaveBeenCalledWith(['taxonomyDetail', 1], taxonomy);
});

it('should call plan import tags', async () => {
Expand All @@ -81,10 +81,8 @@ describe('import taxonomy api calls', () => {

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();
});
});

0 comments on commit 32af5db

Please sign in to comment.