1
1
import { initialState , useFormHandler } from '../useFormHandler'
2
2
import { expect , it , describe } from 'vitest'
3
3
4
- describe ( 'Form handler testing ' , ( ) => {
5
- it ( 'Initial form state and values' , ( ) => {
4
+ describe ( 'useFormHandler() ' , ( ) => {
5
+ it ( 'should have correct initial state and values' , ( ) => {
6
6
const { values, formState } = useFormHandler ( )
7
7
expect ( values ) . toStrictEqual ( { } )
8
8
expect ( formState ) . toStrictEqual ( { ...initialState ( ) } )
9
9
} )
10
- it ( 'Initial values should be applied without ' , ( ) => {
10
+ it ( 'should apply initialValues when specified ' , ( ) => {
11
11
const initialValues = {
12
12
field : 'test' ,
13
13
}
14
14
const { values } = useFormHandler ( { initialValues } )
15
15
expect ( values ) . toStrictEqual ( initialValues )
16
16
} )
17
- it ( 'Setting a value programmatically' , async ( ) => {
17
+ it ( 'should set a value programmatically' , async ( ) => {
18
18
const { values, setValue, formState } = useFormHandler ( )
19
19
await setValue ( 'field' , 'oneTwoThree' )
20
20
expect ( values . field ) . toBe ( 'oneTwoThree' )
21
21
expect ( formState . isDirty ) . toBeTruthy ( )
22
22
} )
23
- it ( 'Clearing a field programmatically ' , async ( ) => {
23
+ it ( 'should clear a field' , async ( ) => {
24
24
const { register, values, setValue, formState, clearField } =
25
25
useFormHandler ( )
26
26
register ( 'field' )
@@ -31,7 +31,7 @@ describe('Form handler testing', () => {
31
31
expect ( values . field ) . toBe ( null )
32
32
expect ( formState . isDirty ) . toBeFalsy ( )
33
33
} )
34
- it ( 'Clearing an initialized field leaves it dirty ' , async ( ) => {
34
+ it ( 'should leave an initialized field dirty when clearing ' , async ( ) => {
35
35
const { register, values, formState, clearField } = useFormHandler ( {
36
36
initialValues : { field : 'value' } ,
37
37
} )
@@ -42,13 +42,13 @@ describe('Form handler testing', () => {
42
42
expect ( values . field ) . toBe ( null )
43
43
expect ( formState . isDirty ) . toBeTruthy ( )
44
44
} )
45
- it ( 'Setting an error programmatically' , async ( ) => {
45
+ it ( 'should set an error programmatically' , async ( ) => {
46
46
const { formState, setError } = useFormHandler ( )
47
47
setError ( 'field' , 'some error' )
48
48
expect ( formState . errors ) . toStrictEqual ( { field : 'some error' } )
49
49
expect ( formState . isValid ) . toBeFalsy ( )
50
50
} )
51
- it ( 'Clearing an error programmatically' , async ( ) => {
51
+ it ( 'should clear an error programmatically' , async ( ) => {
52
52
const { formState, setError, clearError } = useFormHandler ( )
53
53
const error = 'This field has an error'
54
54
setError ( 'field' , error )
@@ -57,7 +57,7 @@ describe('Form handler testing', () => {
57
57
expect ( formState . errors . field ) . toBeUndefined ( )
58
58
expect ( formState . isValid ) . toBeTruthy ( )
59
59
} )
60
- it ( 'Clearing all errors of the form programmatically' , async ( ) => {
60
+ it ( 'should clear all form errors programmatically' , async ( ) => {
61
61
const { formState, setError, clearError } = useFormHandler ( )
62
62
const error = 'some error'
63
63
setError ( 'field1' , error )
@@ -70,7 +70,7 @@ describe('Form handler testing', () => {
70
70
expect ( formState . errors . field2 ) . toBeUndefined ( )
71
71
expect ( formState . isValid ) . toBeTruthy ( )
72
72
} )
73
- it ( 'Resetting a field it back to its initial values and state ' , async ( ) => {
73
+ it ( 'should correctly reset a field ' , async ( ) => {
74
74
const { values, formState, resetField, setValue } = useFormHandler ( {
75
75
initialValues : { field : 'value' } ,
76
76
} )
@@ -83,7 +83,7 @@ describe('Form handler testing', () => {
83
83
expect ( values . field ) . toBe ( 'value' )
84
84
expect ( formState . isDirty ) . toBeFalsy ( )
85
85
} )
86
- it ( 'Expecting modifiedValues to work ' , async ( ) => {
86
+ it ( 'should return correct modifiedValues ' , async ( ) => {
87
87
const { modifiedValues, setValue } = useFormHandler ( {
88
88
initialValues : {
89
89
field : 'something' ,
@@ -92,4 +92,21 @@ describe('Form handler testing', () => {
92
92
await setValue ( 'field2' , 'another thing' )
93
93
expect ( modifiedValues ( ) ) . toStrictEqual ( { field2 : 'another thing' } )
94
94
} )
95
+ it ( 'should register field properly via build' , ( ) => {
96
+ const { build, values } = useFormHandler ( )
97
+ const form = build ( {
98
+ field : { } ,
99
+ } )
100
+
101
+ expect ( form . value . field . name ) . toBe ( 'field' )
102
+ expect ( form . value . field . error ) . toBeUndefined ( )
103
+ expect ( form . value . field . onBlur ) . toBeDefined ( )
104
+ expect ( form . value . field . isDirty ) . toBeUndefined ( )
105
+ expect ( form . value . field . isTouched ) . toBeUndefined ( )
106
+ expect ( form . value . field . onClear ) . toBeDefined ( )
107
+ expect ( form . value . field . onChange ) . toBeDefined ( )
108
+ expect ( form . value . field . modelValue ) . toBe ( null )
109
+ expect ( form . value . field [ 'onUpdate:modelValue' ] ) . toBeDefined ( )
110
+ expect ( values . field ) . toBe ( null )
111
+ } )
95
112
} )
0 commit comments