diff --git a/CHANGELOG.md b/CHANGELOG.md index 25cb15d2..739a4bc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ * Add more reusable hooks and utilities. Refs UISACQCOMP-228. * Move reusable version history components to the ACQ lib. Refs UISACQCOMP-230. * Move reusable helper function to support version history functionality. Refs UISACQCOMP-232. -* Add `useDebouncedQuery` hook to fix endless request for `DynamicSelection` component. Refs UIF-562. +* Add `useDebouncedQuery` hook to fix endless request for `DynamicSelection` component. Refs UISACQCOMP-233. ## [6.0.1](https://github.com/folio-org/stripes-acq-components/tree/v6.0.1) (2024-11-14) [Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v6.0.0...v6.0.1) diff --git a/lib/DynamicSelection/DynamicSelection.test.js b/lib/DynamicSelection/DynamicSelection.test.js index fffe5f1f..b8b73124 100644 --- a/lib/DynamicSelection/DynamicSelection.test.js +++ b/lib/DynamicSelection/DynamicSelection.test.js @@ -5,14 +5,20 @@ import { useOkapiKy } from '@folio/stripes/core'; import { ORDERS_API } from '../constants'; import { DynamicSelection } from './DynamicSelection'; - -jest.mock('@folio/stripes/core', () => ({ - ...jest.requireActual('@folio/stripes/core'), - useOkapiKy: jest.fn(), -})); +import { useDebouncedQuery } from '../hooks'; jest.useFakeTimers('modern'); +jest.mock('../hooks', () => ({ + ...jest.requireActual('../hooks'), + useDebouncedQuery: jest.fn(() => ({ + options: [], + isLoading: false, + inputValue: '', + setInputValue: jest.fn(), + })), +})); + const dataFormatter = ({ poLines }) => poLines.map(({ id, poLineNumber }) => ({ label: poLineNumber, value: id })); const defaultProps = { @@ -36,9 +42,17 @@ const kyMock = { })), }; +const mockSetInputValue = jest.fn(); + describe('DynamicSelection', () => { beforeEach(() => { useOkapiKy.mockClear().mockReturnValue(kyMock); + useDebouncedQuery.mockClear().mockReturnValue({ + isLoading: false, + options: [{ label: '11111', value: 'poLine-1' }], + inputValue: '', + setInputValue: mockSetInputValue, + }); }); it('should call debounced fetch function when \'onFilter\' was triggered', async () => { @@ -52,7 +66,7 @@ describe('DynamicSelection', () => { }); await user.click(screen.getByText('stripes-components.selection.controlLabel')); - expect(kyMock.get).toHaveBeenCalledWith(ORDERS_API, expect.objectContaining({})); + expect(mockSetInputValue).toHaveBeenCalledWith('1'); }); it('should call \'onChange\' when an option from list was selected', async () => { diff --git a/lib/DynamicSelectionFilter/DynamicSelectionFilter.test.js b/lib/DynamicSelectionFilter/DynamicSelectionFilter.test.js index 9c5cb6ea..4b882869 100644 --- a/lib/DynamicSelectionFilter/DynamicSelectionFilter.test.js +++ b/lib/DynamicSelectionFilter/DynamicSelectionFilter.test.js @@ -8,15 +8,21 @@ import { buildFiltersObj } from '../AcqList/utils'; import { ORDERS_API } from '../constants'; import { DynamicSelectionFilter } from './DynamicSelectionFilter'; -jest.mock('@folio/stripes/core', () => ({ - ...jest.requireActual('@folio/stripes/core'), - useOkapiKy: jest.fn(), -})); jest.mock('../AcqList/utils', () => ({ ...jest.requireActual('../AcqList/utils'), buildFiltersObj: jest.fn(), })); +jest.mock('../hooks', () => ({ + ...jest.requireActual('../hooks'), + useDebouncedQuery: jest.fn(() => ({ + options: [{ label: '11111', value: 'poLine-1' }], + isLoading: false, + inputValue: '', + setInputValue: jest.fn(), + })), +})); + jest.useFakeTimers('modern'); const dataFormatter = ({ poLines }) => poLines.map(({ id, poLineNumber }) => ({ label: poLineNumber, value: id }));