import { expect, userEvent, waitFor, within } from "storybook/test"; import type { Meta, StoryObj } from "@storybook/react-vite"; import { type ReactNode, useState } from "react"; import { ConfirmationMessage } from "../feedback/Message"; import { InputText } from "../inputs/InputText"; import { View } from "../primitives/View"; import { Story } from "../story-components/Story"; import { Form, FormValidationError } from "./Form"; import { FormField } from "./FormField"; import { FormSubmitButton } from "./FormSubmitButton"; interface NewsletterValues { email: string; } function delay(ms: number): Promise { return new Promise((resolve) => { setTimeout(resolve, ms); }); } function errorToMessage(error: unknown): string { if (error instanceof FormValidationError) { return "Please fix the errors above."; } return error instanceof Error ? error.message : "Something went wrong."; } function FormSubmitButtonDemo(): ReactNode { const [submitted, setSubmitted] = useState(false); return ( <> defaultValues={{ email: "" }} render={({ control, submit }) => ( ( )} /> )} onSubmit={async () => { await delay(300); setSubmitted(true); }} /> {submitted ? ( Subscribed. ) : null} ); } type ThisStory = StoryObj; export default { title: "alouette/Forms/FormSubmitButton", component: FormSubmitButton, parameters: { componentSubtitle: "Submit button for a
. Reuses Button's own disabled/loading accessibility handling — only wires isSubmitting from react-hook-form's context to it.", }, } satisfies Meta; export const FormSubmitButtonStory: ThisStory = { name: "FormSubmitButton", render: () => ( ), play: async ({ canvasElement }) => { const canvas = within(canvasElement); const emailInput = canvas.getByLabelText("Email"); const submitButton = canvas.getByRole("button", { name: "Subscribe" }); await userEvent.type(emailInput, "ada@example.com"); await userEvent.click(submitButton); await waitFor(async () => { await expect(canvas.getByText("Subscribe")).toHaveAttribute( "aria-disabled", "true", ); }); await waitFor( async () => { await expect(canvas.getByText("Subscribed.")).toBeVisible(); }, { timeout: 2000 }, ); }, };