@@ -7,6 +7,7 @@ import { Form, Field } from 'react-final-form'
7
7
import FieldArray from './FieldArray'
8
8
9
9
const onSubmitMock = values => { }
10
+ const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
10
11
11
12
describe ( 'FieldArray' , ( ) => {
12
13
afterEach ( cleanup )
@@ -626,4 +627,54 @@ describe('FieldArray', () => {
626
627
expect ( getByTestId ( 'arrayDirty' ) ) . toHaveTextContent ( 'Pristine' )
627
628
expect ( getByTestId ( 'names[0].dirty' ) ) . toHaveTextContent ( 'Pristine' )
628
629
} )
630
+
631
+ it ( 'should allow resetting the form in onSubmit' , async ( ) => {
632
+ // https://github.com/final-form/final-form/issues/26#issuecomment-497272119
633
+ const onSubmit = jest . fn ( ( values , form ) => {
634
+ expect ( values ) . toEqual ( { names : [ 'erikras' ] } )
635
+ return Promise . resolve ( ) . then ( ( ) => {
636
+ form . reset ( )
637
+ } )
638
+ } )
639
+ const { getByTestId, getByText } = render (
640
+ < Form
641
+ onSubmit = { onSubmit }
642
+ mutators = { arrayMutators }
643
+ subscription = { { values : true } }
644
+ >
645
+ { ( { handleSubmit, values } ) => (
646
+ < form onSubmit = { handleSubmit } data-testid = "form" >
647
+ < pre data-testid = "values" > { JSON . stringify ( values ) } </ pre >
648
+ < FieldArray
649
+ name = "names"
650
+ render = { ( { fields } ) => (
651
+ < div >
652
+ { fields . map ( field => (
653
+ < Field
654
+ name = { field }
655
+ key = { field }
656
+ component = "input"
657
+ data-testid = { field }
658
+ />
659
+ ) ) }
660
+ < button type = "button" onClick = { ( ) => fields . push ( 'erikras' ) } >
661
+ Add
662
+ </ button >
663
+ </ div >
664
+ ) }
665
+ />
666
+ </ form >
667
+ ) }
668
+ </ Form >
669
+ )
670
+ expect ( getByTestId ( 'values' ) ) . toHaveTextContent ( '' )
671
+ expect ( onSubmit ) . not . toHaveBeenCalled ( )
672
+ fireEvent . click ( getByText ( 'Add' ) )
673
+ expect ( getByTestId ( 'values' ) ) . toHaveTextContent ( '{"names":["erikras"]}' )
674
+ fireEvent . submit ( getByTestId ( 'form' ) )
675
+ await sleep ( 3 )
676
+ expect ( onSubmit ) . toHaveBeenCalled ( )
677
+ expect ( onSubmit ) . toHaveBeenCalledTimes ( 1 )
678
+ expect ( getByTestId ( 'values' ) ) . toHaveTextContent ( '' )
679
+ } )
629
680
} )
0 commit comments