From 524b9efb73189f7fc028329872e8f80fe1a2cb4b Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Mon, 5 Aug 2024 03:50:39 +0300 Subject: [PATCH] test: add validation tests --- packages/core/src/form/useForm.spec.ts | 74 +++++++++++++++++++++++- packages/core/src/form/useFormActions.ts | 12 +++- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/packages/core/src/form/useForm.spec.ts b/packages/core/src/form/useForm.spec.ts index 9290d295..0c4f84f4 100644 --- a/packages/core/src/form/useForm.spec.ts +++ b/packages/core/src/form/useForm.spec.ts @@ -488,7 +488,7 @@ describe('validation', () => { expect(handler).toHaveBeenCalledOnce(); }); - test('type schema clears errors on successful submission', async () => { + test('typed schema clears errors on successful submission', async () => { const handler = vi.fn(); const input = ref(); const schema: TypedSchema = { @@ -530,7 +530,7 @@ describe('validation', () => { expect(screen.getByTestId('form-err').textContent).toBe(''); }); - test('type schema parses values which is used on submission', async () => { + test('typed schema parses values which is used on submission', async () => { const handler = vi.fn(); const input = ref(); const schema: TypedSchema = { @@ -572,4 +572,74 @@ describe('validation', () => { expect(handler).toHaveBeenCalledOnce(); expect(handler).toHaveBeenLastCalledWith({ test: true, foo: 'bar' }); }); + + test('typed schema is executed on form init', async () => { + const schema: TypedSchema = { + async parse() { + return { + errors: [{ path: 'test', errors: ['error'] }], + }; + }, + }; + + await render({ + setup() { + const { getError } = useForm({ + schema, + }); + + return { getError }; + }, + template: ` + {{ getError('test') }} + `, + }); + + await nextTick(); + expect(screen.getByTestId('form-err').textContent).toBe('error'); + }); + + test('form reset clears errors', async () => { + const schema: TypedSchema<{ test: string }> = { + async parse() { + return { + errors: [{ path: 'test', errors: ['error'] }], + }; + }, + }; + + const { reset, getError } = await renderSetup(() => { + return useForm<{ test: string }>({ + schema, + }); + }); + + await nextTick(); + expect(getError('test')).toBe('error'); + await reset(); + expect(getError('test')).toBeUndefined(); + }); + + test('form reset can revalidate', async () => { + let wasReset = false; + const schema: TypedSchema<{ test: string }> = { + async parse() { + return { + errors: [{ path: 'test', errors: wasReset ? ['reset'] : ['error'] }], + }; + }, + }; + + const { reset, getError } = await renderSetup(() => { + return useForm<{ test: string }>({ + schema, + }); + }); + + await nextTick(); + expect(getError('test')).toBe('error'); + wasReset = true; + await reset({ revalidate: true }); + expect(getError('test')).toBe('reset'); + }); }); diff --git a/packages/core/src/form/useFormActions.ts b/packages/core/src/form/useFormActions.ts index 6ec6cc6f..f9ba2320 100644 --- a/packages/core/src/form/useFormActions.ts +++ b/packages/core/src/form/useFormActions.ts @@ -8,6 +8,7 @@ import { unsetPath } from '../utils/path'; export interface ResetState { values: Partial; touched: Partial>; + revalidate?: boolean; } export interface FormActionsOptions { @@ -88,7 +89,7 @@ export function useFormActions>, opts?: SetValueOptions) { + async function reset(state?: Partial>, opts?: SetValueOptions) { if (state?.values) { form.setInitialValues(state.values, opts); } @@ -99,7 +100,14 @@ export function useFormActions