-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.stories.tsx
More file actions
165 lines (148 loc) · 6.01 KB
/
checkbox.stories.tsx
File metadata and controls
165 lines (148 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { zodResolver } from '@hookform/resolvers/zod';
import { Checkbox } from '@lambdacurry/forms/remix-hook-form/checkbox';
import { Button } from '@lambdacurry/forms/ui/button';
import type { ActionFunctionArgs } from 'react-router';
import { useFetcher } from 'react-router-dom';
import type { Meta, StoryContext, StoryObj } from '@storybook/react';
import { expect, userEvent } from '@storybook/test';
import type {} from '@testing-library/dom';
import { RemixFormProvider, getValidatedFormData, useRemixForm } from 'remix-hook-form';
import { z } from 'zod';
import { withRemixStubDecorator } from '../lib/storybook/remix-stub';
const formSchema = z.object({
terms: z.boolean().refine((val) => val === true, 'You must accept the terms and conditions'),
marketing: z.boolean().optional(),
required: z.boolean().refine((val) => val === true, 'This field is required'),
});
type FormData = z.infer<typeof formSchema>;
const ControlledCheckboxExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
terms: false as true, // Note: ZOD Schema expects a true value
marketing: false,
required: false as true, //Note: ZOD Schema expects a true value
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className="grid gap-4">
<Checkbox className="rounded-md border p-4" name="terms" label="Accept terms and conditions" />
<Checkbox
className="rounded-md border p-4"
name="marketing"
label="Receive marketing emails"
description="We will send you hourly updates about our products"
/>
<Checkbox className="rounded-md border p-4" name="required" label="This is a required checkbox" />
</div>
<Button type="submit" className="mt-4">
Submit
</Button>
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
</fetcher.Form>
</RemixFormProvider>
);
};
const handleFormSubmission = async (request: Request) => {
const { errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
return { message: 'Form submitted successfully' };
};
const meta: Meta<typeof Checkbox> = {
title: 'RemixHookForm/Checkbox',
component: Checkbox,
parameters: { layout: 'centered' },
tags: ['autodocs'],
decorators: [
withRemixStubDecorator({
root: {
Component: ControlledCheckboxExample,
action: async ({ request }: ActionFunctionArgs) => handleFormSubmission(request),
},
}),
],
} satisfies Meta<typeof Checkbox>;
export default meta;
type Story = StoryObj<typeof meta>;
const testDefaultValues = ({ canvas }: StoryContext) => {
const termsCheckbox = canvas.getByLabelText('Accept terms and conditions');
const marketingCheckbox = canvas.getByLabelText('Receive marketing emails');
const requiredCheckbox = canvas.getByLabelText('This is a required checkbox');
expect(termsCheckbox).not.toBeChecked();
expect(marketingCheckbox).not.toBeChecked();
expect(requiredCheckbox).not.toBeChecked();
};
const testInvalidSubmission = async ({ canvas }: StoryContext) => {
const submitButton = canvas.getByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
await expect(await canvas.findByText('You must accept the terms and conditions')).toBeInTheDocument();
await expect(await canvas.findByText('This field is required')).toBeInTheDocument();
};
const testValidSubmission = async ({ canvas }: StoryContext) => {
const termsCheckbox = canvas.getByLabelText('Accept terms and conditions');
const requiredCheckbox = canvas.getByLabelText('This is a required checkbox');
await userEvent.click(termsCheckbox);
await userEvent.click(requiredCheckbox);
const submitButton = canvas.getByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
await expect(await canvas.findByText('Form submitted successfully')).toBeInTheDocument();
};
export const Tests: Story = {
parameters: {
docs: {
description: {
story: 'The default checkbox component.',
},
source: {
code: `
const formSchema = z.object({
terms: z.boolean().optional().refine(val => val === true, 'You must accept the terms and conditions'),
marketing: z.boolean().optional(),
required: z.boolean().optional().refine(val => val === true, 'This field is required'),
});
const ControlledCheckboxExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
terms: false as true, // Note: ZOD Schema expects a true value
marketing: false,
required: false as true //Note: ZOD Schema expects a true value
},
fetcher,
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit} method="post" action="/">
<div className='grid gap-4'>
<Checkbox className='rounded-md border p-4' name="terms" label="Accept terms and conditions" />
<Checkbox className='rounded-md border p-4' name="marketing" label="Receive marketing emails" description="We will send you hourly updates about our products" />
<Checkbox className='rounded-md border p-4' name="required" label="This is a required checkbox" />
</div>
<Button type="submit" className="mt-4">
Submit
</Button>
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
</fetcher.Form>
</RemixFormProvider>
);
};`,
},
},
},
play: async (storyContext) => {
testDefaultValues(storyContext);
await testInvalidSubmission(storyContext);
await testValidSubmission(storyContext);
},
};