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 { useStore } from '@tanstack/react-store'
7import { useForm } from '../src/index'
8import { sleep } from './utils'
9import type { FieldApi } from '../src/index'
10​
11const user = userEvent.setup()
12​
13describe('useField', () => {
14 it('should allow to set default value', () => {
15 type Person = {
16 firstName: string
17 lastName: string
18 }
19​
20 function Comp() {
21 const form = useForm({
22 defaultValues: {
23 firstName: 'FirstName',
24 lastName: 'LastName',
25 } as Person,
26 })
27​
28 return (
29 <>
30 <form.Field
31 name="firstName"
32 children={(field) => {
33 return (
34 <input
35 data-testid="fieldinput"
36 value={field.state.value}
37 onBlur={field.handleBlur}
38 onChange={(e) => field.handleChange(e.target.value)}
39 />
40 )
41 }}
42 />
43 </>
44 )
45 }
46​
47 const { getByTestId } = render(<Comp />)
48 const input = getByTestId('fieldinput')
49 expect(input).toHaveValue('FirstName')
50 })
51​
52 it('should use field default value first', () => {
53 type Person = {
54 firstName: string
55 lastName: string
56 }
57​
58 function Comp() {
59 const form = useForm({
60 defaultValues: {
61 firstName: 'FirstName',
62 lastName: 'LastName',
63 } as Person,
64 })
65​
66 return (
67 <>
68 <form.Field
69 name="firstName"
70 defaultValue="otherName"
71 children={(field) => {
72 return (
73 <input
74 data-testid="fieldinput"
75 value={field.state.value}
76 onBlur={field.handleBlur}
77 onChange={(e) => field.handleChange(e.target.value)}
78 />
79 )
80 }}
81 />
82 </>
83 )
84 }
85​
86 const { getByTestId } = render(<Comp />)
87 const input = getByTestId('fieldinput')
88 expect(input).toHaveValue('otherName')
89 })
90​
91 it('should not validate on change if isTouched is false', async () => {
92 type Person = {
93 firstName: string
94 lastName: string
95 }
96 const error = 'Please enter a different value'
97​
98 function Comp() {
99 const form = useForm({
100 defaultValues: {
101 firstName: '',
102 lastName: '',
103 } as Person,
104 })
105​
106 return (
107 <>
108 <form.Field
109 name="firstName"
110 validators={{
111 onChange: ({ value }) => (value === 'other' ? error : undefined),
112 }}
113 children={(field) => (
114 <div>
115 <input
116 data-testid="fieldinput"
117 name={field.name}
118 value={field.state.value}
119 onBlur={field.handleBlur}
120 onChange={(e) =>
121 field.setValue(e.target.value, {
122 dontUpdateMeta: true,
123 })
124 }
125 />
126 <p>{field.getMeta().errors}</p>
127 </div>
128 )}
129 />
130 </>
131 )
132 }
133​
134 const { getByTestId, queryByText } = render(<Comp />)
135 const input = getByTestId('fieldinput')
136 await user.type(input, 'other')
137 expect(queryByText(error)).not.toBeInTheDocument()
138 })
139​
140 it('should validate on change if isTouched is true', async () => {
141 type Person = {
142 firstName: string
143 lastName: string
144 }
145 const error = 'Please enter a different value'
146​
147 function Comp() {
148 const form = useForm({
149 defaultValues: {
150 firstName: '',
151 lastName: '',
152 } as Person,
153 })
154​
155 return (
156 <>
157 <form.Field
158 name="firstName"
159 defaultMeta={{ isTouched: true }}
160 validators={{
161 onChange: ({ value }) => (value === 'other' ? error : undefined),
162 }}
163 children={(field) => (
164 <div>
165 <input
166 data-testid="fieldinput"
167 name={field.name}
168 value={field.state.value}
169 onBlur={field.handleBlur}
170 onChange={(e) => field.handleChange(e.target.value)}
171 />
172 <p>{field.getMeta().errorMap.onChange}</p>
173 </div>
174 )}
175 />
176 </>
177 )
178 }
179​
180 const { getByTestId, getByText, queryByText } = render(<Comp />)
181 const input = getByTestId('fieldinput')
182 expect(queryByText(error)).not.toBeInTheDocument()
183 await user.type(input, 'other')
184 expect(getByText(error)).toBeInTheDocument()
185 })
186​
187 it('should validate on change and on blur', async () => {
188 type Person = {
189 firstName: string
190 lastName: string
191 }
192 const onChangeError = 'Please enter a different value (onChangeError)'
193 const onBlurError = 'Please enter a different value (onBlurError)'
194​
195 function Comp() {
196 const form = useForm({
197 defaultValues: {
198 firstName: '',
199 lastName: '',
200 } as Person,
201 })
202​
203 return (
204 <>
205 <form.Field
206 name="firstName"
207 defaultMeta={{ isTouched: true }}
208 validators={{
209 onChange: ({ value }) =>
210 value === 'other' ? onChangeError : undefined,
211 onBlur: ({ value }) =>
212 value === 'other' ? onBlurError : undefined,
213 }}
214 children={(field) => (
215 <div>
216 <input
217 data-testid="fieldinput"
218 name={field.name}
219 value={field.state.value}
220 onBlur={field.handleBlur}
221 onChange={(e) => field.handleChange(e.target.value)}
222 />
223 <p>{field.getMeta().errorMap.onChange}</p>
224 <p>{field.getMeta().errorMap.onBlur}</p>
225 </div>
226 )}
227 />
228 </>
229 )
230 }
231​
232 const { getByTestId, getByText, queryByText } = render(<Comp />)
233 const input = getByTestId('fieldinput')
234 expect(queryByText(onChangeError)).not.toBeInTheDocument()
235 expect(queryByText(onBlurError)).not.toBeInTheDocument()
236 await user.type(input, 'other')
237 expect(getByText(onChangeError)).toBeInTheDocument()
238 await user.click(document.body)
239 expect(queryByText(onBlurError)).toBeInTheDocument()
240 })
241​
242 it('should validate async on change', async () => {
243 type Person = {
244 firstName: string
245 lastName: string
246 }
247 const error = 'Please enter a different value'
248​
249 function Comp() {
250 const form = useForm({
251 defaultValues: {
252 firstName: '',
253 lastName: '',
254 } as Person,
255 })
256​
257 return (
258 <>
259 <form.Field
260 name="firstName"
261 defaultMeta={{ isTouched: true }}
262 validators={{
263 onChangeAsync: async () => {
264 await sleep(10)
265 return error
266 },
267 }}
268 children={(field) => (
269 <div>
270 <input
271 data-testid="fieldinput"
272 name={field.name}
273 value={field.state.value}
274 onBlur={field.handleBlur}
275 onChange={(e) => field.handleChange(e.target.value)}
276 />
277 <p>{field.getMeta().errorMap.onChange}</p>
278 </div>
279 )}
280 />
281 </>
282 )
283 }
284​
285 const { getByTestId, getByText, queryByText } = render(<Comp />)
286 const input = getByTestId('fieldinput')
287 expect(queryByText(error)).not.toBeInTheDocument()
288 await user.type(input, 'other')
289 await waitFor(() => getByText(error))
290 expect(getByText(error)).toBeInTheDocument()
291 })
292​
293 it('should validate async on change and async on blur', async () => {
294 type Person = {
295 firstName: string
296 lastName: string
297 }
298 const onChangeError = 'Please enter a different value (onChangeError)'
299 const onBlurError = 'Please enter a different value (onBlurError)'
300​
301 function Comp() {
302 const form = useForm({
303 defaultValues: {
304 firstName: '',
305 lastName: '',
306 } as Person,
307 })
308​
309 return (
310 <>
311 <form.Field
312 name="firstName"
313 defaultMeta={{ isTouched: true }}
314 validators={{
315 onChangeAsync: async () => {
316 await sleep(10)
317 return onChangeError
318 },
319 onBlurAsync: async () => {
320 await sleep(10)
321 return onBlurError
322 },
323 }}
324 children={(field) => (
325 <div>
326 <input
327 data-testid="fieldinput"
328 name={field.name}
329 value={field.state.value}
330 onBlur={field.handleBlur}
331 onChange={(e) => field.handleChange(e.target.value)}
332 />
333 <p>{field.getMeta().errorMap.onChange}</p>
334 <p>{field.getMeta().errorMap.onBlur}</p>
335 </div>
336 )}
337 />
338 </>
339 )
340 }
341​
342 const { getByTestId, getByText, queryByText } = render(<Comp />)
343 const input = getByTestId('fieldinput')
344​
345 expect(queryByText(onChangeError)).not.toBeInTheDocument()
346 expect(queryByText(onBlurError)).not.toBeInTheDocument()
347 await user.type(input, 'other')
348 await waitFor(() => getByText(onChangeError))
349 expect(getByText(onChangeError)).toBeInTheDocument()
350 await user.click(document.body)
351 await waitFor(() => getByText(onBlurError))
352 expect(getByText(onBlurError)).toBeInTheDocument()
353 })
354​
355 it('should validate async on change with debounce', async () => {
356 type Person = {
357 firstName: string
358 lastName: string
359 }
360 const mockFn = vi.fn()
361 const error = 'Please enter a different value'
362​
363 function Comp() {
364 const form = useForm({
365 defaultValues: {
366 firstName: '',
367 lastName: '',
368 } as Person,
369 })
370​
371 return (
372 <>
373 <form.Field
374 name="firstName"
375 defaultMeta={{ isTouched: true }}
376 validators={{
377 onChangeAsyncDebounceMs: 100,
378 onChangeAsync: async () => {
379 mockFn()
380 await sleep(10)
381 return error
382 },
383 }}
384 children={(field) => (
385 <div>
386 <input
387 data-testid="fieldinput"
388 name={field.name}
389 value={field.state.value}
390 onBlur={field.handleBlur}
391 onChange={(e) => field.handleChange(e.target.value)}
392 />
393 <p>{field.getMeta().errors}</p>
394 </div>
395 )}
396 />
397 </>
398 )
399 }
400​
401 const { getByTestId, getByText } = render(<Comp />)
402 const input = getByTestId('fieldinput')
403 await user.type(input, 'other')
404 // mockFn will have been called 5 times without onChangeAsyncDebounceMs
405 expect(mockFn).toHaveBeenCalledTimes(0)
406 await waitFor(() => getByText(error))
407 expect(getByText(error)).toBeInTheDocument()
408 })
409​
410 it('should handle strict mode properly with conditional fields', async () => {
411 function FieldInfo({ field }: { field: FieldApi<any, any> }) {
412 return (
413 <>
414 {field.state.meta.isTouched && field.state.meta.errors.length ? (
415 <em>{field.state.meta.errors.join(',')}</em>
416 ) : null}
417 {field.state.meta.isValidating ? 'Validating...' : null}
418 </>
419 )
420 }
421​
422 function Comp() {
423 const [showField, setShowField] = React.useState(true)
424​
425 const form = useForm({
426 defaultValues: {
427 firstName: '',
428 lastName: '',
429 },
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 = useStore(form.store, (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})