Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

useField.test.tsx

982 lines29.2 KB

1import * as React from 'react'
2import { describe, expect, it, vi } from 'vitest'
3import { render, waitFor } from '@testing-library/react'
4import { userEvent } from '@testing-library/user-event'
5import { StrictMode } from 'react'
6import { useForm } from '../src/index'
7import { sleep } from './utils'
8import type { FieldApi, FormApi } from '../src/index'
9​
10const user = userEvent.setup()
11​
12describe('useField', () => {
13 it('should allow to set default value', () => {
14 type Person = {
15 firstName: string
16 lastName: string
17 }
18​
19 function Comp() {
20 const form = useForm({
21 defaultValues: {
22 firstName: 'FirstName',
23 lastName: 'LastName',
24 } as Person,
25 })
26​
27 return (
28 <>
29 <form.Field
30 name="firstName"
31 children={(field) => {
32 return (
33 <input
34 data-testid="fieldinput"
35 value={field.state.value}
36 onBlur={field.handleBlur}
37 onChange={(e) => field.handleChange(e.target.value)}
38 />
39 )
40 }}
41 />
42 </>
43 )
44 }
45​
46 const { getByTestId } = render(<Comp />)
47 const input = getByTestId('fieldinput')
48 expect(input).toHaveValue('FirstName')
49 })
50​
51 it('should use field default value first', () => {
52 type Person = {
53 firstName: string
54 lastName: string
55 }
56​
57 function Comp() {
58 const form = useForm({
59 defaultValues: {
60 firstName: 'FirstName',
61 lastName: 'LastName',
62 } as Person,
63 })
64​
65 return (
66 <>
67 <form.Field
68 name="firstName"
69 defaultValue="otherName"
70 children={(field) => {
71 return (
72 <input
73 data-testid="fieldinput"
74 value={field.state.value}
75 onBlur={field.handleBlur}
76 onChange={(e) => field.handleChange(e.target.value)}
77 />
78 )
79 }}
80 />
81 </>
82 )
83 }
84​
85 const { getByTestId } = render(<Comp />)
86 const input = getByTestId('fieldinput')
87 expect(input).toHaveValue('otherName')
88 })
89​
90 it('should not validate on change if isTouched is false', async () => {
91 type Person = {
92 firstName: string
93 lastName: string
94 }
95 const error = 'Please enter a different value'
96​
97 function Comp() {
98 const form = useForm({
99 defaultValues: {
100 firstName: '',
101 lastName: '',
102 } as Person,
103 })
104​
105 return (
106 <>
107 <form.Field
108 name="firstName"
109 validators={{
110 onChange: ({ value }) => (value === 'other' ? error : undefined),
111 }}
112 children={(field) => (
113 <div>
114 <input
115 data-testid="fieldinput"
116 name={field.name}
117 value={field.state.value}
118 onBlur={field.handleBlur}
119 onChange={(e) =>
120 field.setValue(e.target.value, {
121 dontUpdateMeta: true,
122 })
123 }
124 />
125 <p>{field.getMeta().errors}</p>
126 </div>
127 )}
128 />
129 </>
130 )
131 }
132​
133 const { getByTestId, queryByText } = render(<Comp />)
134 const input = getByTestId('fieldinput')
135 await user.type(input, 'other')
136 expect(queryByText(error)).not.toBeInTheDocument()
137 })
138​
139 it('should validate on change if isTouched is true', async () => {
140 type Person = {
141 firstName: string
142 lastName: string
143 }
144 const error = 'Please enter a different value'
145​
146 function Comp() {
147 const form = useForm({
148 defaultValues: {
149 firstName: '',
150 lastName: '',
151 } as Person,
152 })
153​
154 return (
155 <>
156 <form.Field
157 name="firstName"
158 defaultMeta={{ isTouched: true }}
159 validators={{
160 onChange: ({ value }) => (value === 'other' ? error : undefined),
161 }}
162 children={(field) => (
163 <div>
164 <input
165 data-testid="fieldinput"
166 name={field.name}
167 value={field.state.value}
168 onBlur={field.handleBlur}
169 onChange={(e) => field.handleChange(e.target.value)}
170 />
171 <p>{field.getMeta().errorMap.onChange}</p>
172 </div>
173 )}
174 />
175 </>
176 )
177 }
178​
179 const { getByTestId, getByText, queryByText } = render(<Comp />)
180 const input = getByTestId('fieldinput')
181 expect(queryByText(error)).not.toBeInTheDocument()
182 await user.type(input, 'other')
183 expect(getByText(error)).toBeInTheDocument()
184 })
185​
186 it('should validate on change and on blur', async () => {
187 type Person = {
188 firstName: string
189 lastName: string
190 }
191 const onChangeError = 'Please enter a different value (onChangeError)'
192 const onBlurError = 'Please enter a different value (onBlurError)'
193​
194 function Comp() {
195 const form = useForm({
196 defaultValues: {
197 firstName: '',
198 lastName: '',
199 } as Person,
200 })
201​
202 return (
203 <>
204 <form.Field
205 name="firstName"
206 defaultMeta={{ isTouched: true }}
207 validators={{
208 onChange: ({ value }) =>
209 value === 'other' ? onChangeError : undefined,
210 onBlur: ({ value }) =>
211 value === 'other' ? onBlurError : undefined,
212 }}
213 children={(field) => (
214 <div>
215 <input
216 data-testid="fieldinput"
217 name={field.name}
218 value={field.state.value}
219 onBlur={field.handleBlur}
220 onChange={(e) => field.handleChange(e.target.value)}
221 />
222 <p>{field.getMeta().errorMap.onChange}</p>
223 <p>{field.getMeta().errorMap.onBlur}</p>
224 </div>
225 )}
226 />
227 </>
228 )
229 }
230​
231 const { getByTestId, getByText, queryByText } = render(<Comp />)
232 const input = getByTestId('fieldinput')
233 expect(queryByText(onChangeError)).not.toBeInTheDocument()
234 expect(queryByText(onBlurError)).not.toBeInTheDocument()
235 await user.type(input, 'other')
236 expect(getByText(onChangeError)).toBeInTheDocument()
237 await user.click(document.body)
238 expect(queryByText(onBlurError)).toBeInTheDocument()
239 })
240​
241 it('should validate async on change', async () => {
242 type Person = {
243 firstName: string
244 lastName: string
245 }
246 const error = 'Please enter a different value'
247​
248 function Comp() {
249 const form = useForm({
250 defaultValues: {
251 firstName: '',
252 lastName: '',
253 } as Person,
254 })
255​
256 return (
257 <>
258 <form.Field
259 name="firstName"
260 defaultMeta={{ isTouched: true }}
261 validators={{
262 onChangeAsync: async () => {
263 await sleep(10)
264 return error
265 },
266 }}
267 children={(field) => (
268 <div>
269 <input
270 data-testid="fieldinput"
271 name={field.name}
272 value={field.state.value}
273 onBlur={field.handleBlur}
274 onChange={(e) => field.handleChange(e.target.value)}
275 />
276 <p>{field.getMeta().errorMap.onChange}</p>
277 </div>
278 )}
279 />
280 </>
281 )
282 }
283​
284 const { getByTestId, getByText, queryByText } = render(<Comp />)
285 const input = getByTestId('fieldinput')
286 expect(queryByText(error)).not.toBeInTheDocument()
287 await user.type(input, 'other')
288 await waitFor(() => getByText(error))
289 expect(getByText(error)).toBeInTheDocument()
290 })
291​
292 it('should validate async on change and async on blur', async () => {
293 type Person = {
294 firstName: string
295 lastName: string
296 }
297 const onChangeError = 'Please enter a different value (onChangeError)'
298 const onBlurError = 'Please enter a different value (onBlurError)'
299​
300 function Comp() {
301 const form = useForm({
302 defaultValues: {
303 firstName: '',
304 lastName: '',
305 } as Person,
306 })
307​
308 return (
309 <>
310 <form.Field
311 name="firstName"
312 defaultMeta={{ isTouched: true }}
313 validators={{
314 onChangeAsync: async () => {
315 await sleep(10)
316 return onChangeError
317 },
318 onBlurAsync: async () => {
319 await sleep(10)
320 return onBlurError
321 },
322 }}
323 children={(field) => (
324 <div>
325 <input
326 data-testid="fieldinput"
327 name={field.name}
328 value={field.state.value}
329 onBlur={field.handleBlur}
330 onChange={(e) => field.handleChange(e.target.value)}
331 />
332 <p>{field.getMeta().errorMap.onChange}</p>
333 <p>{field.getMeta().errorMap.onBlur}</p>
334 </div>
335 )}
336 />
337 </>
338 )
339 }
340​
341 const { getByTestId, getByText, queryByText } = render(<Comp />)
342 const input = getByTestId('fieldinput')
343​
344 expect(queryByText(onChangeError)).not.toBeInTheDocument()
345 expect(queryByText(onBlurError)).not.toBeInTheDocument()
346 await user.type(input, 'other')
347 await waitFor(() => getByText(onChangeError))
348 expect(getByText(onChangeError)).toBeInTheDocument()
349 await user.click(document.body)
350 await waitFor(() => getByText(onBlurError))
351 expect(getByText(onBlurError)).toBeInTheDocument()
352 })
353​
354 it('should validate async on change with debounce', async () => {
355 type Person = {
356 firstName: string
357 lastName: string
358 }
359 const mockFn = vi.fn()
360 const error = 'Please enter a different value'
361​
362 function Comp() {
363 const form = useForm({
364 defaultValues: {
365 firstName: '',
366 lastName: '',
367 } as Person,
368 })
369​
370 return (
371 <>
372 <form.Field
373 name="firstName"
374 defaultMeta={{ isTouched: true }}
375 validators={{
376 onChangeAsyncDebounceMs: 100,
377 onChangeAsync: async () => {
378 mockFn()
379 await sleep(10)
380 return error
381 },
382 }}
383 children={(field) => (
384 <div>
385 <input
386 data-testid="fieldinput"
387 name={field.name}
388 value={field.state.value}
389 onBlur={field.handleBlur}
390 onChange={(e) => field.handleChange(e.target.value)}
391 />
392 <p>{field.getMeta().errors}</p>
393 </div>
394 )}
395 />
396 </>
397 )
398 }
399​
400 const { getByTestId, getByText } = render(<Comp />)
401 const input = getByTestId('fieldinput')
402 await user.type(input, 'other')
403 // mockFn will have been called 5 times without onChangeAsyncDebounceMs
404 expect(mockFn).toHaveBeenCalledTimes(0)
405 await waitFor(() => getByText(error))
406 expect(getByText(error)).toBeInTheDocument()
407 })
408​
409 it('should handle strict mode properly with conditional fields', async () => {
410 function FieldInfo({ field }: { field: FieldApi<any, any> }) {
411 return (
412 <>
413 {field.state.meta.isTouched && field.state.meta.errors.length ? (
414 <em>{field.state.meta.errors.join(',')}</em>
415 ) : null}
416 {field.state.meta.isValidating ? 'Validating...' : null}
417 </>
418 )
419 }
420​
421 function Comp() {
422 const [showField, setShowField] = React.useState(true)
423​
424 const form = useForm({
425 defaultValues: {
426 firstName: '',
427 lastName: '',
428 },
429 onSubmit: async () => {},
430 })
431​
432 return (
433 <div>
434 <form
435 onSubmit={(e) => {
436 e.preventDefault()
437 e.stopPropagation()
438 form.handleSubmit()
439 }}
440 >
441 <div>
442 {/* A type-safe field component*/}
443 {showField ? (
444 <form.Field
445 name="firstName"
446 validators={{
447 onChange: ({ value }) =>
448 !value ? 'A first name is required' : undefined,
449 }}
450 children={(field) => {
451 // Avoid hasty abstractions. Render props are great!
452 return (
453 <>
454 <label htmlFor={field.name}>First Name:</label>
455 <input
456 name={field.name}
457 value={field.state.value}
458 onBlur={field.handleBlur}
459 onChange={(e) => field.handleChange(e.target.value)}
460 />
461 <FieldInfo field={field} />
462 </>
463 )
464 }}
465 />
466 ) : null}
467 </div>
468 <div>
469 <form.Field
470 name="lastName"
471 children={(field) => (
472 <>
473 <label htmlFor={field.name}>Last Name:</label>
474 <input
475 name={field.name}
476 value={field.state.value}
477 onBlur={field.handleBlur}
478 onChange={(e) => field.handleChange(e.target.value)}
479 />
480 <FieldInfo field={field} />
481 </>
482 )}
483 />
484 </div>
485 <form.Subscribe
486 selector={(state) => [state.canSubmit, state.isSubmitting]}
487 children={([canSubmit, isSubmitting]) => (
488 <button type="submit" disabled={!canSubmit}>
489 {isSubmitting ? '...' : 'Submit'}
490 </button>
491 )}
492 />
493 <button type="button" onClick={() => setShowField((prev) => !prev)}>
494 {showField ? 'Hide field' : 'Show field'}
495 </button>
496 </form>
497 </div>
498 )
499 }
500​
501 const { getByText, findByText, queryByText } = render(
502 <React.StrictMode>
503 <Comp />
504 </React.StrictMode>,
505 )
506​
507 await user.click(getByText('Submit'))
508 expect(await findByText('A first name is required')).toBeInTheDocument()
509 await user.click(getByText('Hide field'))
510 await user.click(getByText('Submit'))
511 expect(queryByText('A first name is required')).not.toBeInTheDocument()
512 })
513​
514 it('should handle arrays with primitive values', async () => {
515 const fn = vi.fn()
516 function Comp() {
517 const form = useForm({
518 defaultValues: {
519 people: [] as Array<string>,
520 },
521 onSubmit: ({ value }) => fn(value),
522 })
523​
524 return (
525 <div>
526 <form
527 onSubmit={(e) => {
528 e.preventDefault()
529 e.stopPropagation()
530 form.handleSubmit()
531 }}
532 >
533 <form.Field name="people">
534 {(field) => {
535 return (
536 <div>
537 {field.state.value.map((_, i) => {
538 return (
539 <form.Field key={i} name={`people[${i}]`}>
540 {(subField) => {
541 return (
542 <div>
543 <label>
544 <div>Name for person {i}</div>
545 <input
546 value={subField.state.value}
547 onChange={(e) =>
548 subField.handleChange(e.target.value)
549 }
550 />
551 </label>
552 <button
553 onClick={() => field.removeValue(i)}
554 type="button"
555 >
556 Remove person {i}
557 </button>
558 </div>
559 )
560 }}
561 </form.Field>
562 )
563 })}
564 <button onClick={() => field.pushValue('')} type="button">
565 Add person
566 </button>
567 </div>
568 )
569 }}
570 </form.Field>
571 <form.Subscribe
572 selector={(state) => [state.canSubmit, state.isSubmitting]}
573 children={([canSubmit, isSubmitting]) => (
574 <button type="submit" disabled={!canSubmit}>
575 {isSubmitting ? '...' : 'Submit'}
576 </button>
577 )}
578 />
579 </form>
580 </div>
581 )
582 }
583​
584 const { getByText, findByLabelText, queryByText, findByText } = render(
585 <Comp />,
586 )
587​
588 expect(queryByText('Name for person 0')).not.toBeInTheDocument()
589 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
590 await user.click(getByText('Add person'))
591 const input = await findByLabelText('Name for person 0')
592 expect(input).toBeInTheDocument()
593 await user.type(input, 'John')
594​
595 await user.click(getByText('Add person'))
596 const input2 = await findByLabelText('Name for person 1')
597 expect(input).toBeInTheDocument()
598 await user.type(input2, 'Jack')
599​
600 expect(queryByText('Name for person 0')).toBeInTheDocument()
601 expect(queryByText('Name for person 1')).toBeInTheDocument()
602 await user.click(getByText('Remove person 1'))
603 expect(queryByText('Name for person 0')).toBeInTheDocument()
604 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
605​
606 await user.click(await findByText('Submit'))
607 expect(fn).toHaveBeenCalledWith({ people: ['John'] })
608 })
609​
610 it('should handle arrays with subvalues', async () => {
611 const fn = vi.fn()
612 function Comp() {
613 const form = useForm({
614 defaultValues: {
615 people: [] as Array<{ age: number; name: string }>,
616 },
617 onSubmit: ({ value }) => fn(value),
618 })
619​
620 return (
621 <div>
622 <form
623 onSubmit={(e) => {
624 e.preventDefault()
625 e.stopPropagation()
626 form.handleSubmit()
627 }}
628 >
629 <form.Field name="people">
630 {(field) => {
631 return (
632 <div>
633 {field.state.value.map((_, i) => {
634 return (
635 <form.Field key={i} name={`people[${i}].name`}>
636 {(subField) => {
637 return (
638 <div>
639 <label>
640 <div>Name for person {i}</div>
641 <input
642 value={subField.state.value}
643 onChange={(e) =>
644 subField.handleChange(e.target.value)
645 }
646 />
647 </label>
648 <button
649 onClick={() => field.removeValue(i)}
650 type="button"
651 >
652 Remove person {i}
653 </button>
654 </div>
655 )
656 }}
657 </form.Field>
658 )
659 })}
660 <button
661 onClick={() => field.pushValue({ name: '', age: 0 })}
662 type="button"
663 >
664 Add person
665 </button>
666 </div>
667 )
668 }}
669 </form.Field>
670 <form.Subscribe
671 selector={(state) => [state.canSubmit, state.isSubmitting]}
672 children={([canSubmit, isSubmitting]) => (
673 <button type="submit" disabled={!canSubmit}>
674 {isSubmitting ? '...' : 'Submit'}
675 </button>
676 )}
677 />
678 </form>
679 </div>
680 )
681 }
682​
683 const { getByText, findByLabelText, queryByText, findByText } = render(
684 <Comp />,
685 )
686​
687 expect(queryByText('Name for person 0')).not.toBeInTheDocument()
688 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
689 await user.click(getByText('Add person'))
690 const input = await findByLabelText('Name for person 0')
691 expect(input).toBeInTheDocument()
692 await user.type(input, 'John')
693​
694 await user.click(getByText('Add person'))
695 const input2 = await findByLabelText('Name for person 1')
696 expect(input).toBeInTheDocument()
697 await user.type(input2, 'Jack')
698​
699 expect(queryByText('Name for person 0')).toBeInTheDocument()
700 expect(queryByText('Name for person 1')).toBeInTheDocument()
701 await user.click(getByText('Remove person 1'))
702 expect(queryByText('Name for person 0')).toBeInTheDocument()
703 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
704​
705 await user.click(await findByText('Submit'))
706 expect(fn).toHaveBeenCalledWith({ people: [{ name: 'John', age: 0 }] })
707 })
708​
709 it('should handle sync linked fields', async () => {
710 const fn = vi.fn()
711 function Comp() {
712 const form = useForm({
713 defaultValues: {
714 password: '',
715 confirm_password: '',
716 },
717 onSubmit: ({ value }) => fn(value),
718 })
719​
720 return (
721 <div>
722 <form.Field name="password">
723 {(field) => {
724 return (
725 <div>
726 <label>
727 <div>Password</div>
728 <input
729 value={field.state.value}
730 onChange={(e) => field.handleChange(e.target.value)}
731 />
732 </label>
733 </div>
734 )
735 }}
736 </form.Field>
737 <form.Field
738 name="confirm_password"
739 validators={{
740 onChangeListenTo: ['password'],
741 onChange: ({ value, fieldApi }) => {
742 if (value !== fieldApi.form.getFieldValue('password')) {
743 return 'Passwords do not match'
744 }
745 return undefined
746 },
747 }}
748 >
749 {(field) => {
750 return (
751 <div>
752 <label>
753 <div>Confirm Password</div>
754 <input
755 value={field.state.value}
756 onChange={(e) => field.handleChange(e.target.value)}
757 />
758 </label>
759 {field.state.meta.errors.map((err) => {
760 return <div key={err?.toString()}>{err}</div>
761 })}
762 </div>
763 )
764 }}
765 </form.Field>
766 </div>
767 )
768 }
769​
770 const { findByLabelText, queryByText, findByText } = render(<Comp />)
771​
772 const passwordInput = await findByLabelText('Password')
773 const confirmPasswordInput = await findByLabelText('Confirm Password')
774 await user.type(passwordInput, 'password')
775 await user.type(confirmPasswordInput, 'password')
776 expect(queryByText('Passwords do not match')).not.toBeInTheDocument()
777 await user.type(confirmPasswordInput, '1')
778 expect(await findByText('Passwords do not match')).toBeInTheDocument()
779 })
780​
781 it('should handle deeply nested values in StrictMode', async () => {
782 function Comp() {
783 const form = useForm({
784 defaultValues: {
785 name: { first: 'Test', last: 'User' },
786 },
787 })
788​
789 return (
790 <form.Field
791 name="name.last"
792 children={(field) => <p>{field.state.value}</p>}
793 />
794 )
795 }
796​
797 const { queryByText, findByText } = render(
798 <React.StrictMode>
799 <Comp />
800 </React.StrictMode>,
801 )
802​
803 expect(queryByText('Test')).not.toBeInTheDocument()
804 expect(await findByText('User')).toBeInTheDocument()
805 })
806​
807 it('should validate async on submit without debounce', async () => {
808 type Person = {
809 firstName: string
810 lastName: string
811 }
812 const mockFn = vi.fn()
813 const error = 'Please enter a different value'
814​
815 function Comp() {
816 const form = useForm({
817 defaultValues: {
818 firstName: '',
819 lastName: '',
820 } as Person,
821 validators: {
822 onChangeAsyncDebounceMs: 1000000,
823 onChangeAsync: async () => {
824 mockFn()
825 await sleep(10)
826 return error
827 },
828 },
829 })
830 const errors = form.useStore((s) => s.errors)
831​
832 return (
833 <>
834 <form.Field
835 name="firstName"
836 defaultMeta={{ isTouched: true }}
837 children={(field) => (
838 <div>
839 <input
840 data-testid="fieldinput"
841 name={field.name}
842 value={field.state.value}
843 onBlur={field.handleBlur}
844 onChange={(e) => field.handleChange(e.target.value)}
845 />
846 <p>{errors}</p>
847 </div>
848 )}
849 />
850 <button onClick={form.handleSubmit}>Submit</button>
851 </>
852 )
853 }
854​
855 const { getByRole, getByText } = render(<Comp />)
856 await user.click(getByRole('button', { name: 'Submit' }))
857​
858 expect(mockFn).toHaveBeenCalledTimes(1)
859 await waitFor(() => getByText(error))
860 expect(getByText(error)).toBeInTheDocument()
861 })
862​
863 it('should validate allow pushvalue to implicitly set a default value', async () => {
864 type Person = {
865 people: Array<string>
866 }
867​
868 function Comp() {
869 const form = useForm({
870 defaultValues: {
871 people: [],
872 } as Person,
873 })
874​
875 return (
876 <form.Field name="people" mode="array">
877 {(field) => {
878 return (
879 <div>
880 <pre>{JSON.stringify(field.state.value)}</pre>
881 {field.state.value.map((_, i) => {
882 return (
883 <form.Field key={i} name={`people[${i}]`}>
884 {(subField) => {
885 return (
886 <div>
887 <label>
888 <div>Name for person {i}</div>
889 <input
890 name={subField.name}
891 value={subField.state.value}
892 onChange={(e) =>
893 subField.handleChange(e.target.value)
894 }
895 />
896 </label>
897 </div>
898 )
899 }}
900 </form.Field>
901 )
902 })}
903 <button onClick={() => field.pushValue('')} type="button">
904 Add person
905 </button>
906 </div>
907 )
908 }}
909 </form.Field>
910 )
911 }
912​
913 const { getByText, queryByText } = render(
914 <StrictMode>
915 <Comp />
916 </StrictMode>,
917 )
918 expect(getByText('[]')).toBeInTheDocument()
919 await user.click(getByText('Add person'))
920 expect(getByText(`[""]`)).toBeInTheDocument()
921 })
922​
923 it('should validate allow pushvalue to implicitly set a pushed default value', async () => {
924 type Person = {
925 people: Array<string>
926 }
927​
928 function Comp() {
929 const form = useForm({
930 defaultValues: {
931 people: [],
932 } as Person,
933 })
934​
935 return (
936 <form.Field name="people" mode="array">
937 {(field) => {
938 return (
939 <div>
940 <pre>{JSON.stringify(field.state.value)}</pre>
941 {field.state.value.map((_, i) => {
942 return (
943 <form.Field key={i} name={`people[${i}]`}>
944 {(subField) => {
945 return (
946 <div>
947 <label>
948 <div>Name for person {i}</div>
949 <input
950 name={subField.name}
951 value={subField.state.value}
952 onChange={(e) =>
953 subField.handleChange(e.target.value)
954 }
955 />
956 </label>
957 </div>
958 )
959 }}
960 </form.Field>
961 )
962 })}
963 <button onClick={() => field.pushValue('Test')} type="button">
964 Add person
965 </button>
966 </div>
967 )
968 }}
969 </form.Field>
970 )
971 }
972​
973 const { getByText, queryByText } = render(
974 <StrictMode>
975 <Comp />
976 </StrictMode>,
977 )
978 expect(getByText('[]')).toBeInTheDocument()
979 await user.click(getByText('Add person'))
980 expect(getByText(`["Test"]`)).toBeInTheDocument()
981 })
982})