Skip to content

Commit

Permalink
MODLD-547: fix for broken tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio authored Oct 18, 2024
1 parent cf50940 commit a313fee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ jobs:
secrets: inherit
with:
compile-translations: false
jest-enabled: false
jest-test-command: npm run test:unit
sonar-enabled: false
38 changes: 31 additions & 7 deletions src/test/__tests__/common/hooks/useComplexLookup.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { renderHook, act } from '@testing-library/react';
import { ChangeEvent } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil';
import { useComplexLookup } from '@common/hooks/useComplexLookup';
import { useMarcData } from '@common/hooks/useMarcData';
import { AdvancedFieldType } from '@common/constants/uiControls.constants';
import { __MOCK_URI_CHANGE_WHEN_IMPLEMENTING } from '@common/constants/complexLookup.constants';
import {
Expand All @@ -17,11 +18,14 @@ import {

jest.mock('recoil');
jest.mock('@common/helpers/complexLookup.helper');
jest.mock('@common/hooks/useMarcData');

describe('useComplexLookup', () => {
const mockSchema = new Map();
const mockSelectedEntries = [] as string[];
const mockSetSelectedEntries = jest.fn();
const mockClearhMarcData = jest.fn();
const mockResetRecoilState = jest.fn();

const mockEntry = {
uuid: 'testUuid',
Expand All @@ -40,7 +44,21 @@ describe('useComplexLookup', () => {
},
},
];
const mockLookupConfig = {} as ComplexLookupsConfigEntry;
const mockLookupConfig = {
api: {
endpoints: {
marcPreview: '/testEndpoint',
},
},
} as ComplexLookupsConfigEntry;
const mockMarcPreviewMetadata = {
baseId: 'newId',
marcId: 'newMarcId',
srsId: 'newSrsId',
title: 'newTitle',
headingType: 'headingType',
};

const mockOnChange = jest.fn();
let result: any;

Expand All @@ -57,8 +75,13 @@ describe('useComplexLookup', () => {
);

beforeEach(() => {
(useRecoilValue as jest.Mock).mockReturnValue(mockSchema);
(useRecoilValue as jest.Mock).mockReturnValueOnce(mockSchema).mockReturnValueOnce(mockMarcPreviewMetadata);
(useRecoilState as jest.Mock).mockReturnValue([mockSelectedEntries, mockSetSelectedEntries]);
(useResetRecoilState as jest.Mock).mockReturnValue(mockResetRecoilState);
(useMarcData as jest.Mock).mockReturnValue({
fetchMarcData: jest.fn().mockResolvedValue({ matchedId: 'newSrsId' }),
clearMarcData: mockClearhMarcData,
});

result = getRenderedHook()?.result;
});
Expand Down Expand Up @@ -124,19 +147,20 @@ describe('useComplexLookup', () => {
id: 'newId',
label: 'newTitle',
meta: {
srsId: 'newSrsId',
type: AdvancedFieldType.complex,
},
},
];

test('updates state correctly', () => {
test('updates state correctly', async () => {
(getLinkedField as jest.Mock).mockReturnValue(mockLinkedField);
(updateLinkedFieldValue as jest.Mock).mockReturnValue({ uuid: 'newLinkedFieldId' });
(getUpdatedSelectedEntries as jest.Mock).mockReturnValue(['newId']);

result = getRenderedHook()?.result;

act(() => {
await act(async () => {
result.current.handleAssign(mockAssignRecord);
});

Expand All @@ -153,15 +177,15 @@ describe('useComplexLookup', () => {
expect(mockSetSelectedEntries).toHaveBeenCalledWith(['newId']);
});

test('updates state correctly and does not call "setSelectedEntries"', () => {
test('updates state correctly and does not call "setSelectedEntries"', async () => {
result = getRenderedHook({
...mockEntry,
linkedEntry: {
dependent: false,
},
} as unknown as SchemaEntry)?.result;

act(() => {
await act(async () => {
result.current.handleAssign(mockAssignRecord);
});

Expand Down
8 changes: 5 additions & 3 deletions src/test/__tests__/components/SearchFilters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jest.mock('react-router-dom', () => ({
useSearchParams: () => [{}, setSearchParams],
}));

jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: false }));

describe('SearchFilters', () => {
const filters = [
{
Expand Down Expand Up @@ -62,17 +64,17 @@ describe('SearchFilters', () => {
);

test('changes limiters', () => {
const initRadio = screen.getByRole('radio', { name: 'ld.allTime' });
const initRadio = screen.getByLabelText('ld.allTime');

expect(initRadio).toBeChecked();

fireEvent.click(screen.getByRole('radio', { name: 'ld.past12Months' }));
fireEvent.click(screen.getByLabelText('ld.past12Months'));

expect(initRadio).not.toBeChecked();
});

test('adds and removes to the selection of limiters with multiselection option', () => {
const initCheckbox = screen.getByRole('checkbox', { name: 'ld.volume' });
const initCheckbox = screen.getByLabelText('ld.volume');
expect(initCheckbox).not.toBeChecked();

fireEvent.click(initCheckbox);
Expand Down

0 comments on commit a313fee

Please sign in to comment.