import type { Meta, StoryObj } from "@storybook/react-vite"; import React from "react"; import { expect, userEvent, waitFor } from "storybook/test"; import { DurationInput } from "./durationinput"; function Controlled({ initial = "", ...props }: React.ComponentProps & { initial?: string }) { const [val, setVal] = React.useState(initial); return (
setVal(e.target.value)} />
{JSON.stringify({ value: val }, null, 2)}
); } const findBySection = (root: HTMLElement, section: string) => root.querySelector( `[data-section="${section}"]`, ) as HTMLInputElement; const getLog = (root: HTMLElement) => root.querySelector('[role="log"]'); const meta: Meta = { title: "Form/DurationInput", component: DurationInput, args: { mode: "designator", label: "Duration", name: "duration", disabled: false, required: false, }, }; export default meta; type Story = StoryObj; export const Designator: Story = { args: { mode: "designator" }, render: (args) => , play: async ({ canvasElement }) => { const years = findBySection(canvasElement, "YY"); const months = findBySection(canvasElement, "MM"); const days = findBySection(canvasElement, "DD"); const log = getLog(canvasElement); await userEvent.clear(years); await userEvent.type(years, "4", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P4Y")); await userEvent.clear(months); await userEvent.type(months, "2", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P4Y2M")); await userEvent.clear(days); await userEvent.type(days, "3", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P4Y2M3D")); await userEvent.clear(years); await waitFor(() => expect(log).toHaveTextContent("P2M3D")); await userEvent.clear(months); await waitFor(() => expect(log).toHaveTextContent("P3D")); await userEvent.clear(days); await waitFor(() => expect(log).toHaveTextContent("PT0S")); }, }; export const DesignatorWithWeeks: Story = { args: { mode: "designator" }, render: (args) => , play: async ({ canvasElement }) => { const years = findBySection(canvasElement, "YY"); const months = findBySection(canvasElement, "MM"); const days = findBySection(canvasElement, "DD"); const log = getLog(canvasElement); await waitFor(() => expect(log).toHaveTextContent("P56D")); await userEvent.clear(years); await userEvent.type(years, "1", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P1Y56D")); await userEvent.clear(months); await userEvent.type(months, "1", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P1Y1M56D")); await userEvent.clear(days); await userEvent.type(days, "1", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P1Y1M1D")); await userEvent.clear(years); await waitFor(() => expect(log).toHaveTextContent("P1M1D")); await userEvent.clear(months); await waitFor(() => expect(log).toHaveTextContent("P1D")); await userEvent.clear(days); await waitFor(() => expect(log).toHaveTextContent("PT0S")); }, }; export const Weeks: Story = { args: { mode: "weeks" }, render: (args) => , play: async ({ canvasElement }) => { const weeks = findBySection(canvasElement, "WW"); const log = getLog(canvasElement); await expect(log).toHaveTextContent("P2W"); await userEvent.clear(weeks); await userEvent.type(weeks, "1", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("P1W")); await userEvent.type(weeks, "2", { delay: 5 }); // "12" await waitFor(() => expect(log).toHaveTextContent("P12W")); await userEvent.clear(weeks); await userEvent.type(weeks, "0", { delay: 5 }); await waitFor(() => expect(log).toHaveTextContent("PT0S")); }, }; export const Disabled: Story = { args: { mode: "designator", disabled: true }, render: (args) => , play: async ({ canvasElement }) => { const years = findBySection(canvasElement, "YY"); const months = findBySection(canvasElement, "MM"); const days = findBySection(canvasElement, "DD"); const log = getLog(canvasElement); await expect(years).toBeDisabled(); await expect(months).toBeDisabled(); await expect(days).toBeDisabled(); await expect(log).toHaveTextContent("P10D"); }, };