Skip to content

Commit

Permalink
UIIN-2670:Jest/RTL: Cover LocationSelectionWithCheck components with …
Browse files Browse the repository at this point in the history
…unit tests (#2480)
  • Loading branch information
UladzislauKutarkin committed May 14, 2024
1 parent 7858ac5 commit e0bc47e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Change label of eye-readable call number search option in holdings/items. Refs UIIN-2797.
* Jest/RTL: Cover ModalContent components with unit tests. Refs UIIN-2669.
* Add callout noting user's active affiliation when it changes after selecting holding or item. Refs UIIN-2831, UIIN-2872.
* Jest/RTL: Cover LocationSelectionWithCheck components with unit tests. Refs UIIN-2670.

## [11.0.4](https://github.com/folio-org/ui-inventory/tree/v11.0.4) (2024-04-30)
[Full Changelog](https://github.com/folio-org/ui-inventory/compare/v11.0.3...v11.0.4)
Expand Down
97 changes: 97 additions & 0 deletions src/edit/common/LocationSelectionWithCheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React from 'react';

import {
screen,
act,
} from '@folio/jest-config-stripes/testing-library/react';
import { fireEvent } from '@folio/jest-config-stripes/testing-library/dom';

import '../../../test/jest/__mock__';
import { LocationSelectionWithCheck } from './LocationSelectionWithCheck';
import { renderWithFinalForm, renderWithIntl } from '../../../test/jest/helpers';
import renderWithRouter from '../../../test/jest/helpers/renderWithRouter';

jest.mock('../../RemoteStorageService', () => ({
Check: {
useByLocation: jest.fn(() => jest.fn(() => true)), // Mocking always true for check
},
Confirmation: {
Heading: jest.fn(() => 'Remote Storage Heading'),
Message: jest.fn(() => 'Remote Storage Message'),
},
}));

jest.mock('@folio/stripes/smart-components', () => (
{ ...jest.requireActual('@folio/stripes/smart-components'),
LocationSelection: (props) => {
const { onSelect, resources } = props;
return (
<button
type="button"
data-testid="selection-id"
onClick={() => onSelect(resources.locations.records)}
>
LocationSelection
</button>
);
} }));

const inputMock = {
value: 'test',
onChange: jest.fn(),
};

const restMock = {
meta: {
initial: 1
},
resources: {
'id': '2b51c2f5-6779-4913-9cc8-05508ea406a4',
'isPrimary': false,
'tenantId': 'diku',
'tenantName': 'Snapshot',
'userId': 'f2a0e0ce-cef9-51e5-979a-7cea9db21ecf',
'username': 'diku_admin',
locations: {
records:
{
name: 'Test Location',
id: '2',
code: '1',
isActive: true,
}
}
},
};

describe('LocationSelectionWithCheck', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('render with selected location and confirm changes', async () => {
renderWithIntl(renderWithRouter(renderWithFinalForm(
<LocationSelectionWithCheck name="location" input={inputMock} {...restMock} />
)));

const confirmButton = screen.getByRole('button', { name: 'confirm' });

act(() => fireEvent.click(screen.getByTestId('selection-id')));

fireEvent.click(confirmButton);
expect(inputMock.onChange).toHaveBeenCalled();
});

it('render with selected location and cancel changes', async () => {
renderWithIntl(renderWithRouter(renderWithFinalForm(
<LocationSelectionWithCheck name="location" input={inputMock} {...restMock} />
)));

const cancelButton = screen.getByRole('button', { name: 'cancel' });

act(() => fireEvent.click(screen.getByTestId('selection-id')));

fireEvent.click(cancelButton);
expect(inputMock.onChange).not.toHaveBeenCalled();
});
});

0 comments on commit e0bc47e

Please sign in to comment.