@@ -3,6 +3,8 @@ import { initialState, useFormHandler } from '@/useFormHandler'
3
3
import { expect , it , describe } from 'vitest'
4
4
import { retry } from './testing-utils'
5
5
6
+ const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
7
+
6
8
describe ( 'useFormHandler()' , ( ) => {
7
9
describe ( 'register()' , ( ) => {
8
10
it ( 'should register a field' , ( ) => {
@@ -12,6 +14,7 @@ describe('useFormHandler()', () => {
12
14
expect ( field . onBlur ) . toBeDefined ( )
13
15
expect ( field . isDirty ) . toBeUndefined ( )
14
16
expect ( field . isTouched ) . toBeUndefined ( )
17
+ expect ( field . isValidating ) . toBeUndefined ( )
15
18
expect ( field . onClear ) . toBeDefined ( )
16
19
expect ( field . onChange ) . toBeDefined ( )
17
20
expect ( field . modelValue ) . toBe ( null )
@@ -37,11 +40,12 @@ describe('useFormHandler()', () => {
37
40
expect ( field . modelValue ) . toBeDefined ( )
38
41
expect ( field [ 'onUpdate:modelValue' ] ) . toBeDefined ( )
39
42
} )
40
- it ( 'should apply dirty and touched states when withDetails is specified' , ( ) => {
43
+ it ( 'should apply dirty, touched and validation states when withDetails is specified' , ( ) => {
41
44
const { register } = useFormHandler ( )
42
45
const field = register ( 'field' , { withDetails : true } )
43
46
expect ( field . isDirty ) . toBeDefined ( )
44
47
expect ( field . isTouched ) . toBeDefined ( )
48
+ expect ( field . isValidating ) . toBeDefined ( )
45
49
} )
46
50
it ( 'should apply default value' , ( ) => {
47
51
const { values, register } = useFormHandler ( )
@@ -142,6 +146,55 @@ describe('useFormHandler()', () => {
142
146
expect ( values . field ) . toBe ( null )
143
147
expect ( formState . isDirty ) . toBeTruthy ( )
144
148
} )
149
+ it ( 'should set validating state when async validation is in progress' , async ( ) => {
150
+ const { register, formState, triggerValidation } = useFormHandler ( )
151
+ register ( 'field' , {
152
+ validate : {
153
+ asyncValidation : async ( value : string ) => {
154
+ await sleep ( 200 )
155
+ return value === 'test' || 'error'
156
+ } ,
157
+ } ,
158
+ } )
159
+ triggerValidation ( 'field' )
160
+ await sleep ( 20 )
161
+ expect ( formState . isValidating ) . toBeTruthy ( )
162
+ expect ( formState . validating ) . toStrictEqual ( { field : true } )
163
+ await sleep ( 200 )
164
+ expect ( formState . isValidating ) . toBeFalsy ( )
165
+ expect ( formState . validating ) . toStrictEqual ( { } )
166
+ } )
167
+ it ( 'should correctly validate when async validation fails' , async ( ) => {
168
+ const { register, formState, triggerValidation } = useFormHandler ( )
169
+ register ( 'field' , {
170
+ validate : {
171
+ asyncValidation : async ( value : string ) => {
172
+ await sleep ( 200 )
173
+ return value === 'test' || 'error'
174
+ } ,
175
+ } ,
176
+ } )
177
+ triggerValidation ( 'field' )
178
+ await sleep ( 200 )
179
+ expect ( formState . errors . field ) . toBe ( 'error' )
180
+ expect ( formState . isValid ) . toBeFalsy ( )
181
+ expect ( formState . validating ) . toStrictEqual ( { } )
182
+ expect ( formState . isValidating ) . toBeFalsy ( )
183
+ } )
184
+ it ( 'should not set validating state for sync validation' , async ( ) => {
185
+ const { register, formState, triggerValidation } = useFormHandler ( )
186
+ register ( 'field' , {
187
+ validate : {
188
+ error : ( value : string ) => value === 'test' || 'error' ,
189
+ } ,
190
+ } )
191
+ triggerValidation ( 'field' )
192
+ await sleep ( 10 )
193
+ expect ( formState . errors . field ) . toBe ( 'error' )
194
+ expect ( formState . isValid ) . toBeFalsy ( )
195
+ expect ( formState . isValidating ) . toBeFalsy ( )
196
+ expect ( formState . validating ) . toStrictEqual ( { } )
197
+ } )
145
198
it ( 'should set an error programmatically' , async ( ) => {
146
199
const { formState, setError } = useFormHandler ( )
147
200
setError ( 'field' , 'some error' )
0 commit comments