@@ -4,6 +4,7 @@ import Form from './ReactFinalForm'
44import Field from './Field'
55
66const onSubmitMock = values => { }
7+ const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
78
89describe ( 'ReactFinalForm' , ( ) => {
910 it ( 'should render with render function' , ( ) => {
@@ -180,6 +181,37 @@ describe('ReactFinalForm', () => {
180181 expect ( onSubmit ) . toHaveBeenCalledWith ( { foo : 'bar' } )
181182 } )
182183
184+ it ( 'should return a promise from handleSubmit when submission is async' , async ( ) => {
185+ const onSubmit = jest . fn ( )
186+ let promise
187+ const dom = TestUtils . renderIntoDocument (
188+ < Form
189+ onSubmit = { async ( ) => {
190+ await sleep ( 2 )
191+ } }
192+ initialValues = { { foo : 'bar' } }
193+ >
194+ { ( { handleSubmit } ) => (
195+ < form
196+ onSubmit = { event => {
197+ promise = handleSubmit ( event )
198+ expect ( promise ) . not . toBeUndefined ( )
199+ expect ( typeof promise . then ) . toBe ( 'function' )
200+ } }
201+ >
202+ < Field name = "foo" component = "input" />
203+ < button type = "submit" > Submit</ button >
204+ </ form >
205+ ) }
206+ </ Form >
207+ )
208+ expect ( onSubmit ) . not . toHaveBeenCalled ( )
209+
210+ const form = TestUtils . findRenderedDOMComponentWithTag ( dom , 'form' )
211+ TestUtils . Simulate . submit ( form )
212+ return promise
213+ } )
214+
183215 it ( 'should respect validateOnBlur' , ( ) => {
184216 const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
185217 const validate = jest . fn ( values => {
0 commit comments