@@ -3981,95 +3981,79 @@ 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
4008
+ . fn ( )
4009
+ . mockImplementation ( ( ) => 'Submit validation failed' )
4010
+ const onInvalidSpy = vi . fn ( )
4021
4011
4022
4012
const form = new FormApi ( {
4023
4013
defaultValues : { name : '' } ,
4024
4014
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 )
4015
+ onMount : ( ) => 'Name required' ,
4016
+ onSubmit : ( ) => onSubmitValidatorSpy ,
4033
4017
} ,
4018
+ onSubmitInvalid : ( ) => onInvalidSpy ( ) ,
4034
4019
} )
4035
-
4036
4020
form . mount ( )
4037
- new FieldApi ( { form, name : 'name' } ) . mount ( )
4021
+
4022
+ const field = new FieldApi ( { form, name : 'name' } )
4023
+ field . mount ( )
4038
4024
4039
4025
expect ( form . state . canSubmit ) . toBe ( false )
4040
4026
4041
4027
await form . handleSubmit ( )
4042
4028
4043
- expect ( onSubmitValidator ) . not . toHaveBeenCalled ( )
4044
- expect ( onInvalid ) . toHaveBeenCalledTimes ( 1 )
4029
+ expect ( onSubmitValidatorSpy ) . not . toHaveBeenCalled ( )
4030
+ expect ( onInvalidSpy ) . toHaveBeenCalledTimes ( 1 )
4045
4031
} )
4046
4032
4047
4033
it ( 'should respect canSubmitWhenInvalid option and run validation even when canSubmit is false' , async ( ) => {
4048
- const onSubmitValidator = vi . fn ( )
4049
- const onInvalid = vi . fn ( )
4034
+ const onSubmitValidatorSpy = vi
4035
+ . fn ( )
4036
+ . mockImplementation ( ( ) => 'Submit validation failed' )
4037
+ const onInvalidSpy = vi . fn ( )
4050
4038
4051
4039
const form = new FormApi ( {
4052
4040
defaultValues : { name : '' } ,
4053
4041
canSubmitWhenInvalid : true ,
4054
4042
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 )
4043
+ onMount : ( ) => 'Name required' ,
4044
+ onSubmit : ( ) => onSubmitValidatorSpy ( ) ,
4063
4045
} ,
4046
+ onSubmitInvalid : ( ) => onInvalidSpy ( ) ,
4064
4047
} )
4065
-
4066
4048
form . mount ( )
4067
- new FieldApi ( { form, name : 'name' } ) . mount ( )
4049
+
4050
+ const field = new FieldApi ( { form, name : 'name' } )
4051
+ field . mount ( )
4068
4052
4069
4053
expect ( form . state . canSubmit ) . toBe ( true )
4070
4054
4071
4055
await form . handleSubmit ( )
4072
4056
4073
- expect ( onSubmitValidator ) . toHaveBeenCalledTimes ( 1 )
4074
- expect ( onInvalid ) . toHaveBeenCalledTimes ( 1 )
4057
+ expect ( onSubmitValidatorSpy ) . toHaveBeenCalledTimes ( 1 )
4058
+ expect ( onInvalidSpy ) . toHaveBeenCalledTimes ( 1 )
4075
4059
} )
0 commit comments