import { fireEvent, mockMatchMedia, render, screen, userEvent } from '../test-utils'; import { DirectionProvider } from '../Provider/Direction/DirectionProvider'; import Calendar from './Calendar'; import type { DisabledRules } from './Calendar.types'; mockMatchMedia(); const user = userEvent.setup(); const initialValue = new Date(2000, 0, 1); describe('Calendar', () => { describe('in day view', () => { it.each([ ['{ArrowLeft}', -1], ['{ArrowRight}', +1], ['{ArrowUp}', -7], ['{ArrowDown}', +7], ])("handles '%s' to step %d day(s)", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard(text); const expected = new Date(initialValue); expected.setDate(initialValue.getDate() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }); it('selects a day on click', async () => { const handleDateFocus = jest.fn(); render(); await user.click(screen.getByText('15')); expect(handleDateFocus).toHaveBeenCalledWith(new Date(2000, 0, 15), { isEnabled: true }); }); it('fires both onDateFocus and onChange when clicking a day', async () => { const handleDateFocus = jest.fn(); const handleChange = jest.fn(); render( , ); await user.click(screen.getByText('15')); expect(handleDateFocus).toHaveBeenCalledWith(new Date(2000, 0, 15), { isEnabled: true }); expect(handleChange).toHaveBeenCalledWith(new Date(2000, 0, 15)); }); it('does not fire onChange during arrow key navigation', async () => { const handleChange = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); expect(handleChange).toHaveBeenCalledTimes(1); await user.keyboard('{ArrowRight}'); expect(handleChange).toHaveBeenCalledTimes(1); }); it('fires onChange when Enter is pressed on focused date', async () => { const handleChange = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowRight}'); await user.keyboard('{Enter}'); const expected = new Date(initialValue); expected.setDate(initialValue.getDate() + 1); expect(handleChange).toHaveBeenCalledWith(expected); }); it('does not fire onChange when Enter is pressed on a disabled focused date', async () => { const handleChange = jest.fn(); const disabledRules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'Closed on weekends.', }; render( , ); await user.click(screen.getByRole('button', { name: /selected day/iu })); // Click on selected day (Friday) fires onChange once expect(handleChange).toHaveBeenCalledTimes(1); // ArrowRight from Jan 7 (Friday) → Jan 8 (Saturday = disabled) await user.keyboard('{ArrowRight}'); await user.keyboard('{Enter}'); expect(handleChange).toHaveBeenCalledTimes(1); }); it('fires onDateFocus with isEnabled: false when Enter is pressed on a disabled focused date', async () => { const handleDateFocus = jest.fn(); const disabledRules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'Closed on weekends.', }; render( , ); await user.click(screen.getByRole('button', { name: /selected day/iu })); handleDateFocus.mockClear(); // ArrowRight from Jan 7 (Friday) → Jan 8 (Saturday = disabled) await user.keyboard('{ArrowRight}'); handleDateFocus.mockClear(); await user.keyboard('{Enter}'); expect(handleDateFocus).toHaveBeenCalledWith(new Date(2000, 0, 8), { isEnabled: false }); }); it('fires onChange when Space is pressed on a focused enabled date', async () => { const handleChange = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowRight}'); await user.keyboard(' '); const expected = new Date(initialValue); expected.setDate(initialValue.getDate() + 1); expect(handleChange).toHaveBeenCalledWith(expected); }); it('does not fire onChange when Space is pressed on a disabled focused date', async () => { const handleChange = jest.fn(); const disabledRules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'Closed on weekends.', }; render( , ); await user.click(screen.getByRole('button', { name: /selected day/iu })); expect(handleChange).toHaveBeenCalledTimes(1); // ArrowRight from Jan 7 (Friday) → Jan 8 (Saturday = disabled) await user.keyboard('{ArrowRight}'); await user.keyboard(' '); expect(handleChange).toHaveBeenCalledTimes(1); }); it('renders and remains interactive without onDateFocus', async () => { const handleChange = jest.fn(); render(); await user.click(screen.getByText('15')); expect(handleChange).toHaveBeenCalledWith(new Date(2000, 0, 15)); }); it('fires onDateFocus when a cell receives DOM focus directly (screen reader navigation)', () => { const handleDateFocus = jest.fn(); render(); const cell = screen.getByLabelText(/^15 January 2000/u); fireEvent.focus(cell); expect(handleDateFocus).toHaveBeenCalledWith(new Date(2000, 0, 15), { isEnabled: true }); }); it('does not fire onDateFocus when the already-focused cell receives focus', () => { const handleDateFocus = jest.fn(); render(); // Focus the selected date (which is already focusedDate in state) const selected = screen.getByLabelText(/^1 January 2000.*selected/iu); fireEvent.focus(selected); expect(handleDateFocus).not.toHaveBeenCalled(); }); }); describe('in year view', () => { it.each([ ['{ArrowLeft}', -1], ['{ArrowRight}', +1], ['{ArrowUp}', -4], ['{ArrowDown}', +4], ])("handles '%s' to step %d year(s)", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.keyboard(text); const expected = new Date(initialValue); expected.setFullYear(initialValue.getFullYear() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }); }); describe('in month view', () => { it.each([ ['{ArrowLeft}', -1], ['{ArrowRight}', +1], ['{ArrowUp}', -4], ['{ArrowDown}', +4], ])("handles '%s' to step %d month(s)", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.keyboard(' '); await user.keyboard(text); const expected = new Date(initialValue); expected.setMonth(initialValue.getMonth() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }); }); describe('min/max clamping', () => { it('allows keyboard navigation past min within the same month', async () => { const min = new Date(2000, 0, 15); const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowLeft}{ArrowLeft}'); expect(handleDateFocus).toHaveBeenLastCalledWith(new Date(2000, 0, 14), { isEnabled: false, }); }); it('clamps to first of month when navigating before min into a previous month', async () => { const min = new Date(2000, 0, 2); const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowLeft}{ArrowLeft}'); expect(handleDateFocus).toHaveBeenLastCalledWith(new Date(2000, 0, 1), { isEnabled: false, }); }); it('allows keyboard navigation past max within the same month', async () => { const max = new Date(2000, 0, 15); const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowRight}{ArrowRight}'); expect(handleDateFocus).toHaveBeenLastCalledWith(new Date(2000, 0, 16), { isEnabled: false, }); }); it('clamps to last of month when navigating past max into a next month', async () => { const max = new Date(2000, 0, 30); const handleDateFocus = jest.fn(); render(); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard('{ArrowRight}{ArrowRight}'); expect(handleDateFocus).toHaveBeenLastCalledWith(new Date(2000, 0, 31), { isEnabled: false, }); }); it('clamps value to range when value is outside min/max', () => { const min = new Date(2000, 0, 5); const max = new Date(2000, 0, 20); const handleDateFocus = jest.fn(); render(); expect(handleDateFocus).toHaveBeenCalledWith(min, { isEnabled: true }); }); }); describe('navigation button boundary disabling', () => { describe('day view — previous/next month', () => { it('disables previous month button when min is in the current view month', () => { render(); expect(screen.getByRole('button', { name: /previous month/iu })).toBeDisabled(); }); it('disables next month button when max is in the current view month', () => { render(); expect(screen.getByRole('button', { name: /next month/iu })).toBeDisabled(); }); it('does not disable month buttons when min/max are in adjacent months', () => { render( , ); expect(screen.getByRole('button', { name: /previous month/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next month/iu })).toBeEnabled(); }); it('does not disable month buttons when no min/max is set', () => { render(); expect(screen.getByRole('button', { name: /previous month/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next month/iu })).toBeEnabled(); }); it('disables previous month button when min is in January and view is January', () => { // Edge case: month wraps to previous year render(); expect(screen.getByRole('button', { name: /previous month/iu })).toBeDisabled(); }); it('disables next month button when max is in December and view is December', () => { // Edge case: month wraps to next year render(); expect(screen.getByRole('button', { name: /next month/iu })).toBeDisabled(); }); }); describe('month view — previous/next year', () => { it('disables previous year button when min year equals view year', async () => { render(); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.click(screen.getByRole('button', { name: /2000, selected year/iu })); expect(screen.getByRole('button', { name: /previous year/iu })).toBeDisabled(); }); it('disables next year button when max year equals view year', async () => { render(); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.click(screen.getByRole('button', { name: /2000, selected year/iu })); expect(screen.getByRole('button', { name: /next year/iu })).toBeDisabled(); }); it('does not disable year buttons when min/max are in adjacent years', async () => { render( , ); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.click(screen.getByRole('button', { name: /2000, selected year/iu })); expect(screen.getByRole('button', { name: /previous year/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next year/iu })).toBeEnabled(); }); it('does not disable year buttons when no min/max is set', async () => { render(); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.click(screen.getByRole('button', { name: /2000, selected year/iu })); expect(screen.getByRole('button', { name: /previous year/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next year/iu })).toBeEnabled(); }); }); describe('year view — previous/next 20 years', () => { it('disables previous 20-year button when min falls in the current block', async () => { // value is 2000, block is 2000–2019, min is 2005 → previous block (1980–1999) is entirely before min render(); await user.click(screen.getByRole('button', { name: /year view/iu })); expect(screen.getByRole('button', { name: /previous 20 years/iu })).toBeDisabled(); }); it('disables next 20-year button when max falls in the current block', async () => { // value is 2000, block is 2000–2019, max is 2015 → next block (2020–2039) is entirely after max render(); await user.click(screen.getByRole('button', { name: /year view/iu })); expect(screen.getByRole('button', { name: /next 20 years/iu })).toBeDisabled(); }); it('does not disable 20-year buttons when min/max span multiple blocks', async () => { render( , ); await user.click(screen.getByRole('button', { name: /year view/iu })); expect(screen.getByRole('button', { name: /previous 20 years/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next 20 years/iu })).toBeEnabled(); }); it('does not disable 20-year buttons when no min/max is set', async () => { render(); await user.click(screen.getByRole('button', { name: /year view/iu })); expect(screen.getByRole('button', { name: /previous 20 years/iu })).toBeEnabled(); expect(screen.getByRole('button', { name: /next 20 years/iu })).toBeEnabled(); }); }); }); describe('view switching', () => { it('switches to year view and back to day view', async () => { render(); await user.click(screen.getByRole('button', { name: /year view/iu })); expect(screen.getByRole('button', { name: /selected year/iu })).toBeInTheDocument(); await user.click(screen.getByRole('button', { name: /selected year/iu })); expect(screen.getByRole('button', { name: /selected month/iu })).toBeInTheDocument(); await user.click(screen.getByRole('button', { name: /selected month/iu })); expect(screen.getByRole('button', { name: /next month/iu })).toBeInTheDocument(); }); it('switches to year view when header button is activated with keyboard', async () => { const handleChange = jest.fn(); render(); // Tab through to the year view header button const yearViewButton = screen.getByRole('button', { name: /year view/iu }); await user.tab(); while (document.activeElement !== yearViewButton) { await user.tab(); } expect(yearViewButton).toHaveFocus(); await user.keyboard('{Enter}'); expect(screen.getByRole('button', { name: /selected year/iu })).toBeInTheDocument(); expect(handleChange).not.toHaveBeenCalled(); }); }); describe('RTL keyboard navigation', () => { describe('in day view', () => { it.each([ ['{ArrowLeft}', +1], ['{ArrowRight}', -1], ['{ArrowUp}', -7], ['{ArrowDown}', +7], ])( "handles '%s' to step %d day(s) — horizontal arrows are reversed", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render( , ); await user.click(screen.getByRole('button', { name: /selected day/iu })); await user.keyboard(text); const expected = new Date(initialValue); expected.setDate(initialValue.getDate() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }, ); }); describe('in year view', () => { it.each([ ['{ArrowLeft}', +1], ['{ArrowRight}', -1], ['{ArrowUp}', -4], ['{ArrowDown}', +4], ])( "handles '%s' to step %d year(s) — horizontal arrows are reversed", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render( , ); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.keyboard(text); const expected = new Date(initialValue); expected.setFullYear(initialValue.getFullYear() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }, ); }); describe('in month view', () => { it.each([ ['{ArrowLeft}', +1], ['{ArrowRight}', -1], ['{ArrowUp}', -4], ['{ArrowDown}', +4], ])( "handles '%s' to step %d month(s) — horizontal arrows are reversed", async (text: string, step: number) => { const handleDateFocus = jest.fn(); render( , ); await user.click(screen.getByRole('button', { name: /year view/iu })); await user.keyboard(' '); await user.keyboard(text); const expected = new Date(initialValue); expected.setMonth(initialValue.getMonth() + step); expect(handleDateFocus).toHaveBeenCalledWith(expected, { isEnabled: true }); }, ); }); }); });