Skip to content

Commit

Permalink
test: selectors and api
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Oct 25, 2023
1 parent 836212d commit db61ce0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/taxonomy/api/hooks/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
29 changes: 28 additions & 1 deletion src/taxonomy/api/hooks/api.test.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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),
});
});
});
84 changes: 83 additions & 1 deletion src/taxonomy/api/hooks/selectors.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit db61ce0

Please sign in to comment.