Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit c400e74

Browse files
committed
feat: add tests for validation states
1 parent 7f7f4d9 commit c400e74

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

test/useFormHandler.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { initialState, useFormHandler } from '@/useFormHandler'
33
import { expect, it, describe } from 'vitest'
44
import { retry } from './testing-utils'
55

6+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
7+
68
describe('useFormHandler()', () => {
79
describe('register()', () => {
810
it('should register a field', () => {
@@ -12,6 +14,7 @@ describe('useFormHandler()', () => {
1214
expect(field.onBlur).toBeDefined()
1315
expect(field.isDirty).toBeUndefined()
1416
expect(field.isTouched).toBeUndefined()
17+
expect(field.isValidating).toBeUndefined()
1518
expect(field.onClear).toBeDefined()
1619
expect(field.onChange).toBeDefined()
1720
expect(field.modelValue).toBe(null)
@@ -37,11 +40,12 @@ describe('useFormHandler()', () => {
3740
expect(field.modelValue).toBeDefined()
3841
expect(field['onUpdate:modelValue']).toBeDefined()
3942
})
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', () => {
4144
const { register } = useFormHandler()
4245
const field = register('field', { withDetails: true })
4346
expect(field.isDirty).toBeDefined()
4447
expect(field.isTouched).toBeDefined()
48+
expect(field.isValidating).toBeDefined()
4549
})
4650
it('should apply default value', () => {
4751
const { values, register } = useFormHandler()
@@ -142,6 +146,55 @@ describe('useFormHandler()', () => {
142146
expect(values.field).toBe(null)
143147
expect(formState.isDirty).toBeTruthy()
144148
})
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+
})
145198
it('should set an error programmatically', async () => {
146199
const { formState, setError } = useFormHandler()
147200
setError('field', 'some error')

0 commit comments

Comments
 (0)