Skip to content

Commit

Permalink
refactor: move data/selectors to data/apiHooks
Browse files Browse the repository at this point in the history
and fix tests to mock useQuery.
  • Loading branch information
pomegranited committed Nov 15, 2023
1 parent 9df1699 commit 0fdaa07
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/taxonomy/tag-list/TagListTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Proptypes from 'prop-types';
import { useState } from 'react';

import messages from './messages';
import { useTagListDataResponse, useTagListDataStatus } from './data/selectors';
import { useTagListDataResponse, useTagListDataStatus } from './data/apiHooks';

const TagListTable = ({ taxonomyId }) => {
const intl = useIntl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const useTagListDataStatus = (taxonomyId, options) => {
};
};

// ToDo: fix types
/**
* @param {number} taxonomyId
* @param {import("./types.mjs").QueryOptions} options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import {
useTagListDataStatus,
useTagListDataResponse,
} from './selectors';
import {
useTagListData,
} from './api';
} from './apiHooks';

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

describe('useTagListDataStatus', () => {
Expand All @@ -20,7 +18,7 @@ describe('useTagListDataStatus', () => {
isSuccess: true,
};

useTagListData.mockReturnValueOnce(status);
useQuery.mockReturnValueOnce(status);

const result = useTagListDataStatus(0, {});

Expand All @@ -30,15 +28,15 @@ describe('useTagListDataStatus', () => {

describe('useTagListDataResponse', () => {
it('should return data when status is success', () => {
useTagListData.mockReturnValueOnce({ isSuccess: true, data: 'data' });
useQuery.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 });
useQuery.mockReturnValueOnce({ isSuccess: false });

const result = useTagListDataResponse(0, {});

Expand Down
2 changes: 1 addition & 1 deletion src/taxonomy/taxonomy-detail/TaxonomyDetailPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import TaxonomyDetailMenu from './TaxonomyDetailMenu';
import TaxonomyDetailSideCard from './TaxonomyDetailSideCard';
import { TagListTable } from '../tag-list';
import ExportModal from '../export-modal';
import { useTaxonomyDetailDataResponse, useTaxonomyDetailDataStatus } from './data/selectors';
import { useTaxonomyDetailDataResponse, useTaxonomyDetailDataStatus } from './data/apiHooks';

const TaxonomyDetailPage = () => {
const intl = useIntl();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {
useTaxonomyDetailData,
} from './api';
import { useQuery } from '@tanstack/react-query';
import {
useTaxonomyDetailDataStatus,
useTaxonomyDetailDataResponse,
} from './selectors';
} from './apiHooks';

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

describe('useTaxonomyDetailDataStatus', () => {
Expand All @@ -20,7 +17,7 @@ describe('useTaxonomyDetailDataStatus', () => {
isSuccess: true,
};

useTaxonomyDetailData.mockReturnValueOnce(status);
useQuery.mockReturnValueOnce(status);

const result = useTaxonomyDetailDataStatus(0);

Expand All @@ -30,15 +27,15 @@ describe('useTaxonomyDetailDataStatus', () => {

describe('useTaxonomyDetailDataResponse', () => {
it('should return data when status is success', () => {
useTaxonomyDetailData.mockReturnValueOnce({ isSuccess: true, data: 'data' });
useQuery.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 });
useQuery.mockReturnValueOnce({ isSuccess: false });

const result = useTaxonomyDetailDataResponse();

Expand Down

0 comments on commit 0fdaa07

Please sign in to comment.