// noinspection DuplicatedCode import type { StoryObj } from '@storybook/web-components'; import {expect, within, userEvent, fireEvent} from '@storybook/test'; import '../custom-element/custom-element.js'; import { ByIdWithinXsltFile, EmbeddingInAnotherFile, ExternalHtmlFile, ExternalHtmlFileInline, ExternalSvg, ExternalXsltFile, HtmlWithinHtmlFile, MathMLWithinHtmlFile, MissingIdWithinXsltFile, NoTag, SvgWithinHtmlFile, TemplateInPage } from './external-template.test.stories'; type TProps = { title: string; body:string}; type Story = StoryObj; function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } function render(args: TProps) { const {title, body} = args; return `
${ title } ${ body }
`; } const meta = { title: 'dom-merge' , render , parameters: { test:{ dangerouslyIgnoreUnhandledErrors: true}} }; export default meta; export const CharsCountInTextarea:Story = { args : {title: 'Chars count in textarea', body:`

Counter update happens on change event(focus change). The update should not interfere with the input


`} , play: async ({canvasElement}) => { const titleText = CharsCountInTextarea.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); await sleep(100); expect(await canvas.findByTestId('textarea-id')).toBeInTheDocument(); const textarea = canvas.getByTestId('textarea-id'); textarea.value =''; textarea.focus(); await userEvent.keyboard(titleText); expect(textarea.value).toEqual(titleText); expect(textarea.value.length).toEqual(titleText.length); canvas.getByTestId('refocus-id').focus(); await sleep(10); expect(canvas.getByTestId('counter-id').textContent).toEqual(''+titleText?.length,'counter of symbols'); }, }; export const WordCountOnType:Story = { args : {title: 'Word count in HTML input field', body:`

Counter update happens on keyup event. The update should not interfere with the input

txt slice:

{//slice/txt}
`} , play: async ({canvasElement}) => { const titleText = WordCountOnType.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); const input = await canvas.findByTestId('input-id'); input.value = ''; input.focus(); expect(input).toBeInTheDocument(); await userEvent.keyboard(titleText); await sleep(10); expect(input.value).toEqual(titleText); expect(canvas.getByTestId('chars-id').textContent.trim()).toEqual(''+titleText.length, 'counter of symbols'); expect(titleText.split(' ').length).toEqual(6, 'counter of words in text sample'); expect(canvas.getByTestId('words-id').textContent.trim()).toEqual('6', 'counter of words in render'); }, }; export const OrderPreservingOn2ndTransform:Story = { args : {title: 'Order preserving on 2nd transform', body:`

IF condition content should be displayed in place where it is defined (not shifted down on the parent children)


#1


#2

`} , play: async ({canvasElement}) => { const titleText = OrderPreservingOn2ndTransform.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); // userEvent breaks under FF in vitest, fireEvent works await fireEvent.click(await canvas.findByTestId('cb1')); await expect(await canvas.findByText('#1')).toBeInTheDocument(); await fireEvent.click(canvas.getByTestId('cb2')); await expect(await canvas.findByText('#2')).toBeInTheDocument(); await expect(canvas.getByTestId("beforeC1").nextElementSibling.textContent).toEqual('#1') }, }; export const ReadSystemValidityMessage:Story = { args : {title: 'read system validity message', body:`

validationMessage propagated into slice as 'validation-message' attribute

  1. type in input field
  2. delete input field content
  3. observe the warning in string after input
  4. Click Next observe the system warning in dropdown over input
`} , play: async ({canvasElement}) => { const titleText = ReadSystemValidityMessage.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); await userEvent.type(canvas.getByTestId('inp1'),'Hi'); await userEvent.clear(canvas.getByTestId('inp1')); await userEvent.click(canvas.getByTestId('btn1')); await expect(await canvas.findByTestId('var1')).toBeInTheDocument(); await expect(canvas.getByTestId("var1").textContent).toEqual(canvas.getByTestId("inp1").validationMessage); await expect(canvas.getByTestId("var1").textContent.length>1).toEqual(true); }, }; export const EmbedDCE:Story = { args : {title: 'Render inner components', body:` icon: `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await expect(await canvas.findByText('👍')).toBeInTheDocument(); }, }; //#region unit tests /* istanbul ignore else -- @preserve */ if( 'test' === import.meta.env.MODE && !import.meta.url.includes('skiptest') ) { const mod = await import('./dom-merge.test.stories.ts?skiptest'); const { testStoryBook } = await import('./testStoryBook') const { describe } = await import('vitest') describe(meta.title, () => testStoryBook( mod, meta ) ); } //#endregion