// noinspection DuplicatedCode import type { StoryObj } from '@storybook/web-components'; import {expect, within, userEvent} from '@storybook/test'; import '../custom-element/custom-element.js'; type TProps = { title: string; body:string}; type Story = StoryObj; function render(args: TProps) { const {title, body} = args; return `
${ title } ${ body }
`; } const meta = { title: 'form' , render }; export default meta; export const SystemMessage:Story = { args : {title: 'custom-validity boolean', body:`

type and then clear test in input should lead to system validation message shown next to input field. Something like Please fill out this field.

`} , play: async ({canvasElement}) => { const titleText = SystemMessage.args!.title as string; const canvas = within(canvasElement); await expect( canvas.queryByTestId('validation-msg')).toBeNull(); const input = await canvas.findByTestId('input-1'); input.focus(); await userEvent.keyboard('abc'); await userEvent.clear(input); await expect( canvas.queryByTestId('validation-msg')).toBeInTheDocument(); }, }; export const FormData:Story = { args : {title: 'form-data populated', body:`

type and then clear test in input should lead to system validation message shown next to input field. Something like Please fill out this field.

`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByTestId('input-1'), 'ABC'); await userEvent.click(canvas.getByRole('button')); await expect( await canvas.findByText('ABC')).toBeInTheDocument(); await userEvent.type(canvas.getByTestId('input-2'), 'DCE'); await userEvent.click(canvas.getByRole('button')); await expect( await canvas.findByText('DCE')).toBeInTheDocument(); await userEvent.type(canvas.getByTestId('input-3'), 'XYZ'); canvas.getByTestId('input-1').focus(); await expect( await canvas.findByText('XYZ')).toBeInTheDocument(); }, }; export const SetValidityMessage:Story = { args : {title: 'setCustomValidity', body:`

setCustomValidity() invoked by custom-validity attribute. Type in the input fiels to observe the chars count in text, click "next" and observe same message in dropdown.

`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByTestId('input-1'), 'ABC'); await expect( canvas.getByText('ABC')).toBeInTheDocument(); await expect( canvas.getByText('char count 3')).toBeInTheDocument(); }, }; export const FormCustomValidityBoolean:Story = { args : {title: 'form custom-validity, boolean', body:`

Form is valid only when input text length longer of 3 characters.

`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByTestId('input-1'), 'AB'); await userEvent.click(canvas.getByRole('button')); await expect( canvas.getByText('AB')).toBeInTheDocument(); // i.e. not reloaded by form submit }, }; export const FormCustomValidityString:Story = { args : {title: 'form custom-validity, string', body:`

Form is valid only when input text is longer of 3 characters, @validation-message propagated in form slot

`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByTestId('input-1'), 'AB'); await userEvent.click(canvas.getByRole('button')); await expect( canvas.getByText('AB')).toBeInTheDocument(); // i.e. not reloaded by form submit await expect( canvas.getByText('should be more than 3 characters, now is 2')).toBeInTheDocument(); }, }; // custom validity rules on form : boolean and string values //#region unit tests /* istanbul ignore else -- @preserve */ if( 'test' === import.meta.env.MODE && !import.meta.url.includes('skiptest') ) { const mod = await import('./form.test.stories.ts?skiptest'); const { testStoryBook } = await import('./testStoryBook') const { describe } = await import('vitest') describe(meta.title, () => testStoryBook( mod, meta ) ); } //#endregion