@@ -23,7 +23,8 @@ describe('defineModel()', () => {
23
23
expect ( content ) . toMatch (
24
24
`const modelValue = _useModel(__props, "modelValue")` ,
25
25
)
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')` )
27
28
expect ( content ) . toMatch ( `return { modelValue, c, toString }` )
28
29
expect ( content ) . not . toMatch ( 'defineModel' )
29
30
@@ -71,7 +72,7 @@ describe('defineModel()', () => {
71
72
"count": {},
72
73
"countModifiers": {},
73
74
})` )
74
- expect ( content ) . toMatch ( `const count = _useModel(__props, " count" )` )
75
+ expect ( content ) . toMatch ( `const count = _useModel(__props, ' count' )` )
75
76
expect ( content ) . not . toMatch ( 'defineModel' )
76
77
expect ( bindings ) . toStrictEqual ( {
77
78
foo : BindingTypes . PROPS ,
@@ -104,11 +105,15 @@ describe('defineModel()', () => {
104
105
)
105
106
106
107
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')` ,
108
116
)
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")` )
112
117
113
118
expect ( bindings ) . toStrictEqual ( {
114
119
modelValue : BindingTypes . SETUP_REF ,
@@ -143,10 +148,10 @@ describe('defineModel()', () => {
143
148
'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:str", "update:optional"]' ,
144
149
)
145
150
expect ( content ) . toMatch (
146
- `const modelValue = _useModel(__props, "modelValue")` ,
151
+ `const modelValue = _useModel<boolean> (__props, "modelValue")` ,
147
152
)
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' )` )
150
155
expect ( bindings ) . toStrictEqual ( {
151
156
modelValue : BindingTypes . SETUP_REF ,
152
157
fn : BindingTypes . SETUP_REF ,
@@ -171,7 +176,10 @@ describe('defineModel()', () => {
171
176
assertCode ( content )
172
177
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)
173
178
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
+ })` ,
175
183
)
176
184
177
185
const { content : content2 } = compile (
@@ -191,7 +199,26 @@ describe('defineModel()', () => {
191
199
/ " 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,
192
200
)
193
201
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
+ })` ,
195
206
)
196
207
} )
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
+ } )
197
224
} )
0 commit comments