Skip to content

Commit 81e8103

Browse files
authored
Returned a promise from handleSubmit when submission is async (#32)
1 parent ca3afc0 commit 81e8103

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ReactFinalForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default class ReactFinalForm extends React.PureComponent<Props, State> {
9191

9292
handleSubmit = (event: SyntheticEvent<HTMLFormElement>) => {
9393
event.preventDefault()
94-
this.form.submit()
94+
return this.form.submit()
9595
}
9696

9797
componentWillUnmount() {

src/ReactFinalForm.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Form from './ReactFinalForm'
44
import Field from './Field'
55

66
const onSubmitMock = values => {}
7+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
78

89
describe('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

Comments
 (0)