Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit bf83d79

Browse files
author
Daniel Requejo
committed
🧪 Update tests for new functionality
1 parent 94ad27a commit bf83d79

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/test/component.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { FormHandler } from '../FormHandler'
22
import { mount } from '@vue/test-utils'
33
import { expect, it, describe } from 'vitest'
44

5-
describe('FormHandler component testing', () => {
6-
it('Form handler gets mounted', () => {
5+
describe('FormHandler', () => {
6+
it('should mount', () => {
77
expect(FormHandler).toBeTruthy()
88

99
const wrapper = mount(FormHandler)
1010
expect(wrapper.exists()).toBeTruthy()
1111
})
12-
it('Form handler scoped slot provides values and form state', () => {
12+
it('should provide values and formState', () => {
1313
expect(FormHandler).toBeTruthy()
1414

1515
const wrapper = mount(FormHandler, {
@@ -23,7 +23,7 @@ describe('FormHandler component testing', () => {
2323
expect(wrapper.html()).toContain('values')
2424
expect(wrapper.html()).toContain('formState')
2525
})
26-
it('Form handler is correctly initialized', () => {
26+
it('should be properly initialized', () => {
2727
const initialValues = {
2828
field1: 'something',
2929
field2: 'some other thing',

src/test/handler.test.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { initialState, useFormHandler } from '../useFormHandler'
22
import { expect, it, describe } from 'vitest'
33

4-
describe('Form handler testing', () => {
5-
it('Initial form state and values', () => {
4+
describe('useFormHandler()', () => {
5+
it('should have correct initial state and values', () => {
66
const { values, formState } = useFormHandler()
77
expect(values).toStrictEqual({})
88
expect(formState).toStrictEqual({ ...initialState() })
99
})
10-
it('Initial values should be applied without', () => {
10+
it('should apply initialValues when specified', () => {
1111
const initialValues = {
1212
field: 'test',
1313
}
1414
const { values } = useFormHandler({ initialValues })
1515
expect(values).toStrictEqual(initialValues)
1616
})
17-
it('Setting a value programmatically', async () => {
17+
it('should set a value programmatically', async () => {
1818
const { values, setValue, formState } = useFormHandler()
1919
await setValue('field', 'oneTwoThree')
2020
expect(values.field).toBe('oneTwoThree')
2121
expect(formState.isDirty).toBeTruthy()
2222
})
23-
it('Clearing a field programmatically', async () => {
23+
it('should clear a field', async () => {
2424
const { register, values, setValue, formState, clearField } =
2525
useFormHandler()
2626
register('field')
@@ -31,7 +31,7 @@ describe('Form handler testing', () => {
3131
expect(values.field).toBe(null)
3232
expect(formState.isDirty).toBeFalsy()
3333
})
34-
it('Clearing an initialized field leaves it dirty', async () => {
34+
it('should leave an initialized field dirty when clearing', async () => {
3535
const { register, values, formState, clearField } = useFormHandler({
3636
initialValues: { field: 'value' },
3737
})
@@ -42,13 +42,13 @@ describe('Form handler testing', () => {
4242
expect(values.field).toBe(null)
4343
expect(formState.isDirty).toBeTruthy()
4444
})
45-
it('Setting an error programmatically', async () => {
45+
it('should set an error programmatically', async () => {
4646
const { formState, setError } = useFormHandler()
4747
setError('field', 'some error')
4848
expect(formState.errors).toStrictEqual({ field: 'some error' })
4949
expect(formState.isValid).toBeFalsy()
5050
})
51-
it('Clearing an error programmatically', async () => {
51+
it('should clear an error programmatically', async () => {
5252
const { formState, setError, clearError } = useFormHandler()
5353
const error = 'This field has an error'
5454
setError('field', error)
@@ -57,7 +57,7 @@ describe('Form handler testing', () => {
5757
expect(formState.errors.field).toBeUndefined()
5858
expect(formState.isValid).toBeTruthy()
5959
})
60-
it('Clearing all errors of the form programmatically', async () => {
60+
it('should clear all form errors programmatically', async () => {
6161
const { formState, setError, clearError } = useFormHandler()
6262
const error = 'some error'
6363
setError('field1', error)
@@ -70,7 +70,7 @@ describe('Form handler testing', () => {
7070
expect(formState.errors.field2).toBeUndefined()
7171
expect(formState.isValid).toBeTruthy()
7272
})
73-
it('Resetting a field it back to its initial values and state', async () => {
73+
it('should correctly reset a field', async () => {
7474
const { values, formState, resetField, setValue } = useFormHandler({
7575
initialValues: { field: 'value' },
7676
})
@@ -83,7 +83,7 @@ describe('Form handler testing', () => {
8383
expect(values.field).toBe('value')
8484
expect(formState.isDirty).toBeFalsy()
8585
})
86-
it('Expecting modifiedValues to work', async () => {
86+
it('should return correct modifiedValues', async () => {
8787
const { modifiedValues, setValue } = useFormHandler({
8888
initialValues: {
8989
field: 'something',
@@ -92,4 +92,21 @@ describe('Form handler testing', () => {
9292
await setValue('field2', 'another thing')
9393
expect(modifiedValues()).toStrictEqual({ field2: 'another thing' })
9494
})
95+
it('should register field properly via build', () => {
96+
const { build, values } = useFormHandler()
97+
const form = build({
98+
field: {},
99+
})
100+
101+
expect(form.value.field.name).toBe('field')
102+
expect(form.value.field.error).toBeUndefined()
103+
expect(form.value.field.onBlur).toBeDefined()
104+
expect(form.value.field.isDirty).toBeUndefined()
105+
expect(form.value.field.isTouched).toBeUndefined()
106+
expect(form.value.field.onClear).toBeDefined()
107+
expect(form.value.field.onChange).toBeDefined()
108+
expect(form.value.field.modelValue).toBe(null)
109+
expect(form.value.field['onUpdate:modelValue']).toBeDefined()
110+
expect(values.field).toBe(null)
111+
})
95112
})

src/test/register.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, it, expect } from 'vitest'
22
import { useFormHandler } from '../useFormHandler'
33

4-
describe('Register function testing', () => {
5-
it('Registering a field', () => {
4+
describe('register()', () => {
5+
it('should register a field', () => {
66
const { values, register } = useFormHandler()
77
const field = register('field')
88
expect(field.name).toBe('field')
@@ -16,31 +16,37 @@ describe('Register function testing', () => {
1616
expect(field['onUpdate:modelValue']).toBeDefined()
1717
expect(values.field).toBe(null)
1818
})
19-
it('Specified native field should have native handlers', () => {
19+
it('should apply native handlers by default', () => {
20+
const { register } = useFormHandler()
21+
const field = register('field')
22+
expect(field.ref).toBeDefined()
23+
expect(field.onChange).toBeDefined()
24+
})
25+
it('should apply native handlers when native is specified', () => {
2026
const { register } = useFormHandler()
2127
const field = register('field', { native: true })
2228
expect(field.ref).toBeDefined()
2329
expect(field.onChange).toBeDefined()
2430
})
25-
it("Specified custom field shouldn't have native handlers", () => {
31+
it('should not apply native handlers when native is set false', () => {
2632
const { register } = useFormHandler()
2733
const field = register('field', { native: false })
2834
expect(field.onChange).toBeUndefined()
2935
expect(field.modelValue).toBeDefined()
3036
expect(field['onUpdate:modelValue']).toBeDefined()
3137
})
32-
it('Input registered with details receives dirty and touched states', () => {
38+
it('should apply dirty and touched states when withDetails is specified', () => {
3339
const { register } = useFormHandler()
3440
const field = register('field', { withDetails: true })
3541
expect(field.isDirty).toBeDefined()
3642
expect(field.isTouched).toBeDefined()
3743
})
38-
it('Registering a field with default value', () => {
44+
it('should apply default value', () => {
3945
const { values, register } = useFormHandler()
4046
register('field', { defaultValue: 'something' })
4147
expect(values.field).toBe('something')
4248
})
43-
it('Registered validations work on update via handler', async () => {
49+
it('should trigger validation via handler', async () => {
4450
const { values, register, formState } = useFormHandler()
4551
const field = register('field', {
4652
validate: {
@@ -53,7 +59,7 @@ describe('Register function testing', () => {
5359
expect(formState.isValid).toBeFalsy()
5460
}
5561
})
56-
it('Registered validations work on update via setter', async () => {
62+
it('should trigger validation via setter', async () => {
5763
const { values, register, formState, setValue, triggerValidation } =
5864
useFormHandler()
5965
register('field', {

0 commit comments

Comments
 (0)