11import { initialState , useFormHandler } from '../useFormHandler'
22import { 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} )
0 commit comments