@@ -6,7 +6,7 @@ import { ErrorBoundary, Toggle, wrapWith } from './testUtils'
6
6
import { Form , Field } from 'react-final-form'
7
7
import { FieldArray , version } from '.'
8
8
9
- const onSubmitMock = values => { }
9
+ const onSubmitMock = values => { }
10
10
const timeout = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
11
11
async function sleep ( ms ) {
12
12
await act ( async ( ) => {
@@ -22,7 +22,7 @@ describe('FieldArray', () => {
22
22
} )
23
23
24
24
it ( 'should warn if not used inside a form' , ( ) => {
25
- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
25
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
26
26
const errorSpy = jest . fn ( )
27
27
render (
28
28
< ErrorBoundary spy = { errorSpy } >
@@ -38,7 +38,7 @@ describe('FieldArray', () => {
38
38
} )
39
39
40
40
it ( 'should warn if no render strategy is provided' , ( ) => {
41
- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
41
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
42
42
const errorSpy = jest . fn ( )
43
43
render (
44
44
< ErrorBoundary spy = { errorSpy } >
@@ -58,7 +58,7 @@ describe('FieldArray', () => {
58
58
} )
59
59
60
60
it ( 'should warn if no array mutators provided' , ( ) => {
61
- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
61
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
62
62
const errorSpy = jest . fn ( )
63
63
render (
64
64
< ErrorBoundary spy = { errorSpy } >
@@ -451,6 +451,58 @@ describe('FieldArray', () => {
451
451
expect ( queryByTestId ( 'names[1]' ) ) . not . toBe ( null )
452
452
} )
453
453
454
+ it ( 'should push a new value to right place after changing name' , ( ) => {
455
+ const { getByText, queryByTestId } = render (
456
+ < Toggle >
457
+ { isCats => (
458
+ < Form onSubmit = { onSubmitMock } mutators = { arrayMutators } subscription = { { } } >
459
+ { ( ) => (
460
+ < form >
461
+ < FieldArray name = { isCats ? 'cats' : 'dogs' } >
462
+ { ( { fields } ) => (
463
+ < div >
464
+ { fields . map ( field => (
465
+ < Field
466
+ name = { field }
467
+ key = { field }
468
+ component = "input"
469
+ data-testid = { field }
470
+ />
471
+ ) ) }
472
+ < button type = "button" onClick = { ( ) => fields . push ( { } ) } >
473
+ Add
474
+ </ button >
475
+ </ div >
476
+ ) }
477
+ </ FieldArray >
478
+ </ form >
479
+ ) }
480
+ </ Form >
481
+ ) }
482
+ </ Toggle >
483
+ )
484
+ expect ( queryByTestId ( 'dogs[0]' ) ) . toBe ( null )
485
+ expect ( queryByTestId ( 'dogs[1]' ) ) . toBe ( null )
486
+
487
+ // push
488
+ fireEvent . click ( getByText ( 'Add' ) )
489
+
490
+ expect ( queryByTestId ( 'dogs[0]' ) ) . not . toBe ( null )
491
+ expect ( queryByTestId ( 'dogs[1]' ) ) . toBe ( null )
492
+
493
+ // change name
494
+ fireEvent . click ( getByText ( 'Toggle' ) )
495
+
496
+ expect ( queryByTestId ( 'cats[0]' ) ) . toBe ( null )
497
+ expect ( queryByTestId ( 'cats[1]' ) ) . toBe ( null )
498
+
499
+ // push
500
+ fireEvent . click ( getByText ( 'Add' ) )
501
+
502
+ expect ( queryByTestId ( 'cats[0]' ) ) . not . toBe ( null )
503
+ expect ( queryByTestId ( 'cats[1]' ) ) . toBe ( null )
504
+ } )
505
+
454
506
it ( 'should not re-render Field when subscription is empty object' , ( ) => {
455
507
const nameFieldRender = jest . fn ( )
456
508
const surnameFieldRender = jest . fn ( )
0 commit comments