|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import { axe } from 'vitest-axe'; |
| 3 | +import { z } from 'zod'; |
| 4 | + |
| 5 | +import { render, screen, setupUser } from '@/tests/utils'; |
| 6 | + |
| 7 | +import { FormField, FormFieldController } from '..'; |
| 8 | +import { FormMocked } from '../form-test-utils'; |
| 9 | + |
| 10 | +const zFormSchema = () => |
| 11 | + z.object({ |
| 12 | + lovesBears: z.boolean().refine((val) => val === true, { |
| 13 | + message: 'Please say you love bears.', |
| 14 | + }), |
| 15 | + }); |
| 16 | + |
| 17 | +test('should have no a11y violations', async () => { |
| 18 | + const mockedSubmit = vi.fn(); |
| 19 | + |
| 20 | + HTMLCanvasElement.prototype.getContext = vi.fn(); |
| 21 | + |
| 22 | + const { container } = render( |
| 23 | + <FormMocked |
| 24 | + schema={zFormSchema()} |
| 25 | + useFormOptions={{ defaultValues: { lovesBears: false } }} |
| 26 | + onSubmit={mockedSubmit} |
| 27 | + > |
| 28 | + {({ form }) => ( |
| 29 | + <FormField> |
| 30 | + <FormFieldController |
| 31 | + type="checkbox" |
| 32 | + control={form.control} |
| 33 | + name="lovesBears" |
| 34 | + > |
| 35 | + I love bears |
| 36 | + </FormFieldController> |
| 37 | + </FormField> |
| 38 | + )} |
| 39 | + </FormMocked> |
| 40 | + ); |
| 41 | + |
| 42 | + const results = await axe(container); |
| 43 | + |
| 44 | + expect(results).toHaveNoViolations(); |
| 45 | +}); |
| 46 | + |
| 47 | +test('should select checkbox on button click', async () => { |
| 48 | + const user = setupUser(); |
| 49 | + const mockedSubmit = vi.fn(); |
| 50 | + |
| 51 | + render( |
| 52 | + <FormMocked |
| 53 | + schema={zFormSchema()} |
| 54 | + useFormOptions={{ defaultValues: { lovesBears: false } }} |
| 55 | + onSubmit={mockedSubmit} |
| 56 | + > |
| 57 | + {({ form }) => ( |
| 58 | + <FormField> |
| 59 | + <FormFieldController |
| 60 | + type="checkbox" |
| 61 | + control={form.control} |
| 62 | + name="lovesBears" |
| 63 | + > |
| 64 | + I love bears |
| 65 | + </FormFieldController> |
| 66 | + </FormField> |
| 67 | + )} |
| 68 | + </FormMocked> |
| 69 | + ); |
| 70 | + |
| 71 | + const checkbox = screen.getByRole('checkbox', { name: 'I love bears' }); |
| 72 | + expect(checkbox).not.toBeChecked(); |
| 73 | + |
| 74 | + await user.click(checkbox); |
| 75 | + expect(checkbox).toBeChecked(); |
| 76 | + |
| 77 | + await user.click(screen.getByRole('button', { name: 'Submit' })); |
| 78 | + expect(mockedSubmit).toHaveBeenCalledWith({ lovesBears: true }); |
| 79 | +}); |
| 80 | + |
| 81 | +test('should select checkbox on label click', async () => { |
| 82 | + const user = setupUser(); |
| 83 | + const mockedSubmit = vi.fn(); |
| 84 | + |
| 85 | + render( |
| 86 | + <FormMocked |
| 87 | + schema={zFormSchema()} |
| 88 | + useFormOptions={{ defaultValues: { lovesBears: false } }} |
| 89 | + onSubmit={mockedSubmit} |
| 90 | + > |
| 91 | + {({ form }) => ( |
| 92 | + <FormField> |
| 93 | + <FormFieldController |
| 94 | + type="checkbox" |
| 95 | + control={form.control} |
| 96 | + name="lovesBears" |
| 97 | + > |
| 98 | + I love bears |
| 99 | + </FormFieldController> |
| 100 | + </FormField> |
| 101 | + )} |
| 102 | + </FormMocked> |
| 103 | + ); |
| 104 | + |
| 105 | + const checkbox = screen.getByRole('checkbox', { name: 'I love bears' }); |
| 106 | + const label = screen.getByText('I love bears'); |
| 107 | + |
| 108 | + expect(checkbox).not.toBeChecked(); |
| 109 | + |
| 110 | + // Test clicking the label specifically |
| 111 | + await user.click(label); |
| 112 | + expect(checkbox).toBeChecked(); |
| 113 | + |
| 114 | + await user.click(screen.getByRole('button', { name: 'Submit' })); |
| 115 | + expect(mockedSubmit).toHaveBeenCalledWith({ lovesBears: true }); |
| 116 | +}); |
| 117 | + |
| 118 | +test('default value', async () => { |
| 119 | + const user = setupUser(); |
| 120 | + const mockedSubmit = vi.fn(); |
| 121 | + render( |
| 122 | + <FormMocked |
| 123 | + schema={zFormSchema()} |
| 124 | + useFormOptions={{ defaultValues: { lovesBears: true } }} |
| 125 | + onSubmit={mockedSubmit} |
| 126 | + > |
| 127 | + {({ form }) => ( |
| 128 | + <FormField> |
| 129 | + <FormFieldController |
| 130 | + type="checkbox" |
| 131 | + control={form.control} |
| 132 | + name="lovesBears" |
| 133 | + > |
| 134 | + I love bears |
| 135 | + </FormFieldController> |
| 136 | + </FormField> |
| 137 | + )} |
| 138 | + </FormMocked> |
| 139 | + ); |
| 140 | + |
| 141 | + const checkbox = screen.getByRole('checkbox', { name: 'I love bears' }); |
| 142 | + expect(checkbox).toBeChecked(); |
| 143 | + |
| 144 | + await user.click(screen.getByRole('button', { name: 'Submit' })); |
| 145 | + expect(mockedSubmit).toHaveBeenCalledWith({ lovesBears: true }); |
| 146 | +}); |
| 147 | + |
| 148 | +test('disabled', async () => { |
| 149 | + const user = setupUser(); |
| 150 | + const mockedSubmit = vi.fn(); |
| 151 | + render( |
| 152 | + <FormMocked |
| 153 | + schema={z.object({ lovesBears: z.boolean() })} |
| 154 | + useFormOptions={{ defaultValues: { lovesBears: false } }} |
| 155 | + onSubmit={mockedSubmit} |
| 156 | + > |
| 157 | + {({ form }) => ( |
| 158 | + <FormField> |
| 159 | + <FormFieldController |
| 160 | + type="checkbox" |
| 161 | + control={form.control} |
| 162 | + name="lovesBears" |
| 163 | + disabled |
| 164 | + > |
| 165 | + I love bears |
| 166 | + </FormFieldController> |
| 167 | + </FormField> |
| 168 | + )} |
| 169 | + </FormMocked> |
| 170 | + ); |
| 171 | + |
| 172 | + const checkbox = screen.getByRole('checkbox', { name: 'I love bears' }); |
| 173 | + expect(checkbox).toBeDisabled(); |
| 174 | + expect(checkbox).not.toBeChecked(); |
| 175 | + |
| 176 | + await user.click(checkbox); |
| 177 | + expect(checkbox).not.toBeChecked(); |
| 178 | + await user.click(screen.getByRole('button', { name: 'Submit' })); |
| 179 | + expect(mockedSubmit).toHaveBeenCalledWith({ lovesBears: undefined }); |
| 180 | +}); |
0 commit comments