|
| 1 | +import React, {createRef} from 'react'; |
| 2 | +import {render, act} from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import Calendar, {CalendarHandle} from '../../../src/utils/calendar/Calendar'; |
| 5 | + |
| 6 | +describe('Calendar month visibility when calling setVisibleDate', () => { |
| 7 | + const calendarRef = createRef<CalendarHandle>(); |
| 8 | + |
| 9 | + function getDisplayedMonths() { |
| 10 | + // Find all month headers in the calendar |
| 11 | + const monthHeaders = Array.from( |
| 12 | + document.querySelectorAll('.dash-datepicker-calendar-month-header') |
| 13 | + ); |
| 14 | + return monthHeaders.map(header => header.textContent); |
| 15 | + } |
| 16 | + |
| 17 | + function renderCalendar(date: string, numberOfMonthsShown: number) { |
| 18 | + return render( |
| 19 | + <Calendar |
| 20 | + ref={calendarRef} |
| 21 | + initialVisibleDate={new Date(date)} |
| 22 | + numberOfMonthsShown={numberOfMonthsShown} |
| 23 | + monthFormat="MMMM YYYY" |
| 24 | + onSelectionChange={() => null} |
| 25 | + /> |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + describe('Calendar months start centered around initialVisibleDate', () => { |
| 30 | + test('shows current & next month when numberOfMonthsShown=2', () => { |
| 31 | + renderCalendar('2024-06-01', 2); |
| 32 | + expect(getDisplayedMonths()).toEqual(['June 2024', 'July 2024']); |
| 33 | + }); |
| 34 | + |
| 35 | + test('shows previous, current, and next months when numberOfMonthsShown=3', () => { |
| 36 | + renderCalendar('2024-06-01', 3); |
| 37 | + expect(getDisplayedMonths()).toEqual([ |
| 38 | + 'May 2024', |
| 39 | + 'June 2024', |
| 40 | + 'July 2024', |
| 41 | + ]); |
| 42 | + }); |
| 43 | + |
| 44 | + test('shows previous, current, and next 2 months when numberOfMonthsShown=4', () => { |
| 45 | + renderCalendar('2024-06-01', 4); |
| 46 | + expect(getDisplayedMonths()).toEqual([ |
| 47 | + 'May 2024', |
| 48 | + 'June 2024', |
| 49 | + 'July 2024', |
| 50 | + 'August 2024', |
| 51 | + ]); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('calendar stays static when navigating to already-displayed dates', () => { |
| 56 | + test('does not re-order displayed months (numberOfMonthsShown=2)', () => { |
| 57 | + renderCalendar('2024-06-01', 2); |
| 58 | + const expectedCalendarMonths = ['June 2024', 'July 2024']; |
| 59 | + |
| 60 | + // Navigate to second visible month (July) |
| 61 | + act(() => { |
| 62 | + calendarRef.current?.setVisibleDate(new Date('2024-07-23')); |
| 63 | + }); |
| 64 | + |
| 65 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 66 | + |
| 67 | + // Navigate to first visible month (June) |
| 68 | + act(() => { |
| 69 | + calendarRef.current?.setVisibleDate(new Date('2024-06-15')); |
| 70 | + }); |
| 71 | + |
| 72 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 73 | + }); |
| 74 | + |
| 75 | + test('does not re-order displayed months (numberOfMonthsShown=3)', () => { |
| 76 | + renderCalendar('2024-06-01', 3); |
| 77 | + const expectedCalendarMonths = [ |
| 78 | + 'May 2024', |
| 79 | + 'June 2024', |
| 80 | + 'July 2024', |
| 81 | + ]; |
| 82 | + |
| 83 | + // Navigate to first visible month (May) |
| 84 | + act(() => { |
| 85 | + calendarRef.current?.setVisibleDate(new Date('2024-05-15')); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 89 | + |
| 90 | + // Navigate to last visible month (July) |
| 91 | + act(() => { |
| 92 | + calendarRef.current?.setVisibleDate(new Date('2024-07-23')); |
| 93 | + }); |
| 94 | + |
| 95 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 96 | + }); |
| 97 | + |
| 98 | + test('does not re-order displayed months (numberOfMonthsShown=4)', () => { |
| 99 | + renderCalendar('2024-06-01', 4); |
| 100 | + const expectedCalendarMonths = [ |
| 101 | + 'May 2024', |
| 102 | + 'June 2024', |
| 103 | + 'July 2024', |
| 104 | + 'August 2024', |
| 105 | + ]; |
| 106 | + |
| 107 | + // Navigate to middle visible month (June) |
| 108 | + act(() => { |
| 109 | + calendarRef.current?.setVisibleDate(new Date('2024-06-15')); |
| 110 | + }); |
| 111 | + |
| 112 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 113 | + |
| 114 | + // Navigate to another middle visible month (July) |
| 115 | + act(() => { |
| 116 | + calendarRef.current?.setVisibleDate(new Date('2024-07-23')); |
| 117 | + }); |
| 118 | + |
| 119 | + expect(getDisplayedMonths()).toEqual(expectedCalendarMonths); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('forward navigation to a non-visible month', () => { |
| 124 | + test('shows target month as last visible month (numberOfMonthsShown=2)', () => { |
| 125 | + renderCalendar('2024-06-01', 2); |
| 126 | + |
| 127 | + act(() => { |
| 128 | + calendarRef.current?.setVisibleDate(new Date('2024-08-15')); |
| 129 | + }); |
| 130 | + |
| 131 | + expect(getDisplayedMonths()).toEqual(['July 2024', 'August 2024']); |
| 132 | + }); |
| 133 | + |
| 134 | + test('shows target month as last visible month (numberOfMonthsShown=3)', () => { |
| 135 | + renderCalendar('2024-06-01', 3); |
| 136 | + |
| 137 | + act(() => { |
| 138 | + calendarRef.current?.setVisibleDate(new Date('2024-09-15')); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(getDisplayedMonths()).toEqual([ |
| 142 | + 'July 2024', |
| 143 | + 'August 2024', |
| 144 | + 'September 2024', |
| 145 | + ]); |
| 146 | + }); |
| 147 | + |
| 148 | + test('shows target month as last visible month (numberOfMonthsShown=4)', () => { |
| 149 | + renderCalendar('2024-08-01', 4); |
| 150 | + |
| 151 | + act(() => { |
| 152 | + calendarRef.current?.setVisibleDate(new Date('2025-01-17')); |
| 153 | + }); |
| 154 | + |
| 155 | + expect(getDisplayedMonths()).toEqual([ |
| 156 | + 'October 2024', |
| 157 | + 'November 2024', |
| 158 | + 'December 2024', |
| 159 | + 'January 2025', |
| 160 | + ]); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe('backward navigation to non-visible month', () => { |
| 165 | + test('shows target month as first visible month (numberOfMonthsShown=2)', () => { |
| 166 | + renderCalendar('2024-06-01', 2); |
| 167 | + |
| 168 | + act(() => { |
| 169 | + calendarRef.current?.setVisibleDate(new Date('2023-12-10')); |
| 170 | + }); |
| 171 | + |
| 172 | + expect(getDisplayedMonths()).toEqual([ |
| 173 | + 'December 2023', |
| 174 | + 'January 2024', |
| 175 | + ]); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments