@@ -6,7 +6,7 @@ import { ErrorBoundary, Toggle, wrapWith } from './testUtils'
66import { Form , Field } from 'react-final-form'
77import { FieldArray , version } from '.'
88
9- const onSubmitMock = values => { }
9+ const onSubmitMock = values => { }
1010const timeout = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
1111async function sleep ( ms ) {
1212 await act ( async ( ) => {
@@ -22,7 +22,7 @@ describe('FieldArray', () => {
2222 } )
2323
2424 it ( 'should warn if not used inside a form' , ( ) => {
25- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
25+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
2626 const errorSpy = jest . fn ( )
2727 render (
2828 < ErrorBoundary spy = { errorSpy } >
@@ -38,7 +38,7 @@ describe('FieldArray', () => {
3838 } )
3939
4040 it ( 'should warn if no render strategy is provided' , ( ) => {
41- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
41+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
4242 const errorSpy = jest . fn ( )
4343 render (
4444 < ErrorBoundary spy = { errorSpy } >
@@ -58,7 +58,7 @@ describe('FieldArray', () => {
5858 } )
5959
6060 it ( 'should warn if no array mutators provided' , ( ) => {
61- jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
61+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
6262 const errorSpy = jest . fn ( )
6363 render (
6464 < ErrorBoundary spy = { errorSpy } >
@@ -451,6 +451,58 @@ describe('FieldArray', () => {
451451 expect ( queryByTestId ( 'names[1]' ) ) . not . toBe ( null )
452452 } )
453453
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+
454506 it ( 'should not re-render Field when subscription is empty object' , ( ) => {
455507 const nameFieldRender = jest . fn ( )
456508 const surnameFieldRender = jest . fn ( )
0 commit comments