import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { expect, waitFor, within, userEvent } from 'storybook/test'; import Calendar from '../Calendar'; import type { DisabledRules } from '../Calendar.types'; import { forEachColumn, useRtlDiffI18n, withRTLDiff, } from '../../../.storybook/decorators/RtlDiffDecorator'; export default { component: Calendar, title: 'Forms/Calendar/Tests', tags: ['!autodocs', '!manifest'], } satisfies Meta; type Story = StoryObj; // min: March 2024, max: October 2024 const min = new Date(2024, 2, 1); const max = new Date(2024, 9, 31); /** * Year view: years within min/max are enabled, years outside are disabled (aria-disabled), * and both navigation arrows are disabled because min and max fall within the same 2020–2039 block. */ export const YearViewBoundaries: Story = { render: () => , play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: /year view/iu })); // Selected year (within range) is enabled await expect(canvas.getByRole('button', { name: /2024.*selected year/iu })).not.toHaveAttribute( 'aria-disabled', ); // Adjacent years outside range are disabled await expect(canvas.getByRole('button', { name: /^2023.*not available/iu })).toHaveAttribute( 'aria-disabled', 'true', ); await expect(canvas.getByRole('button', { name: /^2025.*not available/iu })).toHaveAttribute( 'aria-disabled', 'true', ); // Both nav arrows disabled — min and max are both in the 2020–2039 block await expect(canvas.getByRole('button', { name: /previous 20 years/iu })).toBeDisabled(); await expect(canvas.getByRole('button', { name: /next 20 years/iu })).toBeDisabled(); }, }; /** * Month view: months within min/max are enabled, months outside are disabled (aria-disabled), * and both navigation arrows are disabled because min and max are both in 2024. */ export const MonthViewBoundaries: Story = { render: () => , play: async ({ canvasElement }) => { const canvas = within(canvasElement); // Navigate: year view → click 2024 → month view await userEvent.click(canvas.getByRole('button', { name: /year view/iu })); await userEvent.click(canvas.getByRole('button', { name: /2024.*selected year/iu })); // Selected month (within range) is enabled await expect( canvas.getByRole('button', { name: /^Jun.*selected month/iu }), ).not.toHaveAttribute('aria-disabled'); // Boundary months (min=Mar, max=Oct) are enabled await expect( canvas.getByRole('button', { name: /^Mar(?!.*not available)/iu }), ).not.toHaveAttribute('aria-disabled'); await expect( canvas.getByRole('button', { name: /^Oct(?!.*not available)/iu }), ).not.toHaveAttribute('aria-disabled'); // Months outside range are disabled await expect(canvas.getByRole('button', { name: /^Feb.*not available/iu })).toHaveAttribute( 'aria-disabled', 'true', ); await expect(canvas.getByRole('button', { name: /^Nov.*not available/iu })).toHaveAttribute( 'aria-disabled', 'true', ); // Both nav arrows disabled — min and max are both in 2024 await expect(canvas.getByRole('button', { name: /previous year/iu })).toBeDisabled(); await expect(canvas.getByRole('button', { name: /next year/iu })).toBeDisabled(); }, }; const disabledRules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'We are closed on weekends.', }; /** * When a disabled cell receives focus via keyboard navigation, * the inline prompt updates to negative sentiment showing the disabled reason. */ export const DisabledDateFocusUpdatesPrompt: Story = { render: () => , play: async ({ canvasElement }) => { // Initially prompt is neutral (focused date is enabled — July 5 is a Friday) const prompt = canvasElement.querySelector('.wds-inline-prompt')!; await expect(prompt).toHaveClass('wds-prompt--neutral'); // Tab into the calendar grid (past month nav buttons), then arrow right to Saturday (disabled) await userEvent.tab(); await userEvent.tab(); await userEvent.tab(); await userEvent.keyboard('{ArrowRight}'); // Prompt should switch to negative after navigating to the disabled date await waitFor(async () => expect(prompt).toHaveClass('wds-prompt--negative')); }, }; const selectedDate = new Date(2024, 5, 15); const minDate = new Date(2024, 2, 1); const maxDate = new Date(2024, 9, 31); const disabledRulesRtl = { weekdays: ['sat', 'sun'] as ('sat' | 'sun')[], dates: [new Date(2024, 5, 10)], }; export const RTLDiff: Story = { render: () => { const i18n = useRtlDiffI18n(); return ( <>
); }, play: async ({ canvasElement }) => { await forEachColumn(canvasElement, async (column) => { const monthContainer = column.querySelector('[data-testid="month-view"]')!; const monthScope = within(monthContainer as HTMLElement); await userEvent.click(monthScope.getByRole('button', { name: /year view/iu })); await userEvent.click(monthScope.getByRole('button', { name: /2024.*selected year/iu })); const yearContainer = column.querySelector('[data-testid="year-view"]')!; const yearScope = within(yearContainer as HTMLElement); await userEvent.click(yearScope.getByRole('button', { name: /year view/iu })); }); }, ...withRTLDiff({ fixedRows: { heights: 'auto' }, i18n: { disabledReason: { en: 'Weekends and specific dates are unavailable.', ar: 'عطلات نهاية الأسبوع وتواريخ محددة غير متاحة.', }, }, }), }; const rtlDiffDayView = withRTLDiff({ i18n: { disabledReason: { en: 'Weekends and specific dates are unavailable.', ar: 'عطلات نهاية الأسبوع وتواريخ محددة غير متاحة.', }, }, }); /** * Day view only, for the RTL Layout docs page. Hidden from the sidebar, tests and Chromatic, * as `RTLDiff` already covers it. */ export const RTLDiffDayView: Story = { tags: ['!dev', '!test'], render: () => { const i18n = useRtlDiffI18n(); return ( ); }, ...rtlDiffDayView, parameters: { ...rtlDiffDayView.parameters, chromatic: { ...rtlDiffDayView.parameters.chromatic, disableSnapshot: true }, }, };