Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIIN-1731 JEST/RTL test cases for HRIDHandlingSettings #2120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/settings/HRIDHandling/HRIDHandlingSettings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import userEvent from '@testing-library/user-event';

import '../../../test/jest/__mock__';
import { renderWithIntl } from '../../../test/jest/helpers';

import HRIDHandlingSettings from './HRIDHandlingSettings';

describe('HRIDHandlingSettings component', () => {
const mutator = {
hridSettings: {
PUT: jest.fn(() => Promise.resolve()),
},
};
const resources = {
hridSettings: {
records: [
{
id: '1',
commonRetainLeadingZeroes: true,
itemRetainLeadingZeroes: false,
instanceRetainLeadingZeroes: true,
holdingsRetainLeadingZeroes: false,
authorityRetainLeadingZeroes: true,
instance: {
prefix: 'INST',
startNumber: 1,
},
},
],
},
};

beforeEach(() => {
mutator.hridSettings.PUT.mockClear();
renderWithIntl(
<MemoryRouter>
<HRIDHandlingSettings
mutator={mutator}
resources={resources}
/>
</MemoryRouter>
);
});
afterEach(() => {
jest.clearAllMocks();
});

it('renders checkbox for removing leading zeroes', () => {
const checkBox = screen.getByRole('checkbox', { id: 'checkbox-3' });
userEvent.click(checkBox);
expect(checkBox).toBeChecked();
const ConfirmationButton = screen.getByRole('button', { name: /confirm/i });
expect(ConfirmationButton).toBeInTheDocument();
userEvent.click(ConfirmationButton);
});

it('Cancellation Button', () => {
const CancellationButton = screen.getByRole('button', { name: 'cancel' });
expect(CancellationButton).toBeInTheDocument();
userEvent.click(CancellationButton);
});
it('allows the user to input values for "startWith" and "assignPrefix" fields', () => {
const startWithFields = [
screen.getByLabelText(/ui-inventory.hridHandling.label.startWith */i, { selector: 'input[name="instances.startNumber"]' }),
screen.getByLabelText(/ui-inventory.hridHandling.label.startWith */i, { selector: 'input[name="holdings.startNumber"]' }),
screen.getByLabelText(/ui-inventory.hridHandling.label.startWith */i, { selector: 'input[name="items.startNumber"]' }),
];
const assignPrefixFields = [
screen.getByLabelText(/ui-inventory.hridHandling.label.assignPrefix/i, { selector: 'input[name="instances.prefix"]' }),
screen.getByLabelText(/ui-inventory.hridHandling.label.assignPrefix/i, { selector: 'input[name="holdings.prefix"]' }),
screen.getByLabelText(/ui-inventory.hridHandling.label.assignPrefix/i, { selector: 'input[name="items.prefix"]' }),
];
const testValues = ['100', 'prefix-', '200', 'prefix2-', '300', 'prefix3-'];
startWithFields.forEach((field, index) => userEvent.type(field, testValues[index * 2]));
assignPrefixFields.forEach((field, index) => userEvent.type(field, testValues[index * 2 + 1]));
startWithFields.forEach((field, index) => expect(field.value).toBe(testValues[index * 2]));
assignPrefixFields.forEach((field, index) => expect(field.value).toBe(testValues[index * 2 + 1]));
const saveAndCloseButton = screen.getByRole('button', { name: /stripes-components.saveAndClose/i });
userEvent.click(saveAndCloseButton);
const ConfirmationButton = screen.getByRole('button', { name: /confirm/i });
userEvent.click(ConfirmationButton);
expect(mutator.hridSettings.PUT).toHaveBeenCalled();
});
});

describe('HRIDHandlingSettings - commonRetainLeadingZeroes', () => {
const initialSettings = {
commonRetainLeadingZeroes: false,
locations: {
startNumber: '00000000001',
},
holdings: {
startNumber: '00000000002',
},
};
it('rendedr commonRetainLeadingZeroes', () => {
renderWithIntl(
<MemoryRouter>
<HRIDHandlingSettings
mutator={{ hridSettings: { PUT: jest.fn() } }}
resources={{ hridSettings: { records: [initialSettings] } }}
/>
</MemoryRouter>
);
const checkBox = screen.getByRole('checkbox', { id: 'checkbox-3' });
expect(checkBox).toBeChecked();
});
});
4 changes: 2 additions & 2 deletions test/jest/__mock__/stripesComponents.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import React from 'react';

jest.mock('@folio/stripes/components', () => ({
...jest.requireActual('@folio/stripes/components'),
ConfirmationModal: jest.fn(({ heading, message, onConfirm, onCancel }) => (
ConfirmationModal: jest.fn(({ heading, message, onConfirm, onCancel, onRemove }) => (
<div>
<span>ConfirmationModal</span>
{heading}
<div>{message}</div>
<div>
<button type="button" onClick={onConfirm}>confirm</button>
<button type="button" onClick={onCancel}>cancel</button>
<button type="button" onClick={onRemove}>remove</button>
</div>
</div>
)),
Expand All @@ -28,5 +29,4 @@ jest.mock('@folio/stripes/components', () => ({
Loading: () => <div>Loading</div>,
LoadingPane: () => <div>LoadingPane</div>,
LoadingView: () => <div>LoadingView</div>,
LoadingPane: () => <div>LoadingPane</div>,
}), { virtual: true });