import { Meta, StoryObj } from '@storybook/react-webpack5'; import { userEvent, within, fn } from 'storybook/test'; import { DateInput, Field } from '..'; import { lorem10 } from '../test-utils'; import Provider from '../provider/Provider'; import translations from '../i18n'; import { withVariantConfig } from '../../.storybook/helpers'; const meta: Meta = { component: DateInput, title: 'Forms/DateInput/Tests', tags: ['!manifest'], args: { dayLabel: 'Day input', dayAutoComplete: 'bday-day', monthLabel: 'Month select', yearLabel: 'Year input', yearAutoComplete: 'bday-year', onChange: fn(), }, }; export default meta; type Story = StoryObj; const Basic: Story = { args: { onChange: fn(), }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('combobox')); }, }; export const WithLabel: Story = { args: { onChange: fn(), }, render: (args) => { return ( ); }, }; export const InputError: Story = { render: (args) => ( <> pure date input:
show padding right below
date input wrapped in Field:
show padding right below
error date input wrapped in Field:
show padding right below
), }; export const CustomPlaceholders: Story = { ...Basic, args: { placeholders: { day: 'D', month: 'M', year: 'YY', }, }, }; export const SingleZero: Story = { ...Basic, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step('can enter leading zero on day field', async () => { const dayInput = await canvas.findByRole('textbox', { name: /day/i }); await userEvent.click(dayInput); await userEvent.type(dayInput, '0'); }); await step('can enter 1 leading zero on year field', async () => { const yearInput = await canvas.findByRole('textbox', { name: /year/i }); await userEvent.click(yearInput); await userEvent.type(yearInput, '0'); }); }, }; export const ZeroesAsValue: Story = { ...Basic, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step('can enter 2 leading zeroes on day field', async () => { const dayInput = await canvas.findByRole('textbox', { name: /day/i }); await userEvent.click(dayInput); await userEvent.type(dayInput, '00'); }); await step('can enter 4 leading zeroes on year field', async () => { const yearInput = await canvas.findByRole('textbox', { name: /year/i }); await userEvent.click(yearInput); await userEvent.type(yearInput, '0000'); }); }, }; export const ZeroesBeforeValue: Story = { ...Basic, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step('can enter leading zeroes on day followed by day', async () => { const dayInput = await canvas.findByRole('textbox', { name: /day/i }); await userEvent.click(dayInput); await userEvent.type(dayInput, '01'); }); await step('can enter leading zeroes on year followed by year', async () => { const yearInput = await canvas.findByRole('textbox', { name: /year/i }); await userEvent.click(yearInput); await userEvent.type(yearInput, '0001'); }); }, }; export const MaxLengthRespected: Story = { ...Basic, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step('can only enter 4 digits in the year field', async () => { const yearInput = await canvas.findByRole('textbox', { name: /year/i }); await userEvent.click(yearInput); await userEvent.type(yearInput, '11111111'); }); await step('can only enter 2 digits in the day field', async () => { const dayInput = await canvas.findByRole('textbox', { name: /day/i }); await userEvent.click(dayInput); await userEvent.type(dayInput, '11111111'); }); }, }; export const LocalizationWorks: Story = { ...Basic, decorators: [ (Story) => { const locale = 'zh-HK'; return ( ); }, ], }; export const YearFirst: Story = { ...Basic, decorators: [ (Story) => { const locale = 'ja'; return ( ); }, ], }; export const MonthFirst: Story = { ...Basic, decorators: [ (Story) => { const locale = 'en-US'; return ( ); }, ], }; export const DayFirst: Story = { ...Basic, decorators: [ (Story) => { const locale = 'en'; return ( ); }, ], }; export const NoNonDigitsAllowed: Story = { ...Basic, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step('can only enter digits in the day field', async () => { const dayInput = await canvas.findByRole('textbox', { name: /day/i }); await userEvent.click(dayInput); await userEvent.type(dayInput, 'abcd1'); }); await step('can only enter digits in the year field', async () => { const yearInput = await canvas.findByRole('textbox', { name: /year/i }); await userEvent.click(yearInput); await userEvent.type(yearInput, 'abcd2'); }); }, }; export const BasicMobile: Story = { ...Basic, ...withVariantConfig(['mobile'], Basic), }; export const InputErrorMobile: Story = { ...InputError, ...withVariantConfig(['mobile'], InputError), };