Skip to content

Commit 82a18ad

Browse files
committed
test: add FormField-wrapped checkbox interaction test to reproduction
1 parent 8df7f01 commit 82a18ad

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lib/form/components/fields/CheckboxGroupInteraction.stories.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { Meta, StoryObj } from '@storybook/nextjs-vite';
22
import { useState } from 'react';
33
import { expect, screen, userEvent, waitFor } from 'storybook/test';
4+
import Field from '~/lib/form/components/Field/Field';
45
import CheckboxGroupField from '~/lib/form/components/fields/CheckboxGroup';
6+
import useFormStore from '~/lib/form/hooks/useFormStore';
7+
import FormStoreProvider from '~/lib/form/store/formStoreProvider';
58

69
function ControlledCheckboxGroup() {
710
const [values, setValues] = useState<(string | number)[]>(['a', 'b', 'c']);
@@ -52,6 +55,58 @@ export const ClickCheckbox: Story = {
5255
},
5356
};
5457

58+
function FormFieldCheckboxGroup() {
59+
return (
60+
<FormStoreProvider>
61+
<FormFieldInner />
62+
</FormStoreProvider>
63+
);
64+
}
65+
66+
function FormFieldInner() {
67+
const getFormValues = useFormStore((s) => s.getFormValues);
68+
const values = getFormValues();
69+
70+
return (
71+
<div>
72+
<Field
73+
name="choices"
74+
label="Pick options"
75+
component={CheckboxGroupField}
76+
options={[
77+
{ value: 'a', label: 'Option A' },
78+
{ value: 'b', label: 'Option B' },
79+
{ value: 'c', label: 'Option C' },
80+
]}
81+
initialValue={['a', 'b', 'c']}
82+
/>
83+
<div data-testid="form-result">
84+
{JSON.stringify(values.choices ?? 'undefined')}
85+
</div>
86+
</div>
87+
);
88+
}
89+
90+
export const ClickCheckboxInFormField: Story = {
91+
render: () => <FormFieldCheckboxGroup />,
92+
play: async () => {
93+
const checkboxC = await screen.findByRole('checkbox', { name: 'Option C' });
94+
await expect(checkboxC).toHaveAttribute('aria-checked', 'true');
95+
96+
await userEvent.click(checkboxC);
97+
98+
await waitFor(async () => {
99+
await expect(checkboxC).toHaveAttribute('aria-checked', 'false');
100+
});
101+
102+
await waitFor(async () => {
103+
await expect(screen.getByTestId('form-result')).toHaveTextContent(
104+
'["a","b"]',
105+
);
106+
});
107+
},
108+
};
109+
55110
export const ClickLabel: Story = {
56111
play: async () => {
57112
// Find the text label and click it instead of the checkbox button

0 commit comments

Comments
 (0)