From a329550edd4ca7262c6bce32761c59be45ece701 Mon Sep 17 00:00:00 2001 From: nikolai Date: Thu, 5 Dec 2024 16:13:19 +0400 Subject: [PATCH] unit tests, unused code cleanup --- src/common/helpers/record.helper.ts | 4 - src/components/FullDisplay/PreviewContent.tsx | 2 +- .../InstancesList/InstancesList.scss | 2 +- src/components/Preview/Fields.tsx | 4 - src/components/Preview/Preview.tsx | 3 - src/components/Preview/TitledPreview.tsx | 9 ++- .../common/hooks/useRecordControls.mock.ts | 2 + .../common/helpers/record.helper.test.ts | 39 +++++++++- .../helpers/recordFormatting.helper.test.ts | 44 +++++++++++ .../hooks/useProcessedRecordAndSchema.test.ts | 39 ++++++++++ .../components/InstancesList.test.tsx | 75 +++++++++++++++++++ .../components/TitledPreview.test.tsx | 59 +++++++++++++++ .../ExternalResourcePreview.tsx | 1 - 13 files changed, 265 insertions(+), 18 deletions(-) create mode 100644 src/test/__tests__/common/hooks/useProcessedRecordAndSchema.test.ts create mode 100644 src/test/__tests__/components/InstancesList.test.tsx create mode 100644 src/test/__tests__/components/TitledPreview.test.tsx diff --git a/src/common/helpers/record.helper.ts b/src/common/helpers/record.helper.ts index febbf21b..8f25896d 100644 --- a/src/common/helpers/record.helper.ts +++ b/src/common/helpers/record.helper.ts @@ -221,7 +221,6 @@ export const getPreviewFieldsConditions = ({ schema, isOnBranchWithUserValue, altDisplayNames, - hideActions, hideEntities, isEntity, forceRenderAllTopLevelEntities, @@ -233,7 +232,6 @@ export const getPreviewFieldsConditions = ({ schema: Schema; isOnBranchWithUserValue: boolean; altDisplayNames?: Record; - hideActions?: boolean; hideEntities?: boolean; isEntity: boolean; forceRenderAllTopLevelEntities?: boolean; @@ -264,7 +262,6 @@ export const getPreviewFieldsConditions = ({ const isBlock = level === GROUP_BY_LEVEL && shouldRenderLabelOrPlaceholders; const isBlockContents = level === GROUP_CONTENTS_LEVEL; const isInstance = bfid === PROFILE_BFIDS.INSTANCE; - const showEntityActions = !hideActions && isEntity; const wrapEntities = forceRenderAllTopLevelEntities && isEntity; return { @@ -277,7 +274,6 @@ export const getPreviewFieldsConditions = ({ isBlock, isBlockContents, isInstance, - showEntityActions, wrapEntities, }; }; diff --git a/src/components/FullDisplay/PreviewContent.tsx b/src/components/FullDisplay/PreviewContent.tsx index 4841db80..843e7c5c 100644 --- a/src/components/FullDisplay/PreviewContent.tsx +++ b/src/components/FullDisplay/PreviewContent.tsx @@ -38,7 +38,7 @@ export const PreviewContent = () => { {Object.keys(userValues).length ? (
- +
) : (
diff --git a/src/components/InstancesList/InstancesList.scss b/src/components/InstancesList/InstancesList.scss index 75498fe8..3ab23020 100644 --- a/src/components/InstancesList/InstancesList.scss +++ b/src/components/InstancesList/InstancesList.scss @@ -27,7 +27,7 @@ tr { padding: 0.5rem 0 } - + // text-decoration: underline; .button-primary { margin: 0.2rem 0; } diff --git a/src/components/Preview/Fields.tsx b/src/components/Preview/Fields.tsx index 001ec5c9..f942b474 100644 --- a/src/components/Preview/Fields.tsx +++ b/src/components/Preview/Fields.tsx @@ -56,7 +56,6 @@ type FieldsProps = { altSchema?: Schema; altUserValues?: UserValues; altDisplayNames?: Record; - hideActions?: boolean; hideEntities?: boolean; forceRenderAllTopLevelEntities?: boolean; }; @@ -69,7 +68,6 @@ export const Fields = ({ altSchema, altUserValues, altDisplayNames, - hideActions, hideEntities, forceRenderAllTopLevelEntities, }: FieldsProps) => { @@ -119,7 +117,6 @@ export const Fields = ({ schema, isOnBranchWithUserValue, altDisplayNames, - hideActions, hideEntities, isEntity, forceRenderAllTopLevelEntities, @@ -167,7 +164,6 @@ export const Fields = ({ altSchema={altSchema} altUserValues={altUserValues} altDisplayNames={altDisplayNames} - hideActions={hideActions} hideEntities={hideEntities} forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities} /> diff --git a/src/components/Preview/Preview.tsx b/src/components/Preview/Preview.tsx index 6454601a..a6315938 100644 --- a/src/components/Preview/Preview.tsx +++ b/src/components/Preview/Preview.tsx @@ -10,7 +10,6 @@ type IPreview = { altUserValues?: UserValues; altInitKey?: string; altDisplayNames?: Record; - hideActions?: boolean; hideEntities?: boolean; forceRenderAllTopLevelEntities?: boolean; entityRowDisplay?: boolean; @@ -21,7 +20,6 @@ export const Preview: FC = ({ altUserValues, altInitKey, altDisplayNames, - hideActions, hideEntities, forceRenderAllTopLevelEntities, entityRowDisplay, @@ -48,7 +46,6 @@ export const Preview: FC = ({ altSchema={altSchema} altUserValues={altUserValues} altDisplayNames={altDisplayNames} - hideActions={hideActions} hideEntities={hideEntities} forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities} /> diff --git a/src/components/Preview/TitledPreview.tsx b/src/components/Preview/TitledPreview.tsx index 6a56a719..d11833d0 100644 --- a/src/components/Preview/TitledPreview.tsx +++ b/src/components/Preview/TitledPreview.tsx @@ -8,7 +8,7 @@ import { useNavigateToEditPage } from '@common/hooks/useNavigateToEditPage'; import { generateEditResourceUrl } from '@common/helpers/navigation.helper'; import classNames from 'classnames'; -type ITitledPreview = { +export type ITitledPreview = { showCloseCtl?: boolean; ownId?: string; refId?: string | null; @@ -62,7 +62,12 @@ export const TitledPreview = ({ )} {type === ResourceType.work && !previewContent && ( - )} diff --git a/src/test/__mocks__/common/hooks/useRecordControls.mock.ts b/src/test/__mocks__/common/hooks/useRecordControls.mock.ts index 74de1ed5..f0bc2369 100644 --- a/src/test/__mocks__/common/hooks/useRecordControls.mock.ts +++ b/src/test/__mocks__/common/hooks/useRecordControls.mock.ts @@ -5,6 +5,7 @@ export const fetchRecord = jest.fn(); export const clearRecordState = jest.fn(); export const fetchRecordAndSelectEntityValues = jest.fn(); export const fetchExternalRecordForPreview = jest.fn(); +export const getRecordAndInitializeParsing = jest.fn(); jest.mock('@common/hooks/useRecordControls', () => ({ useRecordControls: () => ({ @@ -15,5 +16,6 @@ jest.mock('@common/hooks/useRecordControls', () => ({ saveRecordLocally, fetchRecordAndSelectEntityValues, fetchExternalRecordForPreview, + getRecordAndInitializeParsing, }), })); diff --git a/src/test/__tests__/common/helpers/record.helper.test.ts b/src/test/__tests__/common/helpers/record.helper.test.ts index 16670899..5cc3743f 100644 --- a/src/test/__tests__/common/helpers/record.helper.test.ts +++ b/src/test/__tests__/common/helpers/record.helper.test.ts @@ -199,9 +199,9 @@ describe('record.helper', () => { }); describe('getRecordTitle', () => { - const mockMainTitle = '80085' + const mockMainTitle = '80085'; const mockRecord = { - 'testInstanceUri': { + testInstanceUri: { 'http://bibfra.me/vocab/marc/title': [ { 'http://bibfra.me/vocab/marc/Title': { @@ -216,4 +216,39 @@ describe('record.helper', () => { expect(RecordHelper.getRecordTitle(mockRecord as unknown as RecordEntry)).toBe(mockMainTitle); }); }); + + describe('getRecordDependencies', () => { + test("doesn't work if there's no record", () => { + expect(RecordHelper.getRecordDependencies(null)).toBeFalsy(); + }); + + test('returns record dependencies', () => { + const record = { + testInstanceUri: { + testFieldUri_1: [], + testFieldUri_2: [], + workReferenceKey: [ + { + testWorkFieldUri_1: [], + id: ['testWorkId'], + }, + ], + }, + } as unknown as RecordEntry; + + jest.spyOn(RecordHelper, 'getEditingRecordBlocks').mockReturnValue({ + block: 'testInstanceUri', + reference: { + key: 'workReferenceKey', + uri: 'testWorkUri', + }, + }); + + expect(RecordHelper.getRecordDependencies(record)).toEqual({ + entries: [{ id: ['testWorkId'], testWorkFieldUri_1: [] }], + keys: { key: 'workReferenceKey', uri: 'testWorkUri' }, + type: undefined, + }); + }); + }); }); diff --git a/src/test/__tests__/common/helpers/recordFormatting.helper.test.ts b/src/test/__tests__/common/helpers/recordFormatting.helper.test.ts index 315c5c58..a0cdf4bc 100644 --- a/src/test/__tests__/common/helpers/recordFormatting.helper.test.ts +++ b/src/test/__tests__/common/helpers/recordFormatting.helper.test.ts @@ -26,12 +26,17 @@ describe('recordFormatting', () => { WORK: testWorkUri, NOTE: 'testNoteUri', NAME: 'testNameUri', + DATE: 'testDateUri', LABEL: 'testLabelUri', SOURCE: 'testSourceUri', CREATOR: 'testCreatorUri', CONTRIBUTOR: 'testContributorUri', PROVIDER_PLACE: 'testProviderPlaceUri', CLASSIFICATION: 'testClassificationUri', + PUBLICATION: 'testPubUri', + TITLE: 'testTitleUri', + TITLE_CONTAINER: 'testTitleContainerUri', + MAIN_TITLE: 'testMainTitleUri', }); mockBF2Constant({ ...BF2_URIS, CREATOR_NAME: 'creatorNameBF2Uri', ROLE: 'roleBF2Uri' }); mockNonBFRecordElementsConstant({ @@ -386,4 +391,43 @@ describe('recordFormatting', () => { expect(result).toEqual(testResult); }); }); + + describe('formatDependeciesTable', () => { + test('converts record dependencies into Rows', () => { + expect( + RecordFormattingHelper.formatDependeciesTable([ + { + id: 'mockId', + testTitleUri: [ + { + testTitleContainerUri: { + testMainTitleUri: ['mockTitle'], + }, + }, + ], + testPubUri: [ + { + testNameUri: ['mockPubName'], + testDateUri: ['mockPubDate'], + }, + ], + }, + ])[0], + ).toMatchObject({ + __meta: { + id: 'mockId', + key: 'mockId', + }, + title: { + label: 'mockTitle', + }, + publisher: { + label: 'mockPubName', + }, + pubDate: { + label: 'mockPubDate', + }, + }); + }); + }); }); diff --git a/src/test/__tests__/common/hooks/useProcessedRecordAndSchema.test.ts b/src/test/__tests__/common/hooks/useProcessedRecordAndSchema.test.ts new file mode 100644 index 00000000..89ba9552 --- /dev/null +++ b/src/test/__tests__/common/hooks/useProcessedRecordAndSchema.test.ts @@ -0,0 +1,39 @@ +import '@src/test/__mocks__/common/hooks/useServicesContext.mock'; +import { useProcessedRecordAndSchema } from '@common/hooks/useProcessedRecordAndSchema.hook'; +import { act, renderHook } from '@testing-library/react'; +import { useSetRecoilState } from 'recoil'; + +jest.mock('recoil'); + +describe('useProcessedRecordAndSchema', () => { + const mockSetState = jest.fn(); + const props = { + baseSchema: {} as Schema, + userValues: {}, + record: { key: 'value' }, + }; + + beforeEach(() => { + (useSetRecoilState as jest.Mock).mockReturnValueOnce(mockSetState).mockReturnValueOnce(jest.fn()); + }); + + test("doesn't update state when asked not to", () => { + const { result } = renderHook(useProcessedRecordAndSchema); + + act(() => { + result.current.getProcessedRecordAndSchema({ ...props, noStateUpdate: true }); + }); + + expect(mockSetState).not.toHaveBeenCalled(); + }); + + test('updates state when not asked to not update state', () => { + const { result } = renderHook(useProcessedRecordAndSchema); + + act(() => { + result.current.getProcessedRecordAndSchema(props); + }); + + expect(mockSetState).toHaveBeenCalled(); + }); +}); diff --git a/src/test/__tests__/components/InstancesList.test.tsx b/src/test/__tests__/components/InstancesList.test.tsx new file mode 100644 index 00000000..b02ff2a4 --- /dev/null +++ b/src/test/__tests__/components/InstancesList.test.tsx @@ -0,0 +1,75 @@ +import '@src/test/__mocks__/common/hooks/useRecordControls.mock'; +import '@src/test/__mocks__/common/hooks/useNavigateToEditPage.mock'; +import { navigateToEditPage } from '@src/test/__mocks__/common/hooks/useNavigateToEditPage.mock'; +import { getRecordAndInitializeParsing } from '@src/test/__mocks__/common/hooks/useRecordControls.mock'; +import * as RecordFormatter from '@common/helpers/recordFormatting.helper'; +import { InstancesList } from '@components/InstancesList'; +import { fireEvent, render } from '@testing-library/react'; +import { BrowserRouter } from 'react-router-dom'; + +jest.mock('recoil'); +jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: true })); + +describe('InstancesList', () => { + const renderWithProps = () => { + const contents = [ + { + __meta: { + id: 'mockId', + key: 'mockId', + }, + title: { + label: 'mockTitle', + }, + publisher: { + label: 'mockPubName', + }, + pubDate: { + label: 'mockPubDate', + }, + }, + ]; + + jest.spyOn(RecordFormatter, 'formatDependeciesTable').mockReturnValue(contents); + + return render( + + + , + , + ); + }; + test("renders table when there's content", () => { + const { getByText } = renderWithProps(); + + expect(getByText('mockTitle')).toBeInTheDocument(); + }); + + test('invokes new instance control', () => { + const { getByTestId } = renderWithProps(); + + fireEvent.click(getByTestId('new-instance')); + + expect(navigateToEditPage).toHaveBeenCalled(); + }); + + test('invokes preview control', () => { + const { getByTestId } = renderWithProps(); + + fireEvent.click(getByTestId('preview-button__mockId')); + + expect(getRecordAndInitializeParsing).toHaveBeenCalled(); + }); + + test('invokes edit control', () => { + const { getByTestId } = renderWithProps(); + + fireEvent.click(getByTestId('edit-button__mockId')); + + expect(navigateToEditPage).toHaveBeenCalled(); + }); +}); diff --git a/src/test/__tests__/components/TitledPreview.test.tsx b/src/test/__tests__/components/TitledPreview.test.tsx new file mode 100644 index 00000000..6b622eae --- /dev/null +++ b/src/test/__tests__/components/TitledPreview.test.tsx @@ -0,0 +1,59 @@ +import '@src/test/__mocks__/common/hooks/useNavigateToEditPage.mock'; +import { navigateToEditPage } from '@src/test/__mocks__/common/hooks/useNavigateToEditPage.mock'; +import { fireEvent, render } from '@testing-library/react'; +import { BrowserRouter } from 'react-router-dom'; +import { ITitledPreview, TitledPreview } from '@components/Preview/TitledPreview'; + +jest.mock('recoil'); +jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: true })); + +describe('TitledPreview', () => { + const defaultProps = { + showCloseCtl: true, + type: 'work', + onClickClose: jest.fn(), + previewContent: { + title: 'mockTitle', + id: 'mockId', + base: new Map(), + initKey: 'mockInitKey', + userValues: {}, + }, + }; + + const renderWithProps = (props: Partial | undefined = defaultProps) => + render( + + , + , + ); + + test("renders view appropriate for when there's preview content", () => { + const { getByTestId } = renderWithProps(); + + expect(getByTestId('nav-close-button')).toBeInTheDocument(); + }); + + test("renders view appropriate for when there's no preview content (single dep)", () => { + const { queryByTestId } = renderWithProps({ ...defaultProps, previewContent: undefined }); + + expect(queryByTestId('nav-close-button')).not.toBeInTheDocument(); + }); + + test('navigates to ref edit page', () => { + const { getByTestId } = renderWithProps({ ...defaultProps, previewContent: undefined }); + + fireEvent.click(getByTestId('edit-self-as-ref')); + + expect(navigateToEditPage).toHaveBeenCalled(); + }); + + test('navigates to own edit page', () => { + const { getByTestId } = renderWithProps({ ...defaultProps, type: 'instance' }); + + fireEvent.click(getByTestId('preview-actions-dropdown')); + fireEvent.click(getByTestId('preview-actions-dropdown__option-ld.edit')); + + expect(navigateToEditPage).toHaveBeenCalled(); + }); +}); diff --git a/src/views/ExternalResource/ExternalResourcePreview.tsx b/src/views/ExternalResource/ExternalResourcePreview.tsx index d5c2d624..241ec145 100644 --- a/src/views/ExternalResource/ExternalResourcePreview.tsx +++ b/src/views/ExternalResource/ExternalResourcePreview.tsx @@ -25,7 +25,6 @@ export const ExternalResourcePreview = () => { {record ? (