@@ -3981,95 +3981,75 @@ it('should accept formId and return it', () => {
3981
3981
expect ( form . formId ) . toEqual ( 'age' )
3982
3982
} )
3983
3983
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 ( )
3986
3986
3987
3987
const form = new FormApi ( {
3988
- defaultValues : { name : '' , email : '' } ,
3988
+ defaultValues : { name : '' } ,
3989
3989
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' } ) ,
3999
3991
} ,
3992
+ onSubmitInvalid : ( ) => onInvalidSpy ,
4000
3993
} )
4001
-
4002
3994
form . mount ( )
4003
3995
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 ( )
4006
3998
4007
3999
expect ( form . state . canSubmit ) . toBe ( false )
4008
4000
4009
4001
await form . handleSubmit ( )
4010
4002
4011
- expect ( onInvalid ) . toHaveBeenCalledTimes ( 1 )
4012
- expect ( onInvalid ) . toHaveBeenCalledWith (
4013
- { name : '' , email : '' } ,
4014
- expect . any ( Object ) ,
4015
- )
4003
+ expect ( onInvalidSpy ) . toHaveBeenCalledTimes ( 1 )
4016
4004
} )
4017
4005
4018
4006
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 ( )
4021
4009
4022
4010
const form = new FormApi ( {
4023
4011
defaultValues : { name : '' } ,
4024
4012
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
4033
4015
} ,
4016
+ onSubmitInvalid : ( ) => onInvalidSpy
4034
4017
} )
4035
-
4036
4018
form . mount ( )
4037
- new FieldApi ( { form, name : 'name' } ) . mount ( )
4019
+
4020
+ const field = new FieldApi ( { form, name : 'name' } )
4021
+ field . mount ( )
4038
4022
4039
4023
expect ( form . state . canSubmit ) . toBe ( false )
4040
4024
4041
4025
await form . handleSubmit ( )
4042
4026
4043
- expect ( onSubmitValidator ) . not . toHaveBeenCalled ( )
4044
- expect ( onInvalid ) . toHaveBeenCalledTimes ( 1 )
4027
+ expect ( onSubmitValidatorSpy ) . not . toHaveBeenCalled ( )
4028
+ expect ( onInvalidSpy ) . toHaveBeenCalledTimes ( 1 )
4045
4029
} )
4046
4030
4047
4031
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 ( )
4050
4034
4051
4035
const form = new FormApi ( {
4052
4036
defaultValues : { name : '' } ,
4053
4037
canSubmitWhenInvalid : true ,
4054
4038
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
4063
4041
} ,
4042
+ onSubmitInvalid : ( ) => onInvalidSpy
4064
4043
} )
4065
-
4066
4044
form . mount ( )
4067
- new FieldApi ( { form, name : 'name' } ) . mount ( )
4045
+
4046
+ const field = new FieldApi ( { form, name : 'name' } )
4047
+ field . mount ( )
4068
4048
4069
4049
expect ( form . state . canSubmit ) . toBe ( true )
4070
4050
4071
4051
await form . handleSubmit ( )
4072
4052
4073
- expect ( onSubmitValidator ) . toHaveBeenCalledTimes ( 1 )
4074
- expect ( onInvalid ) . toHaveBeenCalledTimes ( 1 )
4053
+ expect ( onSubmitValidatorSpy ) . toHaveBeenCalledTimes ( 1 )
4054
+ expect ( onInvalidSpy ) . toHaveBeenCalledTimes ( 1 )
4075
4055
} )
0 commit comments