Skip to content

Commit

Permalink
unit tests, unused code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
s3fs committed Dec 5, 2024
1 parent de425b7 commit a329550
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 18 deletions.
4 changes: 0 additions & 4 deletions src/common/helpers/record.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ export const getPreviewFieldsConditions = ({
schema,
isOnBranchWithUserValue,
altDisplayNames,
hideActions,
hideEntities,
isEntity,
forceRenderAllTopLevelEntities,
Expand All @@ -233,7 +232,6 @@ export const getPreviewFieldsConditions = ({
schema: Schema;
isOnBranchWithUserValue: boolean;
altDisplayNames?: Record<string, string>;
hideActions?: boolean;
hideEntities?: boolean;
isEntity: boolean;
forceRenderAllTopLevelEntities?: boolean;
Expand Down Expand Up @@ -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 {
Expand All @@ -277,7 +274,6 @@ export const getPreviewFieldsConditions = ({
isBlock,
isBlockContents,
isInstance,
showEntityActions,
wrapEntities,
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/FullDisplay/PreviewContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const PreviewContent = () => {
</div>
{Object.keys(userValues).length ? (
<div data-testid="preview-contents-container" className="preview-contents-container">
<Preview altSchema={base} altUserValues={userValues} altInitKey={initKey} hideActions />
<Preview altSchema={base} altUserValues={userValues} altInitKey={initKey} />
</div>
) : (
<div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/InstancesList/InstancesList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
tr {
padding: 0.5rem 0
}

// text-decoration: underline;
.button-primary {
margin: 0.2rem 0;
}
Expand Down
4 changes: 0 additions & 4 deletions src/components/Preview/Fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type FieldsProps = {
altSchema?: Schema;
altUserValues?: UserValues;
altDisplayNames?: Record<string, string>;
hideActions?: boolean;
hideEntities?: boolean;
forceRenderAllTopLevelEntities?: boolean;
};
Expand All @@ -69,7 +68,6 @@ export const Fields = ({
altSchema,
altUserValues,
altDisplayNames,
hideActions,
hideEntities,
forceRenderAllTopLevelEntities,
}: FieldsProps) => {
Expand Down Expand Up @@ -119,7 +117,6 @@ export const Fields = ({
schema,
isOnBranchWithUserValue,
altDisplayNames,
hideActions,
hideEntities,
isEntity,
forceRenderAllTopLevelEntities,
Expand Down Expand Up @@ -167,7 +164,6 @@ export const Fields = ({
altSchema={altSchema}
altUserValues={altUserValues}
altDisplayNames={altDisplayNames}
hideActions={hideActions}
hideEntities={hideEntities}
forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities}
/>
Expand Down
3 changes: 0 additions & 3 deletions src/components/Preview/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type IPreview = {
altUserValues?: UserValues;
altInitKey?: string;
altDisplayNames?: Record<string, string>;
hideActions?: boolean;
hideEntities?: boolean;
forceRenderAllTopLevelEntities?: boolean;
entityRowDisplay?: boolean;
Expand All @@ -21,7 +20,6 @@ export const Preview: FC<IPreview> = ({
altUserValues,
altInitKey,
altDisplayNames,
hideActions,
hideEntities,
forceRenderAllTopLevelEntities,
entityRowDisplay,
Expand All @@ -48,7 +46,6 @@ export const Preview: FC<IPreview> = ({
altSchema={altSchema}
altUserValues={altUserValues}
altDisplayNames={altDisplayNames}
hideActions={hideActions}
hideEntities={hideEntities}
forceRenderAllTopLevelEntities={forceRenderAllTopLevelEntities}
/>
Expand Down
9 changes: 7 additions & 2 deletions src/components/Preview/TitledPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +62,12 @@ export const TitledPreview = ({
</h3>
)}
{type === ResourceType.work && !previewContent && (
<Button type={ButtonType.Primary} className="toggle-entity-edit" onClick={navigateToRefEditPage}>
<Button
type={ButtonType.Primary}
className="toggle-entity-edit"
data-testid="edit-self-as-ref"
onClick={navigateToRefEditPage}
>
<FormattedMessage id="ld.editWork" />
</Button>
)}
Expand Down
2 changes: 2 additions & 0 deletions src/test/__mocks__/common/hooks/useRecordControls.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand All @@ -15,5 +16,6 @@ jest.mock('@common/hooks/useRecordControls', () => ({
saveRecordLocally,
fetchRecordAndSelectEntityValues,
fetchExternalRecordForPreview,
getRecordAndInitializeParsing,
}),
}));
39 changes: 37 additions & 2 deletions src/test/__tests__/common/helpers/record.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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,
});
});
});
});
44 changes: 44 additions & 0 deletions src/test/__tests__/common/helpers/recordFormatting.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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',
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -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();
});
});
75 changes: 75 additions & 0 deletions src/test/__tests__/components/InstancesList.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<BrowserRouter>
<InstancesList
contents={{ keys: { uri: 'mockUri' }, entries: [{ key: 'value' }] }}
type="mockType"
refId="mockRefId"
/>
,
</BrowserRouter>,
);
};
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();
});
});
Loading

0 comments on commit a329550

Please sign in to comment.