From a313fee1ae102b71534be5876bb501b579a71ee9 Mon Sep 17 00:00:00 2001 From: Siarhei Karol <135722306+SKarolFolio@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:45:23 +0500 Subject: [PATCH] MODLD-547: fix for broken tests (#8) --- .github/workflows/ui.yml | 2 +- .../common/hooks/useComplexLookup.test.ts | 38 +++++++++++++++---- .../components/SearchFilters.test.tsx | 8 ++-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 190a3bb4..a5d24eec 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -13,5 +13,5 @@ jobs: secrets: inherit with: compile-translations: false - jest-enabled: false + jest-test-command: npm run test:unit sonar-enabled: false \ No newline at end of file diff --git a/src/test/__tests__/common/hooks/useComplexLookup.test.ts b/src/test/__tests__/common/hooks/useComplexLookup.test.ts index 930f5280..b28762dd 100644 --- a/src/test/__tests__/common/hooks/useComplexLookup.test.ts +++ b/src/test/__tests__/common/hooks/useComplexLookup.test.ts @@ -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 { @@ -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', @@ -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; @@ -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; }); @@ -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); }); @@ -153,7 +177,7 @@ 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: { @@ -161,7 +185,7 @@ describe('useComplexLookup', () => { }, } as unknown as SchemaEntry)?.result; - act(() => { + await act(async () => { result.current.handleAssign(mockAssignRecord); }); diff --git a/src/test/__tests__/components/SearchFilters.test.tsx b/src/test/__tests__/components/SearchFilters.test.tsx index 1f2386ae..78ca5f4a 100644 --- a/src/test/__tests__/components/SearchFilters.test.tsx +++ b/src/test/__tests__/components/SearchFilters.test.tsx @@ -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 = [ { @@ -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);