import type { Meta, StoryObj } from "@storybook/react-vite"; import { Formik } from "formik"; import * as React from "react"; import { action } from "storybook/actions"; import { expect, fn, userEvent, waitFor, within } from "storybook/test"; import { FORM_TEST_DECORATOR, PAGE_DECORATOR, } from "../../../../.storybook/decorators"; import { Button } from "../../button"; import { Textarea } from "./textarea"; const meta: Meta = { title: "Form/Textarea", component: Textarea, play: async ({ canvasElement, args }) => { const testValue = args.children || args.placeholder?.replace("e.g. ", "") || "Hello world!"; const canvas = within(canvasElement); const textarea = await canvas.getByRole("textbox"); const spy = fn(); textarea.addEventListener("change", spy); await userEvent.click(textarea, { delay: 10 }); await userEvent.clear(textarea); await userEvent.type(textarea, String(testValue)); // Test that event listener on the (custom) select gets called. await waitFor(testEventListener); async function testEventListener() { await expect(spy).toHaveBeenCalled(); } // Test that the FormData serialization returns the correct value. await waitFor(testFormDataSerialization, { timeout: String(testValue).length * 100, }); async function testFormDataSerialization() { const pre = await canvas.findByRole("log"); const data = JSON.parse(pre?.textContent || "{}"); await expect(data.textarea).toBe(testValue); } }, }; export default meta; type Story = StoryObj; const FORM_TEST_ARG_TYPES = { onChange: { action: "onChange" }, }; export const TextareaComponent: Story = { args: { name: "textarea", placeholder: "e.g. John Doe", }, argTypes: FORM_TEST_ARG_TYPES, decorators: [FORM_TEST_DECORATOR], }; export const Padding: Story = { args: { name: "textarea", placeholder: "e.g. John Doe", }, argTypes: FORM_TEST_ARG_TYPES, render: (args) => { return ( <>