Skip to content

Commit 0ce7c1b

Browse files
committed
test: improve validation and submission handling tests for invalid form states
1 parent c8db95a commit 0ce7c1b

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

packages/form-core/tests/FormApi.spec.ts

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,95 +3981,75 @@ it('should accept formId and return it', () => {
39813981
expect(form.formId).toEqual('age')
39823982
})
39833983

3984-
it('should call onSubmitInvalid with current error state when canSubmit is false', async () => {
3985-
const onInvalid = vi.fn()
3984+
it('should call onSubmitInvalid when submitted with onMount error', async () => {
3985+
const onInvalidSpy = vi.fn()
39863986

39873987
const form = new FormApi({
3988-
defaultValues: { name: '', email: '' },
3988+
defaultValues: { name: '' },
39893989
validators: {
3990-
onMount: ({ value }) => {
3991-
const errors: Record<string, string> = {}
3992-
if (!value.name) errors.name = 'Name is required'
3993-
if (!value.email) errors.email = 'Email is required'
3994-
return Object.keys(errors).length > 0 ? errors : undefined
3995-
},
3996-
},
3997-
onSubmitInvalid: ({ value, formApi }) => {
3998-
onInvalid(value, formApi.state.errors)
3990+
onMount: () => ({name: 'Name is required'}),
39993991
},
3992+
onSubmitInvalid: () => onInvalidSpy,
40003993
})
4001-
40023994
form.mount()
40033995

4004-
new FieldApi({ form, name: 'name' }).mount()
4005-
new FieldApi({ form, name: 'email' }).mount()
3996+
const field = new FieldApi({ form, name: 'name' })
3997+
field.mount()
40063998

40073999
expect(form.state.canSubmit).toBe(false)
40084000

40094001
await form.handleSubmit()
40104002

4011-
expect(onInvalid).toHaveBeenCalledTimes(1)
4012-
expect(onInvalid).toHaveBeenCalledWith(
4013-
{ name: '', email: '' },
4014-
expect.any(Object),
4015-
)
4003+
expect(onInvalidSpy).toHaveBeenCalledTimes(1)
40164004
})
40174005

40184006
it('should not run submit validation when canSubmit is false', async () => {
4019-
const onSubmitValidator = vi.fn()
4020-
const onInvalid = vi.fn()
4007+
const onSubmitValidatorSpy = vi.fn().mockImplementation(() => 'Submit validation failed')
4008+
const onInvalidSpy = vi.fn()
40214009

40224010
const form = new FormApi({
40234011
defaultValues: { name: '' },
40244012
validators: {
4025-
onMount: ({ value }) => (!value.name ? 'Name required' : undefined),
4026-
onSubmit: ({ value }) => {
4027-
onSubmitValidator()
4028-
return !value.name ? 'Submit validation failed' : undefined
4029-
},
4030-
},
4031-
onSubmitInvalid: ({ value, formApi }) => {
4032-
onInvalid(value, formApi)
4013+
onMount: () => 'Name required',
4014+
onSubmit: () => onSubmitValidatorSpy
40334015
},
4016+
onSubmitInvalid: () => onInvalidSpy
40344017
})
4035-
40364018
form.mount()
4037-
new FieldApi({ form, name: 'name' }).mount()
4019+
4020+
const field = new FieldApi({ form, name: 'name' })
4021+
field.mount()
40384022

40394023
expect(form.state.canSubmit).toBe(false)
40404024

40414025
await form.handleSubmit()
40424026

4043-
expect(onSubmitValidator).not.toHaveBeenCalled()
4044-
expect(onInvalid).toHaveBeenCalledTimes(1)
4027+
expect(onSubmitValidatorSpy).not.toHaveBeenCalled()
4028+
expect(onInvalidSpy).toHaveBeenCalledTimes(1)
40454029
})
40464030

40474031
it('should respect canSubmitWhenInvalid option and run validation even when canSubmit is false', async () => {
4048-
const onSubmitValidator = vi.fn()
4049-
const onInvalid = vi.fn()
4032+
const onSubmitValidatorSpy = vi.fn().mockImplementation(() => 'Submit validation failed')
4033+
const onInvalidSpy = vi.fn()
40504034

40514035
const form = new FormApi({
40524036
defaultValues: { name: '' },
40534037
canSubmitWhenInvalid: true,
40544038
validators: {
4055-
onMount: ({ value }) => (!value.name ? 'Name required' : undefined),
4056-
onSubmit: ({ value }) => {
4057-
onSubmitValidator()
4058-
return !value.name ? 'Submit validation failed' : undefined
4059-
},
4060-
},
4061-
onSubmitInvalid: ({ value, formApi }) => {
4062-
onInvalid(value, formApi)
4039+
onMount: () => 'Name required',
4040+
onSubmit: () => onSubmitValidatorSpy
40634041
},
4042+
onSubmitInvalid: () => onInvalidSpy
40644043
})
4065-
40664044
form.mount()
4067-
new FieldApi({ form, name: 'name' }).mount()
4045+
4046+
const field = new FieldApi({ form, name: 'name' })
4047+
field.mount()
40684048

40694049
expect(form.state.canSubmit).toBe(true)
40704050

40714051
await form.handleSubmit()
40724052

4073-
expect(onSubmitValidator).toHaveBeenCalledTimes(1)
4074-
expect(onInvalid).toHaveBeenCalledTimes(1)
4053+
expect(onSubmitValidatorSpy).toHaveBeenCalledTimes(1)
4054+
expect(onInvalidSpy).toHaveBeenCalledTimes(1)
40754055
})

0 commit comments

Comments
 (0)