Skip to content

Commit

Permalink
test: add test for value returned by validateField (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini-ghost authored Mar 4, 2024
1 parent 25ec962 commit 1bd2de1
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions packages/core/tests/composables/useForm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from '@vue/test-utils';
import { flushPromises, mount } from '@vue/test-utils';
import { describe, expect, it, vi } from 'vitest';
import { defineComponent, nextTick, ref } from 'vue';
import { defineComponent, h, nextTick, ref, Suspense } from 'vue';

import { useForm } from '../../src';

Expand All @@ -15,6 +15,24 @@ const setup = (setup: () => unknown) => {
return mount(Comp);
};

const setupSuspense = async (setup: () => unknown) => {
const Comp = defineComponent({
setup,
template: `<div />`,
});

const Root = defineComponent({
render() {
return h(Suspense, null, {
default: h(Comp),
});
},
});

await flushPromises();
return mount(Root);
};

const sleep = (ms?: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
Expand Down Expand Up @@ -1216,4 +1234,26 @@ describe('useForm', () => {
});
});
});

it('Should return an error if the validateField function fails', async () => {
await setupSuspense(async () => {
const ERROR_MESSAGE = 'name is required';
const { register, validateField } = useForm({
initialValues: {
name: '',
},
onSubmit: noop,
});

register('name', {
validate() {
return ERROR_MESSAGE;
},
});

const error = await validateField('name');

expect(error).toEqual(ERROR_MESSAGE);
});
});
});

0 comments on commit 1bd2de1

Please sign in to comment.