Skip to content

Commit

Permalink
test: add missing test for handleSubmit return value
Browse files Browse the repository at this point in the history
  • Loading branch information
zoodirector committed Sep 26, 2024
1 parent 749ecca commit 2b7d966
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1983,4 +1983,56 @@ describe('form api', () => {
expect(form.state.canSubmit).toBe(true)
expect(passconfirmField.state.meta.errors.length).toBe(0)
})

it('handleSubmit should return the return value of onSubmit when valid', async () => {
const form = new FormApi({
defaultValues: {
firstName: '',
},
onSubmit: async ({value, formApi}) => {
// simulate a validation error on the server-side
if (value.firstName === 'maya') {
formApi.setFieldMeta('firstName', (meta) => ({...meta, errorMap: {...meta.errorMap, onServer: 'name is already taken'}}))
return
}
// if successful, return a value received from the server
return 23
}
})

const field = new FieldApi({
form,
name: 'firstName',
validators: {
onChange: ({ value }) =>
value.length > 0 ? undefined : 'first name is required',
},
})

field.mount()

const formInvalidResult = await form.handleSubmit()
expect(formInvalidResult).toEqual(undefined)
expect(form.state.isFieldsValid).toEqual(false)
expect(form.state.canSubmit).toEqual(false)
expect(form.state.fieldMeta['firstName'].errors).toEqual([
'first name is required',
])

field.setValue('peter')
const submitSucceedResult = await form.handleSubmit()
expect(submitSucceedResult).toEqual(23)
expect(form.state.isFieldsValid).toEqual(true)
expect(form.state.canSubmit).toEqual(true)

field.setValue('maya')
const submitFailureResult = await form.handleSubmit()
expect(submitFailureResult).toEqual(undefined)
expect(form.state.isFieldsValid).toEqual(false)
expect(form.state.canSubmit).toEqual(false)
expect(form.state.fieldMeta['firstName'].errors).toEqual([
'name is already taken',
])

})
})

0 comments on commit 2b7d966

Please sign in to comment.