From d9abb45fa4f6b1d1426f159c2baaff0a348f2cc6 Mon Sep 17 00:00:00 2001 From: clukhei Date: Fri, 26 Apr 2024 21:24:36 +0800 Subject: [PATCH] fix(datepicker): reset button also reset calendar view to day --- src/DatePicker/DatePicker.tsx | 3 +- tests/DatePicker/DatePicker.test.tsx | 48 +++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/DatePicker/DatePicker.tsx b/src/DatePicker/DatePicker.tsx index 2da6f60f..4e86eb67 100644 --- a/src/DatePicker/DatePicker.tsx +++ b/src/DatePicker/DatePicker.tsx @@ -308,6 +308,7 @@ export const DatePicker: BsPrefixRefForwardingComponent< ? { start: undefined, end: undefined } : undefined, }); + setView("day"); const resetFocusedDate = new Date(); updateFocusedDate(resetFocusedDate); props.onClear?.(); @@ -880,7 +881,7 @@ export const DatePicker: BsPrefixRefForwardingComponent< onClick={clear} disabled={props.disabled} variant={clearBtnVariant} - aria-label="Clear Selection" + aria-label="Reset Datepicker" > clear diff --git a/tests/DatePicker/DatePicker.test.tsx b/tests/DatePicker/DatePicker.test.tsx index 2c11e7fd..cf3a964a 100644 --- a/tests/DatePicker/DatePicker.test.tsx +++ b/tests/DatePicker/DatePicker.test.tsx @@ -242,7 +242,7 @@ describe('DatePicker', () => { ); fireEvent.click( - container.querySelector('button[aria-label="Clear Selection"]')! + container.querySelector('button[aria-label="Reset Datepicker"]')! ); await waitFor(() => { @@ -2218,3 +2218,49 @@ describe('Datepicker a11y', () => { expect(input.getAttribute('aria-describedby')).toEqual(feedbackId); }); }); + +describe('Datepicker reset button', () => { + it('resets calendar to day view and input when clear button is clicked', async () => { + const { container } = render(); + const thisMonth = MONTH_LABELS[new Date().getMonth()]; + const thisYear = new Date().getFullYear(); + const calendarBtn = container.querySelector( + 'button.dropdown-toggle' + ) as HTMLButtonElement; + const resetBtn = container.querySelector( + 'button[aria-label="Reset Datepicker"]' + ) as HTMLButtonElement; + //Open calendar + fireEvent.click(calendarBtn); + await waitFor(() => { + expect( + container.querySelector('.dropdown-menu.datepicker.sgds.show') + ).toBeInTheDocument(); + }); + //navigate to month view + const headerBtn = container.querySelector( + 'button[aria-live="polite"]' + ) as HTMLButtonElement; + expect(headerBtn.textContent).toEqual(`${thisMonth} ${thisYear}`); + fireEvent.click(headerBtn); + await waitFor(() => + expect(headerBtn?.textContent).toEqual(thisYear.toString()) + ); + // clicking reset button + fireEvent.click(resetBtn); + await waitFor(() => + expect(headerBtn?.textContent).toEqual(`${thisMonth} ${thisYear}`) + ); + }); + it('resets input to dd/mm/yyyy when button is clicked ', async () => { + const initialValue = new Date(2024, 3, 26); + const { container } = render(); + const input = container.querySelector('input'); + expect(input?.value).toEqual('26/04/2024'); + const resetBtn = container.querySelector( + 'button[aria-label="Reset Datepicker"]' + ) as HTMLButtonElement; + fireEvent.click(resetBtn); + expect(input?.value).toEqual('dd/mm/yyyy'); + }); +});