@@ -365,6 +365,61 @@ defineExpose({ foo: 123 })
365
365
expect ( content ) . toMatch ( / \b _ _ e x p o s e \( \{ f o o : 1 2 3 \} \) / )
366
366
} )
367
367
368
+ describe ( 'defineModel()' , ( ) => {
369
+ test ( 'basic usage' , ( ) => {
370
+ const { content, bindings } = compile (
371
+ `
372
+ <script setup>
373
+ const modelValue = defineModel({ required: true })
374
+ const c = defineModel('count')
375
+ </script>
376
+ ` ,
377
+ { defineModel : true }
378
+ )
379
+ assertCode ( content )
380
+ expect ( content ) . toMatch ( 'props: {\n "modelValue": { required: true }' )
381
+ expect ( content ) . toMatch ( '"count": {},' )
382
+ expect ( content ) . toMatch ( 'emits: ["update:modelValue", "update:count"],' )
383
+ expect ( content ) . toMatch (
384
+ `const modelValue = _useModel("modelValue", { required: true })`
385
+ )
386
+ expect ( content ) . toMatch ( `const c = _useModel("count")` )
387
+ expect ( content ) . toMatch ( `return { modelValue, c }` )
388
+ expect ( content ) . not . toMatch ( 'defineModel' )
389
+
390
+ expect ( bindings ) . toStrictEqual ( {
391
+ modelValue : BindingTypes . SETUP_REF ,
392
+ count : BindingTypes . PROPS ,
393
+ c : BindingTypes . SETUP_REF
394
+ } )
395
+ } )
396
+
397
+ test ( 'w/ defineProps and defineEmits' , ( ) => {
398
+ const { content, bindings } = compile (
399
+ `
400
+ <script setup>
401
+ defineProps({ foo: String })
402
+ defineEmits(['change'])
403
+ const count = defineModel({ default: 0 })
404
+ </script>
405
+ ` ,
406
+ { defineModel : true }
407
+ )
408
+ assertCode ( content )
409
+ expect ( content ) . toMatch ( `props: _mergeModels({ foo: String }` )
410
+ expect ( content ) . toMatch ( `"modelValue": { default: 0 }` )
411
+ expect ( content ) . toMatch (
412
+ `const count = _useModel("modelValue", { default: 0 })`
413
+ )
414
+ expect ( content ) . not . toMatch ( 'defineModel' )
415
+ expect ( bindings ) . toStrictEqual ( {
416
+ count : BindingTypes . SETUP_REF ,
417
+ foo : BindingTypes . PROPS ,
418
+ modelValue : BindingTypes . PROPS
419
+ } )
420
+ } )
421
+ } )
422
+
368
423
test ( '<script> after <script setup> the script content not end with `\\n`' , ( ) => {
369
424
const { content } = compile ( `
370
425
<script setup>
@@ -1710,6 +1765,58 @@ const emit = defineEmits(['a', 'b'])
1710
1765
} )
1711
1766
} )
1712
1767
1768
+ describe ( 'defineModel' , ( ) => {
1769
+ test ( 'basic usage' , ( ) => {
1770
+ const { content, bindings } = compile (
1771
+ `
1772
+ <script setup lang="ts">
1773
+ const modelValue = defineModel<boolean | string>()
1774
+ const count = defineModel<number>('count')
1775
+ </script>
1776
+ ` ,
1777
+ { defineModel : true }
1778
+ )
1779
+ assertCode ( content )
1780
+ expect ( content ) . toMatch ( '"modelValue": [Boolean, String]' )
1781
+ expect ( content ) . toMatch ( '"count": Number' )
1782
+ expect ( content ) . toMatch ( 'emits: ["update:modelValue", "update:count"]' )
1783
+ expect ( content ) . toMatch ( `const modelValue = _useModel("modelValue")` )
1784
+ expect ( content ) . toMatch ( `const count = _useModel("count")` )
1785
+ expect ( bindings ) . toStrictEqual ( {
1786
+ modelValue : BindingTypes . SETUP_REF ,
1787
+ count : BindingTypes . SETUP_REF
1788
+ } )
1789
+ } )
1790
+
1791
+ test ( 'w/ production mode' , ( ) => {
1792
+ const { content, bindings } = compile (
1793
+ `
1794
+ <script setup lang="ts">
1795
+ const modelValue = defineModel<boolean>()
1796
+ const fn = defineModel<() => void>('fn')
1797
+ const str = defineModel<string>('str')
1798
+ </script>
1799
+ ` ,
1800
+ { defineModel : true , isProd : true }
1801
+ )
1802
+ assertCode ( content )
1803
+ expect ( content ) . toMatch ( '"modelValue": Boolean' )
1804
+ expect ( content ) . toMatch ( '"fn": Function' )
1805
+ expect ( content ) . toMatch ( '"str": {}' )
1806
+ expect ( content ) . toMatch (
1807
+ 'emits: ["update:modelValue", "update:fn", "update:str"]'
1808
+ )
1809
+ expect ( content ) . toMatch ( `const modelValue = _useModel("modelValue")` )
1810
+ expect ( content ) . toMatch ( `const fn = _useModel("fn")` )
1811
+ expect ( content ) . toMatch ( `const str = _useModel("str")` )
1812
+ expect ( bindings ) . toStrictEqual ( {
1813
+ modelValue : BindingTypes . SETUP_REF ,
1814
+ fn : BindingTypes . SETUP_REF ,
1815
+ str : BindingTypes . SETUP_REF
1816
+ } )
1817
+ } )
1818
+ } )
1819
+
1713
1820
test ( 'runtime Enum' , ( ) => {
1714
1821
const { content, bindings } = compile (
1715
1822
`<script setup lang="ts">
0 commit comments