From db61ce0ba0059b39c21a6badaf38a5ab2f0c19ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Wed, 25 Oct 2023 15:04:54 -0300 Subject: [PATCH] test: selectors and api --- src/taxonomy/api/hooks/api.js | 2 +- src/taxonomy/api/hooks/api.test.js | 29 +++++++- src/taxonomy/api/hooks/selectors.test.js | 84 +++++++++++++++++++++++- 3 files changed, 112 insertions(+), 3 deletions(-) diff --git a/src/taxonomy/api/hooks/api.js b/src/taxonomy/api/hooks/api.js index ecfb845a9a..0b6d56ad69 100644 --- a/src/taxonomy/api/hooks/api.js +++ b/src/taxonomy/api/hooks/api.js @@ -39,7 +39,7 @@ export const exportTaxonomy = (pk, format) => { */ export const useTaxonomyDetailData = (taxonomyId) => ( useQuery({ - queryKey: ['taxonomyList', taxonomyId], + queryKey: ['taxonomyDetail', taxonomyId], queryFn: () => getAuthenticatedHttpClient().get(getTaxonomyDetailApiUrl(taxonomyId)) .then(camelCaseObject) .then((response) => response.data), diff --git a/src/taxonomy/api/hooks/api.test.js b/src/taxonomy/api/hooks/api.test.js index b3dc0045d1..76d2f9a496 100644 --- a/src/taxonomy/api/hooks/api.test.js +++ b/src/taxonomy/api/hooks/api.test.js @@ -1,5 +1,10 @@ import { useQuery } from '@tanstack/react-query'; -import { useTaxonomyListData, exportTaxonomy } from './api'; +import { + exportTaxonomy, + useTagListData, + useTaxonomyDetailData, + useTaxonomyListData, +} from './api'; const mockHttpClient = { get: jest.fn(), @@ -54,3 +59,25 @@ describe('exportTaxonomy', () => { ); }); }); + +describe('useTaxonomyDetailData', () => { + it('should call useQuery with the correct parameters', () => { + useTaxonomyDetailData('1'); + + expect(useQuery).toHaveBeenCalledWith({ + queryKey: ['taxonomyDetail', '1'], + queryFn: expect.any(Function), + }); + }); +}); + +describe('useTagListData', () => { + it('should call useQuery with the correct parameters', () => { + useTagListData('1', { pageIndex: 3 }); + + expect(useQuery).toHaveBeenCalledWith({ + queryKey: ['tagList', '1', 3], + queryFn: expect.any(Function), + }); + }); +}); diff --git a/src/taxonomy/api/hooks/selectors.test.js b/src/taxonomy/api/hooks/selectors.test.js index 772b39c4d8..3ae04a742b 100644 --- a/src/taxonomy/api/hooks/selectors.test.js +++ b/src/taxonomy/api/hooks/selectors.test.js @@ -1,14 +1,25 @@ import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded, + useTaxonomyDetailDataStatus, + useTaxonomyDetailDataResponse, + useTagListDataStatus, + useTagListDataResponse, callExportTaxonomy, } from './selectors'; -import { useTaxonomyListData, exportTaxonomy } from './api'; +import { + useTaxonomyListData, + exportTaxonomy, + useTaxonomyDetailData, + useTagListData, +} from './api'; jest.mock('./api', () => ({ __esModule: true, useTaxonomyListData: jest.fn(), exportTaxonomy: jest.fn(), + useTaxonomyDetailData: jest.fn(), + useTagListData: jest.fn(), })); describe('useTaxonomyListDataResponse', () => { @@ -54,3 +65,74 @@ describe('callExportTaxonomy', () => { expect(exportTaxonomy).toHaveBeenCalled(); }); }); + +describe('useTaxonomyDetailDataStatus', () => { + it('should return status values', () => { + const status = { + isError: false, + error: undefined, + isFetched: true, + isSuccess: true, + }; + + useTaxonomyDetailData.mockReturnValueOnce(status); + + const result = useTaxonomyDetailDataStatus(0); + + expect(result).toEqual(status); + }); +}); + +describe('useTaxonomyDetailDataResponse', () => { + it('should return data when status is success', () => { + useTaxonomyDetailData.mockReturnValueOnce({ isSuccess: true, data: 'data' }); + + const result = useTaxonomyDetailDataResponse(); + + expect(result).toEqual('data'); + }); + + it('should return undefined when status is not success', () => { + useTaxonomyDetailData.mockReturnValueOnce({ isSuccess: false }); + + const result = useTaxonomyDetailDataResponse(); + + expect(result).toBeUndefined(); + }); +}); + +describe('useTagListDataStatus', () => { + it('should return status values', () => { + const status = { + error: undefined, + isError: false, + isFetched: true, + isLoading: true, + isSuccess: true, + }; + + useTagListData.mockReturnValueOnce(status); + + const result = useTagListDataStatus(0, {}); + + expect(result).toEqual(status); + }); +}); + +describe('useTagListDataResponse', () => { + it('should return data when status is success', () => { + useTagListData.mockReturnValueOnce({ isSuccess: true, data: 'data' }); + + const result = useTagListDataResponse(0, {}); + + expect(result).toEqual('data'); + }); + + it('should return undefined when status is not success', () => { + useTagListData.mockReturnValueOnce({ isSuccess: false }); + + const result = useTagListDataResponse(0, {}); + + expect(result).toBeUndefined(); + }); +});