Skip to content

Commit

Permalink
FIX: handle both sync and async function
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhya-spend committed Oct 18, 2024
1 parent cd1c539 commit 10b205f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ let defaultGetSnapshot = form => F.flattenObject(toJS(gatherFormValues(form)))

let defaultGetNestedSnapshot = form => F.unflattenObject(form.getSnapshot())

const handleSubmitErr = (state, err) => {
if (err instanceof ValidationError) {
state.errors = err.cause
}
throw err
}

export default ({
submit: configSubmit,
value = {},
Expand Down Expand Up @@ -182,12 +189,12 @@ export default ({
if (_.isEmpty(form.validate())) {
form.submit.state.error = null
try {
return configSubmit(form.getSnapshot(), form)
// Handle both sync and sync configSubmit
return Promise.resolve(configSubmit(form.getSnapshot(), form)).catch(
err => handleSubmitErr(state, err)
)
} catch (err) {
if (err instanceof ValidationError) {
state.errors = err.cause
}
throw err
handleSubmitErr(state, err)
}
}
throw 'Validation Error'
Expand Down
32 changes: 30 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ describe('submit()', () => {
expect(form.submit.state.error).toBe('Validation Error')
expect(form.submitError).toBe('Validation Error')
})
it('succeeds when validation and submit() run', async () => {
it('succeeds when validation and sync submit() run', async () => {
const submit = () => {
return 'submit run'
}
Expand All @@ -398,7 +398,16 @@ describe('submit()', () => {
expect(form.submit.state.status).toBe('succeeded')
expect(result).toBe('submit run')
})
it('has errors when submit throws with ValidationError', async () => {
it('succeeds when validation and async submit() run', async () => {
const submit = async () => {
return 'submit run'
}
form = Form({ fields: goodFields, value: goodValue, submit })
const result = await form.submit()
expect(form.submit.state.status).toBe('succeeded')
expect(result).toBe('submit run')
})
it('has errors when sync submit throws with ValidationError', async () => {
const submit = () => {
throw new ValidationError('My submit failed', {
'location.addresses.0.tenants.0': ['invalid format'],
Expand All @@ -417,4 +426,23 @@ describe('submit()', () => {
expect(form.submitError).toBe('My submit failed')
expect(result).toBeUndefined()
})
it('has errors when async submit throws with ValidationError', async () => {
const submit = async () => {
throw new ValidationError('My submit failed', {
'location.addresses.0.tenants.0': ['invalid format'],
})
}
form = Form({ fields: goodFields, value: goodValue, submit })
const result = await form.submit()
expect(form.submit.state.status).toBe('failed')
expect(form.submit.state.error.message).toBe('My submit failed')
expect(form.submit.state.error.cause).toEqual({
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.errors).toEqual({
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.submitError).toBe('My submit failed')
expect(result).toBeUndefined()
})
})

0 comments on commit 10b205f

Please sign in to comment.