import { mockMatchMedia, mockResizeObserver, render, screen, userEvent } from '../test-utils'; import DateLookup from './DateLookup'; import type { DisabledRules } from '../Calendar/Calendar.types'; import { act } from 'react'; const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTimeAsync }); mockMatchMedia(); mockResizeObserver(); const initialValue = new Date(2024, 6, 1); const weekendRules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'We are closed on weekends.', }; const bankHolidayRules: DisabledRules = { dates: [new Date(2024, 6, 4)], reason: 'Bank holiday.', }; describe('DateLookup - disabled dates', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(async () => { await jest.runOnlyPendingTimersAsync(); jest.useRealTimers(); jest.clearAllMocks(); }); const openDateLookup = async () => { await user.click(screen.getByRole('button')); await act(async () => { await jest.runOnlyPendingTimersAsync(); }); }; describe('panel behavior with rule-disabled dates', () => { it('does NOT close when a rule-disabled date is clicked', async () => { render( , ); await openDateLookup(); const holiday = screen.getByLabelText(/^4 July 2024/u); await user.click(holiday); expect(screen.getByRole('dialog')).toBeInTheDocument(); }); it('closes when an enabled date is clicked', async () => { const handleChange = jest.fn(); render( , ); await openDateLookup(); const enabledDate = screen.getByLabelText(/^3 July 2024/u); await user.click(enabledDate); await act(async () => { await jest.runOnlyPendingTimersAsync(); }); expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); expect(handleChange).toHaveBeenCalledWith(new Date(2024, 6, 3)); }); }); describe('panel behavior with out-of-range dates', () => { const minDate = new Date(2024, 6, 3); const maxDate = new Date(2024, 6, 25); it('does NOT close when an out-of-range date is clicked', async () => { render( , ); await openDateLookup(); await user.click(screen.getByRole('button', { name: /^1 July 2024/u })); expect(screen.getByRole('dialog')).toBeInTheDocument(); }); it('does NOT call onChange when an out-of-range date is clicked', async () => { const handleChange = jest.fn(); render( , ); await openDateLookup(); await user.click(screen.getByRole('button', { name: /^1 July 2024/u })); expect(handleChange).not.toHaveBeenCalled(); }); }); describe('prop threading', () => { it('renders rule-disabled dates with aria-disabled in the calendar', async () => { render(); await openDateLookup(); const saturday = screen.getByLabelText(/^6 July 2024/u); expect(saturday).toHaveAttribute('aria-disabled', 'true'); }); it('renders out-of-range dates with aria-disabled in the calendar', async () => { render( , ); await openDateLookup(); const outOfRangeDate = screen.getByRole('button', { name: /^1 July 2024/u }); expect(outOfRangeDate).toHaveAttribute('aria-disabled', 'true'); expect(outOfRangeDate).toBeEnabled(); }); it('renders InlinePrompt with disabledRules reason text', async () => { render(); await openDateLookup(); expect(screen.getByText('We are closed on weekends.')).toBeInTheDocument(); }); it('renders previous month button as disabled when min is in the current month', async () => { render( , ); await openDateLookup(); expect(screen.getByRole('button', { name: /previous month/iu })).toBeDisabled(); }); it('renders next month button as disabled when max is in the current month', async () => { render( , ); await openDateLookup(); expect(screen.getByRole('button', { name: /next month/iu })).toBeDisabled(); }); }); describe('InlinePrompt for out-of-range dates', () => { const minDate = new Date(2024, 6, 3); const maxDate = new Date(2024, 6, 25); it('shows negative prompt with boundary dates when out-of-range date is clicked', async () => { render( , ); await openDateLookup(); await user.click(screen.getByRole('button', { name: /^1 July 2024/u })); // The prompt renders inside a portal so query from document const prompt = document.querySelector('.wds-inline-prompt'); expect(prompt).toBeInTheDocument(); expect(prompt).toHaveClass('wds-prompt--negative'); expect(prompt).toHaveTextContent( 'Please select a date between 3 July 2024 and 25 July 2024.', ); }); it('shows boundary message (not disabledRules reason) when out-of-range date is clicked', async () => { render( , ); await openDateLookup(); // July 27 is beyond max await user.click(screen.getByRole('button', { name: /^27 July 2024/u })); const prompt = document.querySelector('.wds-inline-prompt'); expect(prompt).toHaveTextContent( 'Please select a date between 3 July 2024 and 25 July 2024.', ); expect(prompt).not.toHaveTextContent('We are closed on weekends.'); }); }); describe('onDateSelect', () => { it('fires with isEnabled: false when navigating to a rule-disabled date', async () => { const handleDateSelect = jest.fn(); render( , ); await openDateLookup(); await user.keyboard('{ArrowRight}'); expect(handleDateSelect).toHaveBeenCalledWith( expect.objectContaining({ date: new Date(2024, 6, 6), isEnabled: false, }), ); }); it('fires with isEnabled: false when clicking an out-of-range date', async () => { const handleDateSelect = jest.fn(); render( , ); await openDateLookup(); await user.click(screen.getByRole('button', { name: /^1 July 2024/u })); expect(handleDateSelect).toHaveBeenCalledWith( expect.objectContaining({ date: new Date(2024, 6, 1), isEnabled: false, }), ); }); it('fires with isEnabled: true when clicking an enabled date', async () => { const handleDateSelect = jest.fn(); render( , ); await openDateLookup(); await user.click(screen.getByLabelText(/^3 July 2024/u)); expect(handleDateSelect).toHaveBeenCalledWith( expect.objectContaining({ date: new Date(2024, 6, 3), isEnabled: true, }), ); }); it('fires exactly once when an enabled date is clicked', async () => { const handleDateSelect = jest.fn(); render( , ); await openDateLookup(); handleDateSelect.mockClear(); await user.click(screen.getByLabelText(/^3 July 2024/u)); expect(handleDateSelect).toHaveBeenCalledTimes(1); }); }); });