Skip to content

Commit

Permalink
update existing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 18, 2024
1 parent a79a586 commit 9c42770
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/common/hooks/useComplexLookupSearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useComplexLookupSearchResults = ({
formatMessage,
onAssign: onAssignRecord,
onTitleClick,
validateId: checkFailedId,
checkFailedId,
})
: row[key].label,
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/ComplexLookupField/formatters/Assign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { Button, ButtonType } from '@components/Button';
export const AssignFormatter = ({
row,
onAssign,
validateId,
checkFailedId,
}: {
row: SearchResultsTableRow;
onAssign: ({ id, title, linkedFieldValue }: ComplexLookupAssignRecordDTO) => void;
validateId: (id: string) => boolean;
checkFailedId: (id: string) => boolean;
}) => {
const isAuthorized = row.authorized.label === AuthRefType.Authorized;
const isDisabled = validateId(row.__meta.id);
const isDisabled = checkFailedId(row.__meta.id);

return isAuthorized ? (
<Button
Expand Down
10 changes: 10 additions & 0 deletions src/test/__tests__/common/hooks/useComplexLookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import {
} from '@src/test/__mocks__/providers/ServicesProvider.mock';
import { setInitialGlobalState } from '@src/test/__mocks__/store';
import { useInputsStore } from '@src/store';
import { useApi } from '@common/hooks/useApi';

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

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

const mockEntry = {
uuid: 'testUuid',
Expand Down Expand Up @@ -81,6 +84,10 @@ describe('useComplexLookup', () => {
clearMarcData: mockClearhMarcData,
});

(useApi as jest.Mock).mockReturnValue({
makeRequest: mockMakeRequest,
});

result = getRenderedHook()?.result;
});

Expand Down Expand Up @@ -155,6 +162,7 @@ describe('useComplexLookup', () => {
(getLinkedField as jest.Mock).mockReturnValue(mockLinkedField);
(updateLinkedFieldValue as jest.Mock).mockReturnValue({ uuid: 'newLinkedFieldId' });
(getUpdatedSelectedEntries as jest.Mock).mockReturnValue(['newId']);
mockMakeRequest.mockResolvedValue(true);

result = getRenderedHook()?.result;

Expand All @@ -176,6 +184,8 @@ describe('useComplexLookup', () => {
});

test('updates state correctly and does not call "setSelectedEntries"', async () => {
mockMakeRequest.mockResolvedValue(true);

result = getRenderedHook({
...mockEntry,
linkedEntry: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ describe('authoritiesTableConfig', () => {
},
title: {
label: 'ld.headingReference',
position: 1,
position: 2,
className: 'cell-fixed cell-fixed-370',
formatter: mockTitleFormatter,
},
subclass: {
label: 'ld.typeOfHeading',
position: 2,
position: 3,
className: 'cell-fixed cell-fixed-140',
},
authoritySource: {
label: 'ld.authoritySource',
position: 3,
position: 4,
className: 'cell-fixed cell-fixed-250',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,60 @@ import { AuthRefType } from '@common/constants/search.constants';
import { AssignFormatter } from '@components/ComplexLookupField/formatters';

describe('AssignFormatter', () => {
it('renders Button when authorized', () => {
const mockOnAssign = jest.fn();
const mockOnAssign = jest.fn();
const mockCheckFailedId = jest.fn();

describe('Button', () => {
const row = {
authorized: { label: AuthRefType.Authorized },
__meta: { id: '1' },
title: { label: 'Title 1' },
subclass: { label: 'Subclass 1' },
};

const { getByTestId } = render(AssignFormatter({ row, onAssign: mockOnAssign }));
const button = getByTestId('assign-button-1');
it('renders when authorized', () => {
mockCheckFailedId.mockReturnValue(false);

const { getByTestId } = render(
AssignFormatter({ row, onAssign: mockOnAssign, checkFailedId: mockCheckFailedId }),
);
const button = getByTestId('assign-button-1');

expect(button).toBeInTheDocument();

fireEvent.click(button);

expect(mockOnAssign).toHaveBeenCalledWith({
id: '1',
title: 'Title 1',
linkedFieldValue: 'Subclass 1',
});
});

expect(button).toBeInTheDocument();
it('renders disabled Button', () => {
mockCheckFailedId.mockReturnValue(true);

fireEvent.click(button);
const { getByTestId } = render(
AssignFormatter({ row, onAssign: mockOnAssign, checkFailedId: mockCheckFailedId }),
);
const button = getByTestId('assign-button-1');

expect(mockOnAssign).toHaveBeenCalledWith({
id: '1',
title: 'Title 1',
linkedFieldValue: 'Subclass 1',
expect(button).toBeInTheDocument();
expect(button).toBeDisabled();
});
});

it('does not render Button when not authorized', () => {
const mockOnAssign = jest.fn();
const row = {
authorized: { label: AuthRefType.AuthRef },
__meta: { id: '2' },
title: { label: 'Title 2' },
subclass: { label: 'Subclass 2' },
};

const { queryByTestId } = render(AssignFormatter({ row, onAssign: mockOnAssign }));
const { queryByTestId } = render(
AssignFormatter({ row, onAssign: mockOnAssign, checkFailedId: mockCheckFailedId }),
);

expect(queryByTestId('assign-button-2')).toBeNull();
});
Expand Down

0 comments on commit 9c42770

Please sign in to comment.