Skip to content

Commit

Permalink
UISACQCOMP-227 added tests for AcqDateRangeFilter reset
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanDenis committed Nov 6, 2024
1 parent f231c8c commit a3d40c3
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lib/AcqDateRangeFilter/AcqDateRangeFilter.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { noop } from 'lodash';

import { nativeChangeFieldValue } from '@folio/stripes/components';

import AcqDateRangeFilter from './AcqDateRangeFilter';

jest.mock('@folio/stripes/components', () => ({
...jest.requireActual('@folio/stripes-components'),
nativeChangeFieldValue: jest.fn(),
}));

const FILTER_LABEL = 'some date filter';
const FILTER_NAME = 'some-date-filter';

const mockSubscribeOnReset = jest.fn();

const renderFilter = (closedByDefault, onChange = noop, dateFormat) => (render(
<AcqDateRangeFilter
id="some-date-filter"
Expand All @@ -15,6 +24,7 @@ const renderFilter = (closedByDefault, onChange = noop, dateFormat) => (render(
closedByDefault={closedByDefault}
onChange={onChange}
dateFormat={dateFormat}
subscribeOnReset={mockSubscribeOnReset}
/>,
));

Expand All @@ -32,6 +42,12 @@ describe('AcqDateRangeFilter component', () => {
expect(button.getAttribute('aria-expanded') || 'false').toBe('false');
});

it('should subscribe to reset events', () => {
renderFilter();

expect(mockSubscribeOnReset).toHaveBeenCalled();
});

it('should be opened by default when closedByDefault=false prop is passed', () => {
const { container } = renderFilter(false);
const button = container.querySelector('[id="accordion-toggle-button-some-date-filter"]');
Expand Down Expand Up @@ -78,4 +94,26 @@ describe('AcqDateRangeFilter component', () => {

expect(onChangeFilter).toHaveBeenCalled();
});

describe('when reset handler is called', () => {
it('should clear dates', async () => {
const callResetHandler = mockSubscribeOnReset.mockImplementation(cb => {
cb?.();
});

const { getByLabelText } = renderFilter(false, () => {}, 'YYYY-DD-MM');
const fromDate = getByLabelText('stripes-smart-components.dateRange.from');
const toDate = getByLabelText('stripes-smart-components.dateRange.to');

fireEvent.change(fromDate, { target: { value: '2000-01-01' } });
fireEvent.change(toDate, { target: { value: '2020-01-01' } });

expect(fromDate).toHaveValue('2000-01-01');
expect(toDate).toHaveValue('2020-01-01');

callResetHandler();

await waitFor(() => expect(nativeChangeFieldValue).toHaveBeenCalledTimes(2));
});
});
});

0 comments on commit a3d40c3

Please sign in to comment.