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

useField.test.tsx

983 lines29.3 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 // eslint-disable-next-line ts/no-empty-function
430 onSubmit: async () => {},
431 })
432​
433 return (
434 <div>
435 <form
436 onSubmit={(e) => {
437 e.preventDefault()
438 e.stopPropagation()
439 form.handleSubmit()
440 }}
441 >
442 <div>
443 {/* A type-safe field component*/}
444 {showField ? (
445 <form.Field
446 name="firstName"
447 validators={{
448 onChange: ({ value }) =>
449 !value ? 'A first name is required' : undefined,
450 }}
451 children={(field) => {
452 // Avoid hasty abstractions. Render props are great!
453 return (
454 <>
455 <label htmlFor={field.name}>First Name:</label>
456 <input
457 name={field.name}
458 value={field.state.value}
459 onBlur={field.handleBlur}
460 onChange={(e) => field.handleChange(e.target.value)}
461 />
462 <FieldInfo field={field} />
463 </>
464 )
465 }}
466 />
467 ) : null}
468 </div>
469 <div>
470 <form.Field
471 name="lastName"
472 children={(field) => (
473 <>
474 <label htmlFor={field.name}>Last Name:</label>
475 <input
476 name={field.name}
477 value={field.state.value}
478 onBlur={field.handleBlur}
479 onChange={(e) => field.handleChange(e.target.value)}
480 />
481 <FieldInfo field={field} />
482 </>
483 )}
484 />
485 </div>
486 <form.Subscribe
487 selector={(state) => [state.canSubmit, state.isSubmitting]}
488 children={([canSubmit, isSubmitting]) => (
489 <button type="submit" disabled={!canSubmit}>
490 {isSubmitting ? '...' : 'Submit'}
491 </button>
492 )}
493 />
494 <button type="button" onClick={() => setShowField((prev) => !prev)}>
495 {showField ? 'Hide field' : 'Show field'}
496 </button>
497 </form>
498 </div>
499 )
500 }
501​
502 const { getByText, findByText, queryByText } = render(
503 <React.StrictMode>
504 <Comp />
505 </React.StrictMode>,
506 )
507​
508 await user.click(getByText('Submit'))
509 expect(await findByText('A first name is required')).toBeInTheDocument()
510 await user.click(getByText('Hide field'))
511 await user.click(getByText('Submit'))
512 expect(queryByText('A first name is required')).not.toBeInTheDocument()
513 })
514​
515 it('should handle arrays with primitive values', async () => {
516 const fn = vi.fn()
517 function Comp() {
518 const form = useForm({
519 defaultValues: {
520 people: [] as Array<string>,
521 },
522 onSubmit: ({ value }) => fn(value),
523 })
524​
525 return (
526 <div>
527 <form
528 onSubmit={(e) => {
529 e.preventDefault()
530 e.stopPropagation()
531 form.handleSubmit()
532 }}
533 >
534 <form.Field name="people">
535 {(field) => {
536 return (
537 <div>
538 {field.state.value.map((_, i) => {
539 return (
540 <form.Field key={i} name={`people[${i}]`}>
541 {(subField) => {
542 return (
543 <div>
544 <label>
545 <div>Name for person {i}</div>
546 <input
547 value={subField.state.value}
548 onChange={(e) =>
549 subField.handleChange(e.target.value)
550 }
551 />
552 </label>
553 <button
554 onClick={() => field.removeValue(i)}
555 type="button"
556 >
557 Remove person {i}
558 </button>
559 </div>
560 )
561 }}
562 </form.Field>
563 )
564 })}
565 <button onClick={() => field.pushValue('')} type="button">
566 Add person
567 </button>
568 </div>
569 )
570 }}
571 </form.Field>
572 <form.Subscribe
573 selector={(state) => [state.canSubmit, state.isSubmitting]}
574 children={([canSubmit, isSubmitting]) => (
575 <button type="submit" disabled={!canSubmit}>
576 {isSubmitting ? '...' : 'Submit'}
577 </button>
578 )}
579 />
580 </form>
581 </div>
582 )
583 }
584​
585 const { getByText, findByLabelText, queryByText, findByText } = render(
586 <Comp />,
587 )
588​
589 expect(queryByText('Name for person 0')).not.toBeInTheDocument()
590 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
591 await user.click(getByText('Add person'))
592 const input = await findByLabelText('Name for person 0')
593 expect(input).toBeInTheDocument()
594 await user.type(input, 'John')
595​
596 await user.click(getByText('Add person'))
597 const input2 = await findByLabelText('Name for person 1')
598 expect(input).toBeInTheDocument()
599 await user.type(input2, 'Jack')
600​
601 expect(queryByText('Name for person 0')).toBeInTheDocument()
602 expect(queryByText('Name for person 1')).toBeInTheDocument()
603 await user.click(getByText('Remove person 1'))
604 expect(queryByText('Name for person 0')).toBeInTheDocument()
605 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
606​
607 await user.click(await findByText('Submit'))
608 expect(fn).toHaveBeenCalledWith({ people: ['John'] })
609 })
610​
611 it('should handle arrays with subvalues', async () => {
612 const fn = vi.fn()
613 function Comp() {
614 const form = useForm({
615 defaultValues: {
616 people: [] as Array<{ age: number; name: string }>,
617 },
618 onSubmit: ({ value }) => fn(value),
619 })
620​
621 return (
622 <div>
623 <form
624 onSubmit={(e) => {
625 e.preventDefault()
626 e.stopPropagation()
627 form.handleSubmit()
628 }}
629 >
630 <form.Field name="people">
631 {(field) => {
632 return (
633 <div>
634 {field.state.value.map((_, i) => {
635 return (
636 <form.Field key={i} name={`people[${i}].name`}>
637 {(subField) => {
638 return (
639 <div>
640 <label>
641 <div>Name for person {i}</div>
642 <input
643 value={subField.state.value}
644 onChange={(e) =>
645 subField.handleChange(e.target.value)
646 }
647 />
648 </label>
649 <button
650 onClick={() => field.removeValue(i)}
651 type="button"
652 >
653 Remove person {i}
654 </button>
655 </div>
656 )
657 }}
658 </form.Field>
659 )
660 })}
661 <button
662 onClick={() => field.pushValue({ name: '', age: 0 })}
663 type="button"
664 >
665 Add person
666 </button>
667 </div>
668 )
669 }}
670 </form.Field>
671 <form.Subscribe
672 selector={(state) => [state.canSubmit, state.isSubmitting]}
673 children={([canSubmit, isSubmitting]) => (
674 <button type="submit" disabled={!canSubmit}>
675 {isSubmitting ? '...' : 'Submit'}
676 </button>
677 )}
678 />
679 </form>
680 </div>
681 )
682 }
683​
684 const { getByText, findByLabelText, queryByText, findByText } = render(
685 <Comp />,
686 )
687​
688 expect(queryByText('Name for person 0')).not.toBeInTheDocument()
689 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
690 await user.click(getByText('Add person'))
691 const input = await findByLabelText('Name for person 0')
692 expect(input).toBeInTheDocument()
693 await user.type(input, 'John')
694​
695 await user.click(getByText('Add person'))
696 const input2 = await findByLabelText('Name for person 1')
697 expect(input).toBeInTheDocument()
698 await user.type(input2, 'Jack')
699​
700 expect(queryByText('Name for person 0')).toBeInTheDocument()
701 expect(queryByText('Name for person 1')).toBeInTheDocument()
702 await user.click(getByText('Remove person 1'))
703 expect(queryByText('Name for person 0')).toBeInTheDocument()
704 expect(queryByText('Name for person 1')).not.toBeInTheDocument()
705​
706 await user.click(await findByText('Submit'))
707 expect(fn).toHaveBeenCalledWith({ people: [{ name: 'John', age: 0 }] })
708 })
709​
710 it('should handle sync linked fields', async () => {
711 const fn = vi.fn()
712 function Comp() {
713 const form = useForm({
714 defaultValues: {
715 password: '',
716 confirm_password: '',
717 },
718 onSubmit: ({ value }) => fn(value),
719 })
720​
721 return (
722 <div>
723 <form.Field name="password">
724 {(field) => {
725 return (
726 <div>
727 <label>
728 <div>Password</div>
729 <input
730 value={field.state.value}
731 onChange={(e) => field.handleChange(e.target.value)}
732 />
733 </label>
734 </div>
735 )
736 }}
737 </form.Field>
738 <form.Field
739 name="confirm_password"
740 validators={{
741 onChangeListenTo: ['password'],
742 onChange: ({ value, fieldApi }) => {
743 if (value !== fieldApi.form.getFieldValue('password')) {
744 return 'Passwords do not match'
745 }
746 return undefined
747 },
748 }}
749 >
750 {(field) => {
751 return (
752 <div>
753 <label>
754 <div>Confirm Password</div>
755 <input
756 value={field.state.value}
757 onChange={(e) => field.handleChange(e.target.value)}
758 />
759 </label>
760 {field.state.meta.errors.map((err) => {
761 return <div key={err?.toString()}>{err}</div>
762 })}
763 </div>
764 )
765 }}
766 </form.Field>
767 </div>
768 )
769 }
770​
771 const { findByLabelText, queryByText, findByText } = render(<Comp />)
772​
773 const passwordInput = await findByLabelText('Password')
774 const confirmPasswordInput = await findByLabelText('Confirm Password')
775 await user.type(passwordInput, 'password')
776 await user.type(confirmPasswordInput, 'password')
777 expect(queryByText('Passwords do not match')).not.toBeInTheDocument()
778 await user.type(confirmPasswordInput, '1')
779 expect(await findByText('Passwords do not match')).toBeInTheDocument()
780 })
781​
782 it('should handle deeply nested values in StrictMode', async () => {
783 function Comp() {
784 const form = useForm({
785 defaultValues: {
786 name: { first: 'Test', last: 'User' },
787 },
788 })
789​
790 return (
791 <form.Field
792 name="name.last"
793 children={(field) => <p>{field.state.value}</p>}
794 />
795 )
796 }
797​
798 const { queryByText, findByText } = render(
799 <React.StrictMode>
800 <Comp />
801 </React.StrictMode>,
802 )
803​
804 expect(queryByText('Test')).not.toBeInTheDocument()
805 expect(await findByText('User')).toBeInTheDocument()
806 })
807​
808 it('should validate async on submit without debounce', async () => {
809 type Person = {
810 firstName: string
811 lastName: string
812 }
813 const mockFn = vi.fn()
814 const error = 'Please enter a different value'
815​
816 function Comp() {
817 const form = useForm({
818 defaultValues: {
819 firstName: '',
820 lastName: '',
821 } as Person,
822 validators: {
823 onChangeAsyncDebounceMs: 1000000,
824 onChangeAsync: async () => {
825 mockFn()
826 await sleep(10)
827 return error
828 },
829 },
830 })
831 const errors = form.useStore((s) => s.errors)
832​
833 return (
834 <>
835 <form.Field
836 name="firstName"
837 defaultMeta={{ isTouched: true }}
838 children={(field) => (
839 <div>
840 <input
841 data-testid="fieldinput"
842 name={field.name}
843 value={field.state.value}
844 onBlur={field.handleBlur}
845 onChange={(e) => field.handleChange(e.target.value)}
846 />
847 <p>{errors}</p>
848 </div>
849 )}
850 />
851 <button onClick={form.handleSubmit}>Submit</button>
852 </>
853 )
854 }
855​
856 const { getByRole, getByText } = render(<Comp />)
857 await user.click(getByRole('button', { name: 'Submit' }))
858​
859 expect(mockFn).toHaveBeenCalledTimes(1)
860 await waitFor(() => getByText(error))
861 expect(getByText(error)).toBeInTheDocument()
862 })
863​
864 it('should validate allow pushvalue to implicitly set a default value', async () => {
865 type Person = {
866 people: Array<string>
867 }
868​
869 function Comp() {
870 const form = useForm({
871 defaultValues: {
872 people: [],
873 } as Person,
874 })
875​
876 return (
877 <form.Field name="people" mode="array">
878 {(field) => {
879 return (
880 <div>
881 <pre>{JSON.stringify(field.state.value)}</pre>
882 {field.state.value.map((_, i) => {
883 return (
884 <form.Field key={i} name={`people[${i}]`}>
885 {(subField) => {
886 return (
887 <div>
888 <label>
889 <div>Name for person {i}</div>
890 <input
891 name={subField.name}
892 value={subField.state.value}
893 onChange={(e) =>
894 subField.handleChange(e.target.value)
895 }
896 />
897 </label>
898 </div>
899 )
900 }}
901 </form.Field>
902 )
903 })}
904 <button onClick={() => field.pushValue('')} type="button">
905 Add person
906 </button>
907 </div>
908 )
909 }}
910 </form.Field>
911 )
912 }
913​
914 const { getByText, queryByText } = render(
915 <StrictMode>
916 <Comp />
917 </StrictMode>,
918 )
919 expect(getByText('[]')).toBeInTheDocument()
920 await user.click(getByText('Add person'))
921 expect(getByText(`[""]`)).toBeInTheDocument()
922 })
923​
924 it('should validate allow pushvalue to implicitly set a pushed default value', async () => {
925 type Person = {
926 people: Array<string>
927 }
928​
929 function Comp() {
930 const form = useForm({
931 defaultValues: {
932 people: [],
933 } as Person,
934 })
935​
936 return (
937 <form.Field name="people" mode="array">
938 {(field) => {
939 return (
940 <div>
941 <pre>{JSON.stringify(field.state.value)}</pre>
942 {field.state.value.map((_, i) => {
943 return (
944 <form.Field key={i} name={`people[${i}]`}>
945 {(subField) => {
946 return (
947 <div>
948 <label>
949 <div>Name for person {i}</div>
950 <input
951 name={subField.name}
952 value={subField.state.value}
953 onChange={(e) =>
954 subField.handleChange(e.target.value)
955 }
956 />
957 </label>
958 </div>
959 )
960 }}
961 </form.Field>
962 )
963 })}
964 <button onClick={() => field.pushValue('Test')} type="button">
965 Add person
966 </button>
967 </div>
968 )
969 }}
970 </form.Field>
971 )
972 }
973​
974 const { getByText, queryByText } = render(
975 <StrictMode>
976 <Comp />
977 </StrictMode>,
978 )
979 expect(getByText('[]')).toBeInTheDocument()
980 await user.click(getByText('Add person'))
981 expect(getByText(`["Test"]`)).toBeInTheDocument()
982 })
983})