Skip to content

Commit

Permalink
test: add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 5, 2024
1 parent 6145f48 commit 524b9ef
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
74 changes: 72 additions & 2 deletions packages/core/src/form/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>();
const schema: TypedSchema<object, object> = {
Expand Down Expand Up @@ -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<HTMLInputElement>();
const schema: TypedSchema<object, { test: true; foo: string }> = {
Expand Down Expand Up @@ -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<object, object> = {
async parse() {
return {
errors: [{ path: 'test', errors: ['error'] }],
};
},
};

await render({
setup() {
const { getError } = useForm({
schema,
});

return { getError };
},
template: `
<span data-testid="form-err">{{ getError('test') }}</span>
`,
});

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');
});
});
12 changes: 10 additions & 2 deletions packages/core/src/form/useFormActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { unsetPath } from '../utils/path';
export interface ResetState<TForm extends FormObject> {
values: Partial<TForm>;
touched: Partial<TouchedSchema<TForm>>;
revalidate?: boolean;
}

export interface FormActionsOptions<TForm extends FormObject = FormObject, TOutput extends FormObject = TForm> {
Expand Down Expand Up @@ -88,7 +89,7 @@ export function useFormActions<TForm extends FormObject = FormObject, TOutput ex
}
}

function reset(state?: Partial<ResetState<TForm>>, opts?: SetValueOptions) {
async function reset(state?: Partial<ResetState<TForm>>, opts?: SetValueOptions) {
if (state?.values) {
form.setInitialValues(state.values, opts);
}
Expand All @@ -99,7 +100,14 @@ export function useFormActions<TForm extends FormObject = FormObject, TOutput ex

form.revertValues();
form.revertTouched();
validate();
if (state?.revalidate) {
await validate();
return;
}

form.clearErrors();

return Promise.resolve();
}

return {
Expand Down

0 comments on commit 524b9ef

Please sign in to comment.