Skip to content

Commit

Permalink
UIQM-588 Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanDenis committed Nov 29, 2023
1 parent 83bded4 commit 7a0ff16
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/QuickMarcEditor/QuickMarcEditorContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const QuickMarcEditorContainer = ({
&& action !== QUICK_MARC_ACTIONS.CREATE;

const showCallout = useShowCallout();
const { linksCount } = useAuthorityLinksCount({ id: externalId });
const { linksCount } = useAuthorityLinksCount({ id: marcType === MARC_TYPES.AUTHORITY && externalId });

const closeEditor = useCallback((id) => {
if (marcType === MARC_TYPES.HOLDINGS && action !== QUICK_MARC_ACTIONS.CREATE) {
Expand Down
6 changes: 1 addition & 5 deletions src/QuickMarcEditor/QuickMarcEditorContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ describe('Given Quick Marc Editor Container', () => {

describe('when the marc type is authority', () => {
it('should make a request to get the number of links', async () => {
useAuthorityLinksCount.mockClear().mockReturnValue({
fetchLinksCount: mockFetchLinksCount,
});

await act(async () => {
await renderQuickMarcEditorContainer({
mutator,
Expand All @@ -174,7 +170,7 @@ describe('Given Quick Marc Editor Container', () => {
});
});

expect(mockFetchLinksCount).toHaveBeenCalledWith([match.params.externalId]);
expect(useAuthorityLinksCount).toHaveBeenCalledWith({ id: match.params.externalId });
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const useAuthorityLinksCount = ({ id, tenantId } = {}) => {
() => ky.get('search/authorities', { searchParams }).json(),
{
keepPreviousData: true,
enabled: Boolean(id),
},
);

Expand Down
29 changes: 16 additions & 13 deletions src/queries/useAuthorityLinksCount/useAuthorityLinksCount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import {
} from 'react-query';
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';

import '../../../test/jest/__mock__';

import { useOkapiKy } from '@folio/stripes/core';

import useAuthorityLinksCount from './useAuthorityLinksCount';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
}));
import '../../../test/jest/__mock__';

const queryClient = new QueryClient();

Expand All @@ -23,22 +18,30 @@ const wrapper = ({ children }) => (
</QueryClientProvider>
);

const mockPost = jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ links: [] }),
const mockGet = jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ authorities: [{ numberOfTitles: 2 }] }),
});

describe('Given useAuthorityLinksCount', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
post: mockPost,
get: mockGet,
});
});

it('should fetch links count', async () => {
const { result } = renderHook(() => useAuthorityLinksCount(), { wrapper });
it('should fetch authorities', async () => {
renderHook(() => useAuthorityLinksCount({ id: 'fakeId' }), { wrapper });

expect(mockGet).toHaveBeenCalledWith('search/authorities', {
searchParams: {
query: '(id==fakeId and authRefType==("Authorized"))',
},
});
});

await result.current.fetchLinksCount(['fakeId']);
it('should return links count', async () => {
const { result } = renderHook(() => useAuthorityLinksCount({ id: 'fakeId' }), { wrapper });

expect(mockPost).toHaveBeenCalledWith('links/authorities/bulk/count', { json: { ids: ['fakeId'] } });
expect(result.current.linksCount).toEqual(2);
});
});

0 comments on commit 7a0ff16

Please sign in to comment.