defineModel.spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { assertCode, compileSFCScript as compile } from '../utils'
  3. describe('defineModel()', () => {
  4. test('basic usage', () => {
  5. const { content, bindings } = compile(
  6. `
  7. <script setup>
  8. const modelValue = defineModel({ required: true })
  9. const c = defineModel('count')
  10. const toString = defineModel('toString', { type: Function })
  11. </script>
  12. `,
  13. )
  14. assertCode(content)
  15. expect(content).toMatch('props: {')
  16. expect(content).toMatch('"modelValue": { required: true },')
  17. expect(content).toMatch('"count": {},')
  18. expect(content).toMatch('"toString": { type: Function },')
  19. expect(content).toMatch(
  20. 'emits: ["update:modelValue", "update:count", "update:toString"],',
  21. )
  22. expect(content).toMatch(
  23. `const modelValue = _useModel(__props, "modelValue")`,
  24. )
  25. expect(content).toMatch(`const c = _useModel(__props, 'count')`)
  26. expect(content).toMatch(`const toString = _useModel(__props, 'toString')`)
  27. expect(content).toMatch(`return { modelValue, c, toString }`)
  28. expect(content).not.toMatch('defineModel')
  29. expect(bindings).toStrictEqual({
  30. modelValue: BindingTypes.SETUP_REF,
  31. count: BindingTypes.PROPS,
  32. c: BindingTypes.SETUP_REF,
  33. toString: BindingTypes.SETUP_REF,
  34. })
  35. })
  36. test('w/ defineProps and defineEmits', () => {
  37. const { content, bindings } = compile(
  38. `
  39. <script setup>
  40. defineProps({ foo: String })
  41. defineEmits(['change'])
  42. const count = defineModel({ default: 0 })
  43. </script>
  44. `,
  45. )
  46. assertCode(content)
  47. expect(content).toMatch(`props: /*#__PURE__*/_mergeModels({ foo: String }`)
  48. expect(content).toMatch(`"modelValue": { default: 0 }`)
  49. expect(content).toMatch(`const count = _useModel(__props, "modelValue")`)
  50. expect(content).not.toMatch('defineModel')
  51. expect(bindings).toStrictEqual({
  52. count: BindingTypes.SETUP_REF,
  53. foo: BindingTypes.PROPS,
  54. modelValue: BindingTypes.PROPS,
  55. })
  56. })
  57. test('w/ array props', () => {
  58. const { content, bindings } = compile(
  59. `
  60. <script setup>
  61. defineProps(['foo', 'bar'])
  62. const count = defineModel('count')
  63. </script>
  64. `,
  65. )
  66. assertCode(content)
  67. expect(content).toMatch(`props: /*#__PURE__*/_mergeModels(['foo', 'bar'], {
  68. "count": {},
  69. "countModifiers": {},
  70. })`)
  71. expect(content).toMatch(`const count = _useModel(__props, 'count')`)
  72. expect(content).not.toMatch('defineModel')
  73. expect(bindings).toStrictEqual({
  74. foo: BindingTypes.PROPS,
  75. bar: BindingTypes.PROPS,
  76. count: BindingTypes.SETUP_REF,
  77. })
  78. })
  79. test('w/ types, basic usage', () => {
  80. const { content, bindings } = compile(
  81. `
  82. <script setup lang="ts">
  83. const modelValue = defineModel<boolean | string>()
  84. const count = defineModel<number>('count')
  85. const disabled = defineModel<number>('disabled', { required: false })
  86. const any = defineModel<any | boolean>('any')
  87. </script>
  88. `,
  89. )
  90. assertCode(content)
  91. expect(content).toMatch('"modelValue": { type: [Boolean, String] }')
  92. expect(content).toMatch('"modelModifiers": {}')
  93. expect(content).toMatch('"count": { type: Number }')
  94. expect(content).toMatch(
  95. '"disabled": { type: Number, ...{ required: false } }',
  96. )
  97. expect(content).toMatch('"any": { type: Boolean, skipCheck: true }')
  98. expect(content).toMatch(
  99. 'emits: ["update:modelValue", "update:count", "update:disabled", "update:any"]',
  100. )
  101. expect(content).toMatch(
  102. `const modelValue = _useModel<boolean | string>(__props, "modelValue")`,
  103. )
  104. expect(content).toMatch(`const count = _useModel<number>(__props, 'count')`)
  105. expect(content).toMatch(
  106. `const disabled = _useModel<number>(__props, 'disabled')`,
  107. )
  108. expect(content).toMatch(
  109. `const any = _useModel<any | boolean>(__props, 'any')`,
  110. )
  111. expect(bindings).toStrictEqual({
  112. modelValue: BindingTypes.SETUP_REF,
  113. count: BindingTypes.SETUP_REF,
  114. disabled: BindingTypes.SETUP_REF,
  115. any: BindingTypes.SETUP_REF,
  116. })
  117. })
  118. test('w/ types, production mode', () => {
  119. const { content, bindings } = compile(
  120. `
  121. <script setup lang="ts">
  122. const modelValue = defineModel<boolean>()
  123. const fn = defineModel<() => void>('fn')
  124. const fnWithDefault = defineModel<() => void>('fnWithDefault', { default: () => null })
  125. const str = defineModel<string>('str')
  126. const optional = defineModel<string>('optional', { required: false })
  127. </script>
  128. `,
  129. { isProd: true },
  130. )
  131. assertCode(content)
  132. expect(content).toMatch('"modelValue": { type: Boolean }')
  133. expect(content).toMatch('"fn": {}')
  134. expect(content).toMatch(
  135. '"fnWithDefault": { type: Function, ...{ default: () => null } },',
  136. )
  137. expect(content).toMatch('"str": {}')
  138. expect(content).toMatch('"optional": { required: false }')
  139. expect(content).toMatch(
  140. 'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:str", "update:optional"]',
  141. )
  142. expect(content).toMatch(
  143. `const modelValue = _useModel<boolean>(__props, "modelValue")`,
  144. )
  145. expect(content).toMatch(`const fn = _useModel<() => void>(__props, 'fn')`)
  146. expect(content).toMatch(`const str = _useModel<string>(__props, 'str')`)
  147. expect(bindings).toStrictEqual({
  148. modelValue: BindingTypes.SETUP_REF,
  149. fn: BindingTypes.SETUP_REF,
  150. fnWithDefault: BindingTypes.SETUP_REF,
  151. str: BindingTypes.SETUP_REF,
  152. optional: BindingTypes.SETUP_REF,
  153. })
  154. })
  155. test('get / set transformers', () => {
  156. const { content } = compile(
  157. `
  158. <script setup lang="ts">
  159. const modelValue = defineModel({
  160. get(v) { return v - 1 },
  161. set: (v) => { return v + 1 },
  162. required: true
  163. })
  164. </script>
  165. `,
  166. )
  167. assertCode(content)
  168. expect(content).toMatch(/"modelValue": {\s+required: true,?\s+}/m)
  169. expect(content).toMatch(
  170. `_useModel(__props, "modelValue", {
  171. get(v) { return v - 1 },
  172. set: (v) => { return v + 1 },
  173. })`,
  174. )
  175. const { content: content2 } = compile(
  176. `
  177. <script setup lang="ts">
  178. const modelValue = defineModel({
  179. default: 0,
  180. get(v) { return v - 1 },
  181. required: true,
  182. set: (v) => { return v + 1 },
  183. })
  184. </script>
  185. `,
  186. )
  187. assertCode(content2)
  188. expect(content2).toMatch(
  189. /"modelValue": {\s+default: 0,\s+required: true,?\s+}/m,
  190. )
  191. expect(content2).toMatch(
  192. `_useModel(__props, "modelValue", {
  193. get(v) { return v - 1 },
  194. set: (v) => { return v + 1 },
  195. })`,
  196. )
  197. })
  198. test('usage w/ props destructure', () => {
  199. const { content } = compile(
  200. `
  201. <script setup lang="ts">
  202. const { x } = defineProps<{ x: number }>()
  203. const modelValue = defineModel({
  204. set: (v) => { return v + x }
  205. })
  206. </script>
  207. `,
  208. { propsDestructure: true },
  209. )
  210. assertCode(content)
  211. expect(content).toMatch(`set: (v) => { return v + __props.x }`)
  212. })
  213. })