@@ -23,7 +23,8 @@ describe('defineModel()', () => {
2323 expect ( content ) . toMatch (
2424 `const modelValue = _useModel(__props, "modelValue")` ,
2525 )
26- expect ( content ) . toMatch ( `const c = _useModel(__props, "count")` )
26+ expect ( content ) . toMatch ( `const c = _useModel(__props, 'count')` )
27+ expect ( content ) . toMatch ( `const toString = _useModel(__props, 'toString')` )
2728 expect ( content ) . toMatch ( `return { modelValue, c, toString }` )
2829 expect ( content ) . not . toMatch ( 'defineModel' )
2930
@@ -71,7 +72,7 @@ describe('defineModel()', () => {
7172 "count": {},
7273 "countModifiers": {},
7374 })` )
74- expect ( content ) . toMatch ( `const count = _useModel(__props, " count" )` )
75+ expect ( content ) . toMatch ( `const count = _useModel(__props, ' count' )` )
7576 expect ( content ) . not . toMatch ( 'defineModel' )
7677 expect ( bindings ) . toStrictEqual ( {
7778 foo : BindingTypes . PROPS ,
@@ -104,11 +105,15 @@ describe('defineModel()', () => {
104105 )
105106
106107 expect ( content ) . toMatch (
107- `const modelValue = _useModel(__props, "modelValue")` ,
108+ `const modelValue = _useModel<boolean | string>(__props, "modelValue")` ,
109+ )
110+ expect ( content ) . toMatch ( `const count = _useModel<number>(__props, 'count')` )
111+ expect ( content ) . toMatch (
112+ `const disabled = _useModel<number>(__props, 'disabled')` ,
113+ )
114+ expect ( content ) . toMatch (
115+ `const any = _useModel<any | boolean>(__props, 'any')` ,
108116 )
109- expect ( content ) . toMatch ( `const count = _useModel(__props, "count")` )
110- expect ( content ) . toMatch ( `const disabled = _useModel(__props, "disabled")` )
111- expect ( content ) . toMatch ( `const any = _useModel(__props, "any")` )
112117
113118 expect ( bindings ) . toStrictEqual ( {
114119 modelValue : BindingTypes . SETUP_REF ,
@@ -143,10 +148,10 @@ describe('defineModel()', () => {
143148 'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:str", "update:optional"]' ,
144149 )
145150 expect ( content ) . toMatch (
146- `const modelValue = _useModel(__props, "modelValue")` ,
151+ `const modelValue = _useModel<boolean> (__props, "modelValue")` ,
147152 )
148- expect ( content ) . toMatch ( `const fn = _useModel( __props, "fn" )` )
149- expect ( content ) . toMatch ( `const str = _useModel(__props, " str" )` )
153+ expect ( content ) . toMatch ( `const fn = _useModel<() => void>( __props, 'fn' )` )
154+ expect ( content ) . toMatch ( `const str = _useModel<string> (__props, ' str' )` )
150155 expect ( bindings ) . toStrictEqual ( {
151156 modelValue : BindingTypes . SETUP_REF ,
152157 fn : BindingTypes . SETUP_REF ,
@@ -171,7 +176,10 @@ describe('defineModel()', () => {
171176 assertCode ( content )
172177 expect ( content ) . toMatch ( / " m o d e l V a l u e " : { \s + r e q u i r e d : t r u e , ? \s + } / m)
173178 expect ( content ) . toMatch (
174- `_useModel(__props, "modelValue", { get(v) { return v - 1 }, set: (v) => { return v + 1 }, })` ,
179+ `_useModel(__props, "modelValue", {
180+ get(v) { return v - 1 },
181+ set: (v) => { return v + 1 },
182+ })` ,
175183 )
176184
177185 const { content : content2 } = compile (
@@ -191,7 +199,26 @@ describe('defineModel()', () => {
191199 / " m o d e l V a l u e " : { \s + d e f a u l t : 0 , \s + r e q u i r e d : t r u e , ? \s + } / m,
192200 )
193201 expect ( content2 ) . toMatch (
194- `_useModel(__props, "modelValue", { get(v) { return v - 1 }, set: (v) => { return v + 1 }, })` ,
202+ `_useModel(__props, "modelValue", {
203+ get(v) { return v - 1 },
204+ set: (v) => { return v + 1 },
205+ })` ,
195206 )
196207 } )
208+
209+ test ( 'usage w/ props destructure' , ( ) => {
210+ const { content } = compile (
211+ `
212+ <script setup lang="ts">
213+ const { x } = defineProps<{ x: number }>()
214+ const modelValue = defineModel({
215+ set: (v) => { return v + x }
216+ })
217+ </script>
218+ ` ,
219+ { propsDestructure : true } ,
220+ )
221+ assertCode ( content )
222+ expect ( content ) . toMatch ( `set: (v) => { return v + __props.x }` )
223+ } )
197224} )
0 commit comments