Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterlms committed Jan 20, 2024
1 parent f6a1504 commit dbe75a4
Showing 1 changed file with 170 additions and 1 deletion.
171 changes: 170 additions & 1 deletion test/useForm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,80 @@ describe('useForm', () => {
})
})

describe('set a value of a field', () => {
it('should set a value of a field with `onUpdate:modelValue`', () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name')

name['onUpdate:modelValue']('John')

expect(name.modelValue).toEqual('John')

expect(form.state).toEqual({
name: 'John',
})
})

it('should set a value of a field with `setValue`', () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name')

name.setValue('John')

expect(name.modelValue).toEqual('John')

expect(form.state).toEqual({
name: 'John',
})
})

it('should set a value of a field with `form.setValues`', () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name')

form.setValues({
name: 'John',
})

expect(name.modelValue).toEqual('John')

expect(form.state).toEqual({
name: 'John',
})
})

it('should set a nested value of a field with `form.setValues`', () => {
const { form } = useForm({
schema: objectSchema,
})

const b = form.register('a.b')

form.setValues({
a: {
b: 'John',
},
})

expect(b.modelValue).toEqual('John')

expect(form.state).toEqual({
a: {
b: 'John',
},
})
})
})

describe('isDirty', () => {
it('should be false by default', () => {
const { form } = useForm({
Expand Down Expand Up @@ -377,7 +451,7 @@ describe('useForm', () => {
expect(name.isTouched).toEqual(false)
})

it('should be true when field is blurred', () => {
it('should be true when `onBlur` is called', () => {
const { form } = useForm({
schema: basicSchema,
})
Expand All @@ -390,12 +464,49 @@ describe('useForm', () => {
})
})

describe('isChanged', () => {
it('should be false by default', () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name')

expect(name.isChanged).toEqual(false)
})

it('should be true when `onChange` is called', () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name')

name.onChange()

expect(name.isChanged).toEqual(true)
})
})

describe('errors', () => {
it('should not have any errors when all fields are valid', async () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name', 'John')

await sleep(0)

expect(form.errors).toEqual({})
expect(name.errors).toBeUndefined()
})

it('should have errors when a field is invalid', async () => {
const { form } = useForm({
schema: basicSchema,
})

const name = form.register('name', 'Jon')

await sleep(0)
Expand All @@ -404,4 +515,62 @@ describe('useForm', () => {
expect(name.errors).toBeDefined()
})
})

describe('submit', () => {
it('should submit', async () => {
const { form, onSubmitForm } = useForm({
schema: basicSchema,
})

form.register('name', 'John')

let submitted = false

onSubmitForm(() => {
submitted = true
})

await sleep(0)
await form.submit()

expect(submitted).toEqual(true)
expect(form.hasAttemptedToSubmit).toEqual(true)
})

it('should blur all fields', async () => {
const { form, onSubmitForm } = useForm({
schema: basicSchema,
})

const name = form.register('name', 'John')

expect(name.isTouched).toEqual(false)

onSubmitForm(() => {})

await sleep(0)
await form.submit()

expect(name.isTouched).toEqual(true)
})

it('should not submit if there are errors', async () => {
const { form, onSubmitForm } = useForm({
schema: basicSchema,
})

let submitted = false

form.register('name', 'Jon')

onSubmitForm(() => {
submitted = true
})

await sleep(0)
await form.submit()

expect(submitted).toEqual(false)
})
})
})

0 comments on commit dbe75a4

Please sign in to comment.