This repository was archived by the owner on Apr 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseFormHandler.test.ts
356 lines (348 loc) · 12.3 KB
/
useFormHandler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { ref } from '@vue/runtime-core'
import { initialState, useFormHandler } from '@/useFormHandler'
import { expect, it, describe } from 'vitest'
import { retry } from './testing-utils'
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
describe('useFormHandler()', () => {
describe('register()', () => {
it('should register a field', () => {
const { values, register } = useFormHandler()
const field = register('field')
expect(field.name).toBe('field')
expect(field.onBlur).toBeDefined()
expect(field.isDirty).toBeUndefined()
expect(field.isTouched).toBeUndefined()
expect(field.isValidating).toBeUndefined()
expect(field.onClear).toBeDefined()
expect(field.onChange).toBeDefined()
expect(field.modelValue).toBe(null)
expect(field['onUpdate:modelValue']).toBeDefined()
expect(values.field).toBe(null)
})
it('should apply native handlers by default', () => {
const { register } = useFormHandler()
const field = register('field')
expect(field.ref).toBeDefined()
expect(field.onChange).toBeDefined()
})
it('should apply native handlers when native is specified', () => {
const { register } = useFormHandler()
const field = register('field', { native: true })
expect(field.ref).toBeDefined()
expect(field.onChange).toBeDefined()
})
it('should not apply native handlers when native is set false', () => {
const { register } = useFormHandler()
const field = register('field', { native: false })
expect(field.onChange).toBeUndefined()
expect(field.modelValue).toBeDefined()
expect(field['onUpdate:modelValue']).toBeDefined()
})
it('should apply dirty, touched and validation states when withDetails is specified', () => {
const { register } = useFormHandler()
const field = register('field', { withDetails: true })
expect(field.isDirty).toBeDefined()
expect(field.isTouched).toBeDefined()
expect(field.isValidating).toBeDefined()
})
it('should apply default value', () => {
const { values, register } = useFormHandler()
register('field', { defaultValue: 'something' })
expect(values.field).toBe('something')
})
it('should trigger validation via handler', async () => {
const { values, register, formState } = useFormHandler()
const field = register('field', {
validate: {
error: (val) => val !== 'error' || 'Error with your field',
},
})
if (field['onUpdate:modelValue']) {
await field['onUpdate:modelValue']('error')
expect(values.field).toBe('error')
expect(formState.isValid).toBeFalsy()
}
})
it('should trigger validation via setter', async () => {
const { values, register, formState, setValue, triggerValidation } =
useFormHandler()
register('field', {
validate: {
error: (val) => val !== 'error' || 'Error with your field',
},
})
await setValue('field', 'error')
await triggerValidation('field')
expect(values.field).toBe('error')
expect(formState.isValid).toBeFalsy()
})
it('should trigger validation on dependent field', async () => {
const { register, formState, setValue } = useFormHandler()
register('field1', {
dependentFields: ['field2'],
})
register('field2', {
required: true,
})
await setValue('field1', 'error')
expect(formState.isValid).toBeFalsy()
})
it('should trigger validation on multiple dependent fields', async () => {
const { register, formState, setValue } = useFormHandler()
register('field1', {
dependentFields: ['field2', 'field3'],
})
register('field2', {
required: true,
})
register('field3', {
required: true,
})
await setValue('field1', 'error')
expect(formState.errors.field2).toBeDefined()
expect(formState.errors.field3).toBeDefined()
})
})
describe('handler functionality', () => {
it('should have correct initial state and values', () => {
const { values, formState } = useFormHandler()
expect(values).toStrictEqual({})
expect(formState).toStrictEqual({ ...initialState() })
})
it('should apply initialValues when specified', () => {
const initialValues = {
field: 'test',
}
const { values } = useFormHandler({ initialValues })
expect(values).toStrictEqual(initialValues)
})
it('should set a value programmatically', async () => {
const { values, setValue, formState } = useFormHandler()
await setValue('field', 'oneTwoThree')
expect(values.field).toBe('oneTwoThree')
expect(formState.isDirty).toBeTruthy()
})
it('should clear a field', async () => {
const { register, values, setValue, formState, clearField } =
useFormHandler()
register('field')
await setValue('field', 'oneTwoThree')
expect(values.field).toBe('oneTwoThree')
expect(formState.isDirty).toBeTruthy()
await clearField('field')
expect(values.field).toBe(null)
expect(formState.isDirty).toBeFalsy()
})
it('should leave an initialized field dirty when clearing', async () => {
const { register, values, formState, clearField } = useFormHandler({
initialValues: { field: 'value' },
})
register('field')
expect(values.field).toBe('value')
expect(formState.isDirty).toBeFalsy()
await clearField('field')
expect(values.field).toBe(null)
expect(formState.isDirty).toBeTruthy()
})
it('should set validating state when async validation is in progress', async () => {
const { register, formState, triggerValidation } = useFormHandler()
register('field', {
validate: {
asyncValidation: async (value: string) => {
await sleep(200)
return value === 'test' || 'error'
},
},
})
triggerValidation('field')
await sleep(20)
expect(formState.isValidating).toBeTruthy()
expect(formState.validating).toStrictEqual({ field: true })
await sleep(200)
expect(formState.isValidating).toBeFalsy()
expect(formState.validating).toStrictEqual({})
})
it('should correctly validate when async validation fails', async () => {
const { register, formState, triggerValidation } = useFormHandler()
register('field', {
validate: {
asyncValidation: async (value: string) => {
await sleep(200)
return value === 'test' || 'error'
},
},
})
triggerValidation('field')
await sleep(200)
expect(formState.errors.field).toBe('error')
expect(formState.isValid).toBeFalsy()
expect(formState.validating).toStrictEqual({})
expect(formState.isValidating).toBeFalsy()
})
it('should not set validating state for sync validation', async () => {
const { register, formState, triggerValidation } = useFormHandler()
register('field', {
validate: {
error: (value: string) => value === 'test' || 'error',
},
})
triggerValidation('field')
await sleep(10)
expect(formState.errors.field).toBe('error')
expect(formState.isValid).toBeFalsy()
expect(formState.isValidating).toBeFalsy()
expect(formState.validating).toStrictEqual({})
})
it('should set an error programmatically', async () => {
const { formState, setError } = useFormHandler()
setError('field', 'some error')
expect(formState.errors).toStrictEqual({ field: 'some error' })
expect(formState.isValid).toBeFalsy()
})
it('should clear an error programmatically', async () => {
const { formState, setError, clearError } = useFormHandler()
const error = 'This field has an error'
setError('field', error)
expect(formState.isValid).toBeFalsy()
clearError('field')
expect(formState.errors.field).toBeUndefined()
expect(formState.isValid).toBeTruthy()
})
it('should clear all form errors programmatically', async () => {
const { formState, setError, clearError } = useFormHandler()
const error = 'some error'
setError('field1', error)
setError('field2', error)
expect(formState.errors.field1).toBe(error)
expect(formState.errors.field2).toBe(error)
expect(formState.isValid).toBeFalsy()
clearError()
expect(formState.errors.field1).toBeUndefined()
expect(formState.errors.field2).toBeUndefined()
expect(formState.isValid).toBeTruthy()
})
it('should correctly reset a field', async () => {
const { values, formState, resetField, setValue } = useFormHandler({
initialValues: { field: 'value' },
})
expect(values.field).toBe('value')
expect(formState.isDirty).toBeFalsy()
await setValue('field', 'some other value')
expect(values.field).toBe('some other value')
expect(formState.isDirty).toBeTruthy()
resetField('field')
expect(values.field).toBe('value')
expect(formState.isDirty).toBeFalsy()
})
it('should return correct modifiedValues', async () => {
const { modifiedValues, setValue } = useFormHandler({
initialValues: {
field: 'something',
},
})
await setValue('field', 'another thing')
expect(modifiedValues.value).toStrictEqual({ field: 'another thing' })
})
it('should register field properly via build', () => {
const { build, values } = useFormHandler()
const form = build({
field: {},
})
expect(form.value.field.name).toBe('field')
expect(form.value.field.onBlur).toBeDefined()
expect(form.value.field.isDirty).toBeUndefined()
expect(form.value.field.isTouched).toBeUndefined()
expect(form.value.field.onClear).toBeDefined()
expect(form.value.field.onChange).toBeDefined()
expect(form.value.field.modelValue).toBe(null)
expect(form.value.field['onUpdate:modelValue']).toBeDefined()
expect(values.field).toBe(null)
})
it('should call success fn if no validation errors', async () => {
const { build, handleSubmit, formState } = useFormHandler()
build({
field: {},
})
const successFn = (form: any) => {
expect(form).toBeDefined()
}
await handleSubmit(successFn)
expect(formState.isValid).toBe(true)
})
it('should call error fn if present and validation errors', async () => {
let success = false
const { build, handleSubmit, formState, setError } = useFormHandler({
validationMode: 'onSubmit',
})
build({
field: {
required: true,
},
})
setError('field', 'this is an error')
const successFn = () => {
success = true
}
const errorFn = (errors: any) => {
success = false
}
await handleSubmit(successFn, errorFn)
expect(success).toBe(false)
expect(formState.isValid).toBe(false)
})
it('should use validate function if present', async () => {
let success = false
const { build, handleSubmit } = useFormHandler<{
field: string
}>({
validate: (values) => {
if (!values.field) {
return false
}
return true
},
})
build({
field: {},
})
const successFn = () => {
success = true
}
const errorFn = (errors: any) => {
success = false
}
await handleSubmit(successFn, errorFn)
expect(success).toBe(false)
})
it('should reset the form when initialValues are modified', async () => {
interface FormInterface {
field1: string | null
field2: string | null
}
const initialValues = ref<FormInterface>({
field1: null,
field2: null,
})
const { build, values, setValue } = useFormHandler<FormInterface>({
initialValues,
})
build({
field1: {},
field2: {},
})
expect(values).toEqual(initialValues.value)
setValue('field1', 'test')
expect(values.field1).toBe('test')
initialValues.value = {
...initialValues.value,
field2: 'test4',
}
retry(() =>
expect(values).toEqual({
...initialValues.value,
field2: 'test4',
})
)
})
})
})