11import { zodResolver } from '@hookform/resolvers/zod' ;
22import { Textarea } from '@lambdacurry/forms/remix-hook-form/textarea' ;
33import { Button } from '@lambdacurry/forms/ui/button' ;
4- import type { Meta , StoryObj } from '@storybook/react-vite' ;
4+ import type { Meta , StoryContext , StoryObj } from '@storybook/react-vite' ;
55import { expect , userEvent , within } from '@storybook/test' ;
66import { type ActionFunctionArgs , useFetcher } from 'react-router' ;
77import { RemixFormProvider , createFormData , getValidatedFormData , useRemixForm } from 'remix-hook-form' ;
@@ -49,6 +49,7 @@ const ControlledTextareaExample = () => {
4949 < Button type = "submit" className = "mt-4" >
5050 Submit
5151 </ Button >
52+ { fetcher . data ?. message && < p className = "mt-2 text-green-600" > { fetcher . data . message } </ p > }
5253 { fetcher . data ?. submittedMessage && (
5354 < div className = "mt-4" >
5455 < p className = "text-sm font-medium" > Submitted message:</ p >
@@ -92,6 +93,41 @@ const meta: Meta<typeof Textarea> = {
9293export default meta ;
9394type Story = StoryObj < typeof meta > ;
9495
96+ // Test scenarios
97+ const testInvalidSubmission = async ( { canvas } : StoryContext ) => {
98+ const messageInput = canvas . getByLabelText ( 'Your message' ) ;
99+ const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
100+
101+ // Clear the textarea and enter text that's too short
102+ await userEvent . click ( messageInput ) ;
103+ await userEvent . clear ( messageInput ) ;
104+ await userEvent . type ( messageInput , 'Short' ) ;
105+ await userEvent . click ( submitButton ) ;
106+
107+ // Check for validation error
108+ await expect ( await canvas . findByText ( 'Message must be at least 10 characters' ) ) . toBeInTheDocument ( ) ;
109+ } ;
110+
111+ const testValidSubmission = async ( { canvas } : StoryContext ) => {
112+ const messageInput = canvas . getByLabelText ( 'Your message' ) ;
113+ const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
114+
115+ // Clear and enter valid text
116+ await userEvent . click ( messageInput ) ;
117+ await userEvent . clear ( messageInput ) ;
118+ await userEvent . type ( messageInput , 'This is a test message that is longer than 10 characters.' ) ;
119+ await userEvent . click ( submitButton ) ;
120+
121+ // Check for success message
122+ const successMessage = await canvas . findByText ( 'Message submitted successfully' ) ;
123+ expect ( successMessage ) . toBeInTheDocument ( ) ;
124+
125+ // Check if the submitted message is displayed
126+ await expect (
127+ await canvas . findByText ( 'This is a test message that is longer than 10 characters.' ) ,
128+ ) . toBeInTheDocument ( ) ;
129+ } ;
130+
95131export const Default : Story = {
96132 parameters : {
97133 docs : {
@@ -100,20 +136,8 @@ export const Default: Story = {
100136 } ,
101137 } ,
102138 } ,
103- play : async ( { canvasElement } ) => {
104- const canvas = within ( canvasElement ) ;
105-
106- // Enter text
107- const messageInput = canvas . getByLabelText ( 'Your message' ) ;
108- await userEvent . type ( messageInput , 'This is a test message that is longer than 10 characters.' ) ;
109-
110- // Submit the form
111- const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
112- await userEvent . click ( submitButton ) ;
113-
114- // Check if the submitted message is displayed
115- await expect (
116- await canvas . findByText ( 'This is a test message that is longer than 10 characters.' ) ,
117- ) . toBeInTheDocument ( ) ;
139+ play : async ( storyContext ) => {
140+ await testInvalidSubmission ( storyContext ) ;
141+ await testValidSubmission ( storyContext ) ;
118142 } ,
119143} ;
0 commit comments