|
1 | 1 | import type { Meta, StoryObj } from '@storybook/nextjs-vite'; |
2 | 2 | import { useState } from 'react'; |
3 | 3 | import { expect, screen, userEvent, waitFor } from 'storybook/test'; |
| 4 | +import Field from '~/lib/form/components/Field/Field'; |
4 | 5 | import CheckboxGroupField from '~/lib/form/components/fields/CheckboxGroup'; |
| 6 | +import useFormStore from '~/lib/form/hooks/useFormStore'; |
| 7 | +import FormStoreProvider from '~/lib/form/store/formStoreProvider'; |
5 | 8 |
|
6 | 9 | function ControlledCheckboxGroup() { |
7 | 10 | const [values, setValues] = useState<(string | number)[]>(['a', 'b', 'c']); |
@@ -52,6 +55,58 @@ export const ClickCheckbox: Story = { |
52 | 55 | }, |
53 | 56 | }; |
54 | 57 |
|
| 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 | + |
55 | 110 | export const ClickLabel: Story = { |
56 | 111 | play: async () => { |
57 | 112 | // Find the text label and click it instead of the checkbox button |
|
0 commit comments