|
| 1 | +import { MockGamutProvider, setupRtl } from '@codecademy/gamut-tests'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import type { FC } from 'react'; |
| 5 | + |
| 6 | +import { DatePickerProvider } from '../../DatePickerContext'; |
| 7 | +import { |
| 8 | + createMockRangeContext, |
| 9 | + createMockSingleContext, |
| 10 | +} from '../../DatePickerContext/__tests__/mockContexts'; |
| 11 | +import type { DatePickerContextValue } from '../../DatePickerContext/types'; |
| 12 | +import { DatePickerCalendar } from '..'; |
| 13 | + |
| 14 | +jest.mock('react-use', () => { |
| 15 | + const actual = jest.requireActual<typeof import('react-use')>('react-use'); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + /** One-month layout in tests (stable gridcell queries). */ |
| 19 | + useMedia: jest.fn(() => false), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +type CalendarHarnessProps = { context: DatePickerContextValue }; |
| 24 | + |
| 25 | +const DatePickerCalendarHarness: FC<CalendarHarnessProps> = ({ context }) => ( |
| 26 | + <DatePickerProvider value={context}> |
| 27 | + <DatePickerCalendar dialogId="test-cal-dialog" /> |
| 28 | + </DatePickerProvider> |
| 29 | +); |
| 30 | + |
| 31 | +const renderCalendar = setupRtl(DatePickerCalendarHarness, { |
| 32 | + context: createMockSingleContext({ |
| 33 | + isCalendarOpen: true, |
| 34 | + selectedDate: new Date(2024, 2, 1), |
| 35 | + }), |
| 36 | +}); |
| 37 | + |
| 38 | +describe('DatePickerCalendar', () => { |
| 39 | + it('throws when rendered without DatePickerProvider', () => { |
| 40 | + expect(() => |
| 41 | + render( |
| 42 | + <MockGamutProvider> |
| 43 | + <DatePickerCalendar dialogId="orphan" /> |
| 44 | + </MockGamutProvider> |
| 45 | + ) |
| 46 | + ).toThrow(/useDatePickerContext must be used within a DatePicker/); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders a calendar grid when the picker context is open', () => { |
| 50 | + const { view } = renderCalendar(); |
| 51 | + |
| 52 | + expect(view.getByRole('grid')).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('calls setSelection when a day cell is activated in single mode', async () => { |
| 56 | + const onSelection = jest.fn(); |
| 57 | + const { view } = renderCalendar({ |
| 58 | + context: createMockSingleContext({ |
| 59 | + isCalendarOpen: true, |
| 60 | + selectedDate: new Date(2024, 2, 1), |
| 61 | + onSelection, |
| 62 | + }), |
| 63 | + }); |
| 64 | + |
| 65 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 66 | + await userEvent.click(march20); |
| 67 | + |
| 68 | + expect(onSelection).toHaveBeenCalledWith(new Date(2024, 2, 20)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('calls onSelection(null) when the already-selected day is clicked again in single mode', async () => { |
| 72 | + const onSelection = jest.fn(); |
| 73 | + const selected = new Date(2024, 2, 20); |
| 74 | + const { view } = renderCalendar({ |
| 75 | + context: createMockSingleContext({ |
| 76 | + isCalendarOpen: true, |
| 77 | + selectedDate: selected, |
| 78 | + onSelection, |
| 79 | + }), |
| 80 | + }); |
| 81 | + |
| 82 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 83 | + await userEvent.click(march20); |
| 84 | + |
| 85 | + expect(onSelection).toHaveBeenCalledWith(null); |
| 86 | + }); |
| 87 | + |
| 88 | + it('calls closeCalendar after selecting a date in single mode', async () => { |
| 89 | + const closeCalendar = jest.fn(); |
| 90 | + const { view } = renderCalendar({ |
| 91 | + context: createMockSingleContext({ |
| 92 | + isCalendarOpen: true, |
| 93 | + selectedDate: new Date(2024, 2, 1), |
| 94 | + closeCalendar, |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 99 | + await userEvent.click(march20); |
| 100 | + |
| 101 | + expect(closeCalendar).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('invokes closeCalendar when Escape is pressed on the grid', async () => { |
| 105 | + const closeCalendar = jest.fn(); |
| 106 | + const { view } = renderCalendar({ |
| 107 | + context: createMockSingleContext({ |
| 108 | + isCalendarOpen: true, |
| 109 | + selectedDate: new Date(2024, 2, 1), |
| 110 | + closeCalendar, |
| 111 | + }), |
| 112 | + }); |
| 113 | + |
| 114 | + const march15 = view.getByRole('gridcell', { name: /March 15, 2024/i }); |
| 115 | + march15.focus(); |
| 116 | + await userEvent.keyboard('{Escape}'); |
| 117 | + |
| 118 | + expect(closeCalendar).toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('calls onRangeSelection with a new start when the start field is active and a day is chosen', async () => { |
| 122 | + const onRangeSelection = jest.fn(); |
| 123 | + const { view } = renderCalendar({ |
| 124 | + context: createMockRangeContext({ |
| 125 | + isCalendarOpen: true, |
| 126 | + startDate: new Date(2024, 2, 1), |
| 127 | + endDate: null, |
| 128 | + activeRangePart: 'start', |
| 129 | + onRangeSelection, |
| 130 | + }), |
| 131 | + }); |
| 132 | + |
| 133 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 134 | + await userEvent.click(march20); |
| 135 | + |
| 136 | + expect(onRangeSelection).toHaveBeenCalledWith(new Date(2024, 2, 20), null); |
| 137 | + }); |
| 138 | + |
| 139 | + it('calls setSelection in range mode when choosing an end date with the end field active', async () => { |
| 140 | + const onRangeSelection = jest.fn(); |
| 141 | + const { view } = renderCalendar({ |
| 142 | + context: createMockRangeContext({ |
| 143 | + isCalendarOpen: true, |
| 144 | + startDate: new Date(2024, 2, 1), |
| 145 | + endDate: null, |
| 146 | + activeRangePart: 'end', |
| 147 | + onRangeSelection, |
| 148 | + }), |
| 149 | + }); |
| 150 | + |
| 151 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 152 | + await userEvent.click(march20); |
| 153 | + |
| 154 | + expect(onRangeSelection).toHaveBeenCalledWith( |
| 155 | + new Date(2024, 2, 1), |
| 156 | + new Date(2024, 2, 20) |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('calls closeCalendar in range mode when a full range is selected', async () => { |
| 161 | + const closeCalendar = jest.fn(); |
| 162 | + const { view } = renderCalendar({ |
| 163 | + context: createMockRangeContext({ |
| 164 | + isCalendarOpen: true, |
| 165 | + startDate: new Date(2024, 2, 1), |
| 166 | + endDate: null, |
| 167 | + activeRangePart: 'end', |
| 168 | + closeCalendar, |
| 169 | + }), |
| 170 | + }); |
| 171 | + |
| 172 | + const march20 = view.getByRole('gridcell', { name: /March 20, 2024/i }); |
| 173 | + await userEvent.click(march20); |
| 174 | + |
| 175 | + expect(closeCalendar).toHaveBeenCalled(); |
| 176 | + }); |
| 177 | +}); |
0 commit comments