@@ -2,7 +2,7 @@ import { FieldMeta, FormContext, FormMeta, useField, useForm } from '@/vee-valid
22import { mountWithHoc , setValue , flushPromises , runInSetup , dispatchEvent } from './helpers' ;
33import * as yup from 'yup' ;
44import { onMounted , Ref } from 'vue' ;
5- import ModelComp from './helpers/ModelComp' ;
5+ import { ModelComp , CustomModelComp } from './helpers/ModelComp' ;
66
77describe ( 'useForm()' , ( ) => {
88 const REQUIRED_MESSAGE = 'Field is required' ;
@@ -928,6 +928,80 @@ describe('useForm()', () => {
928928 await flushPromises ( ) ;
929929 expect ( document . body . innerHTML ) . toContain ( 'valid' ) ;
930930 } ) ;
931+
932+ test ( 'can have custom model' , async ( ) => {
933+ mountWithHoc ( {
934+ components : {
935+ CustomModelComp,
936+ } ,
937+ setup ( ) {
938+ const { defineComponentBinds, values, errors } = useForm ( {
939+ validationSchema : yup . object ( {
940+ name : yup . string ( ) . required ( ) ,
941+ } ) ,
942+ } ) ;
943+
944+ const field = defineComponentBinds ( 'name' , { model : 'value' } ) ;
945+
946+ return { field, values, errors } ;
947+ } ,
948+ template : `
949+ <CustomModelComp v-bind="field" />
950+ <span id="errors">{{ errors.name }}</span>
951+ <span id="values">{{ values.name }}</span>
952+ ` ,
953+ } ) ;
954+
955+ await flushPromises ( ) ;
956+ const errorEl = document . getElementById ( 'errors' ) ;
957+ const valuesEl = document . getElementById ( 'values' ) ;
958+ setValue ( document . querySelector ( 'input' ) as any , '' ) ;
959+ dispatchEvent ( document . querySelector ( 'input' ) as any , 'blur' ) ;
960+ await flushPromises ( ) ;
961+ expect ( errorEl ?. textContent ) . toBe ( 'name is a required field' ) ;
962+ setValue ( document . querySelector ( 'input' ) as any , '123' ) ;
963+ dispatchEvent ( document . querySelector ( 'input' ) as any , 'blur' ) ;
964+ await flushPromises ( ) ;
965+ expect ( errorEl ?. textContent ) . toBe ( '' ) ;
966+ expect ( valuesEl ?. textContent ) . toBe ( '123' ) ;
967+ } ) ;
968+
969+ test ( 'can have lazy custom model' , async ( ) => {
970+ mountWithHoc ( {
971+ components : {
972+ CustomModelComp,
973+ } ,
974+ setup ( ) {
975+ const { defineComponentBinds, values, errors } = useForm ( {
976+ validationSchema : yup . object ( {
977+ name : yup . string ( ) . required ( ) ,
978+ } ) ,
979+ } ) ;
980+
981+ const field = defineComponentBinds ( 'name' , ( ) => ( { model : 'value' } ) ) ;
982+
983+ return { field, values, errors } ;
984+ } ,
985+ template : `
986+ <CustomModelComp v-bind="field" />
987+ <span id="errors">{{ errors.name }}</span>
988+ <span id="values">{{ values.name }}</span>
989+ ` ,
990+ } ) ;
991+
992+ await flushPromises ( ) ;
993+ const errorEl = document . getElementById ( 'errors' ) ;
994+ const valuesEl = document . getElementById ( 'values' ) ;
995+ setValue ( document . querySelector ( 'input' ) as any , '' ) ;
996+ dispatchEvent ( document . querySelector ( 'input' ) as any , 'blur' ) ;
997+ await flushPromises ( ) ;
998+ expect ( errorEl ?. textContent ) . toBe ( 'name is a required field' ) ;
999+ setValue ( document . querySelector ( 'input' ) as any , '123' ) ;
1000+ dispatchEvent ( document . querySelector ( 'input' ) as any , 'blur' ) ;
1001+ await flushPromises ( ) ;
1002+ expect ( errorEl ?. textContent ) . toBe ( '' ) ;
1003+ expect ( valuesEl ?. textContent ) . toBe ( '123' ) ;
1004+ } ) ;
9311005 } ) ;
9321006
9331007 describe ( 'defineInputBinds' , ( ) => {
0 commit comments