From 3586aad891e0939609c6b89bf626b9fc44bbb93c Mon Sep 17 00:00:00 2001 From: Denys Bohdan Date: Wed, 6 Nov 2024 13:52:37 +0100 Subject: [PATCH] UISACQCOMP-227 added tests for AcqDateRangeFilter reset --- .../AcqDateRangeFilter.test.js | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/AcqDateRangeFilter/AcqDateRangeFilter.test.js b/lib/AcqDateRangeFilter/AcqDateRangeFilter.test.js index 591923e4..df286bfa 100644 --- a/lib/AcqDateRangeFilter/AcqDateRangeFilter.test.js +++ b/lib/AcqDateRangeFilter/AcqDateRangeFilter.test.js @@ -1,5 +1,5 @@ import React from 'react'; -import { render, fireEvent } from '@testing-library/react'; +import { render, fireEvent, waitFor } from '@testing-library/react'; import { noop } from 'lodash'; import AcqDateRangeFilter from './AcqDateRangeFilter'; @@ -7,6 +7,8 @@ import AcqDateRangeFilter from './AcqDateRangeFilter'; const FILTER_LABEL = 'some date filter'; const FILTER_NAME = 'some-date-filter'; +const mockSubscribeOnReset = jest.fn(); + const renderFilter = (closedByDefault, onChange = noop, dateFormat) => (render( (render( closedByDefault={closedByDefault} onChange={onChange} dateFormat={dateFormat} + subscribeOnReset={mockSubscribeOnReset} />, )); @@ -32,6 +35,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"]'); @@ -78,4 +87,24 @@ describe('AcqDateRangeFilter component', () => { expect(onChangeFilter).toHaveBeenCalled(); }); + + describe('when reset handler is called', () => { + it('should clear dates', async () => { + const callResetHandler = mockSubscribeOnReset.mockImplementationOnce(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(fromDate).toHaveValue('')); + await waitFor(() => expect(toDate).toHaveValue('')); + }); + }); });