@teamnovu/kit-vue-forms 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/FormFieldWrapper.vue.d.ts +1 -1
- package/dist/composables/useField.d.ts +5 -2
- package/dist/composables/useFieldRegistry.d.ts +13 -4
- package/dist/composables/useFormData.d.ts +5 -0
- package/dist/index.js +370 -324
- package/dist/types/form.d.ts +3 -1
- package/docs/reference.md +0 -2
- package/package.json +1 -1
- package/src/components/FormFieldWrapper.vue +8 -0
- package/src/composables/useField.ts +9 -3
- package/src/composables/useFieldRegistry.ts +80 -14
- package/src/composables/useForm.ts +53 -10
- package/src/composables/useFormData.ts +1 -1
- package/src/composables/useValidation.ts +1 -1
- package/src/types/form.ts +23 -8
- package/src/utils/path.ts +7 -1
- package/tests/subform.test.ts +17 -0
- package/tests/useField.test.ts +17 -0
- package/tests/useForm.test.ts +313 -201
- package/tests/useValidation.test.ts +73 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
2
|
import { nextTick, ref } from 'vue'
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
import { useForm } from '../src/composables/useForm'
|
|
@@ -279,4 +279,76 @@ describe('useValidation', () => {
|
|
|
279
279
|
expect(validation.isValidated.value).toBe(false)
|
|
280
280
|
expect(validation.errors.value).toEqual(SuccessValidationResult.errors)
|
|
281
281
|
})
|
|
282
|
+
|
|
283
|
+
it('should validate the form on blur if configured', async () => {
|
|
284
|
+
const schema = z.object({
|
|
285
|
+
name: z.string().min(2),
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
const initialData = { name: 'A' }
|
|
289
|
+
const form = useForm({
|
|
290
|
+
initialData,
|
|
291
|
+
schema,
|
|
292
|
+
validationStrategy: 'onTouch',
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
const nameField = form.getField('name')
|
|
296
|
+
|
|
297
|
+
expect(form.isValidated.value).toBe(false)
|
|
298
|
+
|
|
299
|
+
// Simulate blur event
|
|
300
|
+
nameField.onBlur()
|
|
301
|
+
|
|
302
|
+
// onBlur is not async but the validation runs async
|
|
303
|
+
await delay()
|
|
304
|
+
|
|
305
|
+
expect(form.isValidated.value).toBe(true)
|
|
306
|
+
expect(form.isValid.value).toBe(false)
|
|
307
|
+
expect(form.errors.value.propertyErrors.name).toHaveLength(1)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
it('should validate the form on form open if configured', async () => {
|
|
311
|
+
const schema = z.object({
|
|
312
|
+
name: z.string().min(2),
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
const initialData = { name: 'A' }
|
|
316
|
+
const form = useForm({
|
|
317
|
+
initialData,
|
|
318
|
+
schema,
|
|
319
|
+
validationStrategy: 'onFormOpen',
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
form.getField('name')
|
|
323
|
+
|
|
324
|
+
await delay()
|
|
325
|
+
|
|
326
|
+
expect(form.isValidated.value).toBe(true)
|
|
327
|
+
expect(form.isValid.value).toBe(false)
|
|
328
|
+
expect(form.errors.value.propertyErrors.name).toHaveLength(1)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
it('should not the form on submit if validation strategy is "none"', async () => {
|
|
332
|
+
const schema = z.object({
|
|
333
|
+
name: z.string().min(2),
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
const initialData = { name: 'A' }
|
|
337
|
+
const form = useForm({
|
|
338
|
+
initialData,
|
|
339
|
+
schema,
|
|
340
|
+
validationStrategy: 'none',
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
form.getField('name')
|
|
344
|
+
|
|
345
|
+
const cb = vi.fn()
|
|
346
|
+
|
|
347
|
+
const submitHandler = form.submitHandler(cb)
|
|
348
|
+
|
|
349
|
+
await submitHandler(new SubmitEvent('submit'))
|
|
350
|
+
|
|
351
|
+
expect(form.isValidated.value).toBe(false)
|
|
352
|
+
expect(form.isValid.value).toBe(true)
|
|
353
|
+
})
|
|
282
354
|
})
|