From e0bc47eea5b2b1c365d5ed9e534a129e61c56615 Mon Sep 17 00:00:00 2001
From: UladzislauKutarkin
<72550466+UladzislauKutarkin@users.noreply.github.com>
Date: Tue, 14 May 2024 10:39:34 +0400
Subject: [PATCH] UIIN-2670:Jest/RTL: Cover LocationSelectionWithCheck
components with unit tests (#2480)
---
CHANGELOG.md | 1 +
.../common/LocationSelectionWithCheck.test.js | 97 +++++++++++++++++++
2 files changed, 98 insertions(+)
create mode 100644 src/edit/common/LocationSelectionWithCheck.test.js
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 687283b69..8845c4274 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/src/edit/common/LocationSelectionWithCheck.test.js b/src/edit/common/LocationSelectionWithCheck.test.js
new file mode 100644
index 000000000..5387ea013
--- /dev/null
+++ b/src/edit/common/LocationSelectionWithCheck.test.js
@@ -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 (
+
+ );
+ } }));
+
+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(
+
+ )));
+
+ 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(
+
+ )));
+
+ const cancelButton = screen.getByRole('button', { name: 'cancel' });
+
+ act(() => fireEvent.click(screen.getByTestId('selection-id')));
+
+ fireEvent.click(cancelButton);
+ expect(inputMock.onChange).not.toHaveBeenCalled();
+ });
+});