import { zodResolver } from '@hookform/resolvers/zod'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { expect, userEvent, within } from 'storybook/test'; import LoginForm from './LoginForm'; import { loginSchema, type LoginSchema } from '../schemas'; const loginFormDescription = `Renders the real \`LoginForm\` component wired to the real \`loginSchema\` (zod) through the same react-hook-form setup used on the login page, so these stories validate actual client-side validation behavior rather than a re-implemented form.`; const LoginFormHarness = () => { const [submitted, setSubmitted] = useState(null); const form = useForm({ resolver: zodResolver(loginSchema) }); return (
setSubmitted(values))} formState={form.formState} register={form.register} submitting={false} /> {submitted ? (

Signed in as {submitted.username}

) : null}
); }; const meta = { title: 'Forms/LoginForm', component: LoginFormHarness, tags: ['autodocs'], parameters: { layout: 'centered', docs: { description: { component: loginFormDescription, }, }, }, } satisfies Meta; export default meta; type Story = StoryObj; export const Empty: Story = {}; export const RejectsEmptySubmission: Story = { play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: /sign in/i })); await expect(canvas.getByText(/username is required/i)).toBeInTheDocument(); await expect(canvas.getByText(/password is required/i)).toBeInTheDocument(); await expect(canvas.queryByTestId('submit-result')).not.toBeInTheDocument(); }, }; export const AcceptsValidLogin: Story = { play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByLabelText(/username/i), 'hans'); await userEvent.type(canvas.getByLabelText(/password/i), 'correct-horse-battery'); await userEvent.click(canvas.getByRole('button', { name: /sign in/i })); await expect(canvas.getByTestId('submit-result')).toHaveTextContent('hans'); }, };