@@ -321,6 +321,61 @@ defineExpose({ foo: 123 })
321321 expect ( content ) . toMatch ( / \b _ _ e x p o s e \( \{ f o o : 1 2 3 \} \) / )
322322 } )
323323
324+ describe ( 'defineModel()' , ( ) => {
325+ test ( 'basic usage' , ( ) => {
326+ const { content, bindings } = compile (
327+ `
328+ <script setup>
329+ const modelValue = defineModel({ required: true })
330+ const c = defineModel('count')
331+ </script>
332+ ` ,
333+ { defineModel : true }
334+ )
335+ assertCode ( content )
336+ expect ( content ) . toMatch ( 'props: {\n "modelValue": { required: true }' )
337+ expect ( content ) . toMatch ( '"count": {},' )
338+ expect ( content ) . toMatch ( 'emits: ["update:modelValue", "update:count"],' )
339+ expect ( content ) . toMatch (
340+ `const modelValue = _useModel("modelValue", { required: true })`
341+ )
342+ expect ( content ) . toMatch ( `const c = _useModel("count")` )
343+ expect ( content ) . toMatch ( `return { modelValue, c }` )
344+ expect ( content ) . not . toMatch ( 'defineModel' )
345+
346+ expect ( bindings ) . toStrictEqual ( {
347+ modelValue : BindingTypes . SETUP_REF ,
348+ count : BindingTypes . PROPS ,
349+ c : BindingTypes . SETUP_REF
350+ } )
351+ } )
352+
353+ test ( 'w/ defineProps and defineEmits' , ( ) => {
354+ const { content, bindings } = compile (
355+ `
356+ <script setup>
357+ defineProps({ foo: String })
358+ defineEmits(['change'])
359+ const count = defineModel({ default: 0 })
360+ </script>
361+ ` ,
362+ { defineModel : true }
363+ )
364+ assertCode ( content )
365+ expect ( content ) . toMatch ( `props: _mergeModels({ foo: String }` )
366+ expect ( content ) . toMatch ( `"modelValue": { default: 0 }` )
367+ expect ( content ) . toMatch (
368+ `const count = _useModel("modelValue", { default: 0 })`
369+ )
370+ expect ( content ) . not . toMatch ( 'defineModel' )
371+ expect ( bindings ) . toStrictEqual ( {
372+ count : BindingTypes . SETUP_REF ,
373+ foo : BindingTypes . PROPS ,
374+ modelValue : BindingTypes . PROPS
375+ } )
376+ } )
377+ } )
378+
324379 test ( '<script> after <script setup> the script content not end with `\\n`' , ( ) => {
325380 const { content } = compile ( `
326381 <script setup>
@@ -1666,6 +1721,58 @@ const emit = defineEmits(['a', 'b'])
16661721 } )
16671722 } )
16681723
1724+ describe ( 'defineModel' , ( ) => {
1725+ test ( 'basic usage' , ( ) => {
1726+ const { content, bindings } = compile (
1727+ `
1728+ <script setup lang="ts">
1729+ const modelValue = defineModel<boolean | string>()
1730+ const count = defineModel<number>('count')
1731+ </script>
1732+ ` ,
1733+ { defineModel : true }
1734+ )
1735+ assertCode ( content )
1736+ expect ( content ) . toMatch ( '"modelValue": [Boolean, String]' )
1737+ expect ( content ) . toMatch ( '"count": Number' )
1738+ expect ( content ) . toMatch ( 'emits: ["update:modelValue", "update:count"]' )
1739+ expect ( content ) . toMatch ( `const modelValue = _useModel("modelValue")` )
1740+ expect ( content ) . toMatch ( `const count = _useModel("count")` )
1741+ expect ( bindings ) . toStrictEqual ( {
1742+ modelValue : BindingTypes . SETUP_REF ,
1743+ count : BindingTypes . SETUP_REF
1744+ } )
1745+ } )
1746+
1747+ test ( 'w/ production mode' , ( ) => {
1748+ const { content, bindings } = compile (
1749+ `
1750+ <script setup lang="ts">
1751+ const modelValue = defineModel<boolean>()
1752+ const fn = defineModel<() => void>('fn')
1753+ const str = defineModel<string>('str')
1754+ </script>
1755+ ` ,
1756+ { defineModel : true , isProd : true }
1757+ )
1758+ assertCode ( content )
1759+ expect ( content ) . toMatch ( '"modelValue": Boolean' )
1760+ expect ( content ) . toMatch ( '"fn": Function' )
1761+ expect ( content ) . toMatch ( '"str": {}' )
1762+ expect ( content ) . toMatch (
1763+ 'emits: ["update:modelValue", "update:fn", "update:str"]'
1764+ )
1765+ expect ( content ) . toMatch ( `const modelValue = _useModel("modelValue")` )
1766+ expect ( content ) . toMatch ( `const fn = _useModel("fn")` )
1767+ expect ( content ) . toMatch ( `const str = _useModel("str")` )
1768+ expect ( bindings ) . toStrictEqual ( {
1769+ modelValue : BindingTypes . SETUP_REF ,
1770+ fn : BindingTypes . SETUP_REF ,
1771+ str : BindingTypes . SETUP_REF
1772+ } )
1773+ } )
1774+ } )
1775+
16691776 test ( 'runtime Enum' , ( ) => {
16701777 const { content, bindings } = compile (
16711778 `<script setup lang="ts">
0 commit comments