|
|
@@ -23,7 +23,8 @@ describe('defineModel()', () => {
|
|
|
expect(content).toMatch(
|
|
|
`const modelValue = _useModel(__props, "modelValue")`,
|
|
|
)
|
|
|
- expect(content).toMatch(`const c = _useModel(__props, "count")`)
|
|
|
+ expect(content).toMatch(`const c = _useModel(__props, 'count')`)
|
|
|
+ expect(content).toMatch(`const toString = _useModel(__props, 'toString')`)
|
|
|
expect(content).toMatch(`return { modelValue, c, toString }`)
|
|
|
expect(content).not.toMatch('defineModel')
|
|
|
|
|
|
@@ -71,7 +72,7 @@ describe('defineModel()', () => {
|
|
|
"count": {},
|
|
|
"countModifiers": {},
|
|
|
})`)
|
|
|
- expect(content).toMatch(`const count = _useModel(__props, "count")`)
|
|
|
+ expect(content).toMatch(`const count = _useModel(__props, 'count')`)
|
|
|
expect(content).not.toMatch('defineModel')
|
|
|
expect(bindings).toStrictEqual({
|
|
|
foo: BindingTypes.PROPS,
|
|
|
@@ -104,11 +105,15 @@ describe('defineModel()', () => {
|
|
|
)
|
|
|
|
|
|
expect(content).toMatch(
|
|
|
- `const modelValue = _useModel(__props, "modelValue")`,
|
|
|
+ `const modelValue = _useModel<boolean | string>(__props, "modelValue")`,
|
|
|
+ )
|
|
|
+ expect(content).toMatch(`const count = _useModel<number>(__props, 'count')`)
|
|
|
+ expect(content).toMatch(
|
|
|
+ `const disabled = _useModel<number>(__props, 'disabled')`,
|
|
|
+ )
|
|
|
+ expect(content).toMatch(
|
|
|
+ `const any = _useModel<any | boolean>(__props, 'any')`,
|
|
|
)
|
|
|
- expect(content).toMatch(`const count = _useModel(__props, "count")`)
|
|
|
- expect(content).toMatch(`const disabled = _useModel(__props, "disabled")`)
|
|
|
- expect(content).toMatch(`const any = _useModel(__props, "any")`)
|
|
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
modelValue: BindingTypes.SETUP_REF,
|
|
|
@@ -143,10 +148,10 @@ describe('defineModel()', () => {
|
|
|
'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:str", "update:optional"]',
|
|
|
)
|
|
|
expect(content).toMatch(
|
|
|
- `const modelValue = _useModel(__props, "modelValue")`,
|
|
|
+ `const modelValue = _useModel<boolean>(__props, "modelValue")`,
|
|
|
)
|
|
|
- expect(content).toMatch(`const fn = _useModel(__props, "fn")`)
|
|
|
- expect(content).toMatch(`const str = _useModel(__props, "str")`)
|
|
|
+ expect(content).toMatch(`const fn = _useModel<() => void>(__props, 'fn')`)
|
|
|
+ expect(content).toMatch(`const str = _useModel<string>(__props, 'str')`)
|
|
|
expect(bindings).toStrictEqual({
|
|
|
modelValue: BindingTypes.SETUP_REF,
|
|
|
fn: BindingTypes.SETUP_REF,
|
|
|
@@ -171,7 +176,10 @@ describe('defineModel()', () => {
|
|
|
assertCode(content)
|
|
|
expect(content).toMatch(/"modelValue": {\s+required: true,?\s+}/m)
|
|
|
expect(content).toMatch(
|
|
|
- `_useModel(__props, "modelValue", { get(v) { return v - 1 }, set: (v) => { return v + 1 }, })`,
|
|
|
+ `_useModel(__props, "modelValue", {
|
|
|
+ get(v) { return v - 1 },
|
|
|
+ set: (v) => { return v + 1 },
|
|
|
+ })`,
|
|
|
)
|
|
|
|
|
|
const { content: content2 } = compile(
|
|
|
@@ -191,7 +199,26 @@ describe('defineModel()', () => {
|
|
|
/"modelValue": {\s+default: 0,\s+required: true,?\s+}/m,
|
|
|
)
|
|
|
expect(content2).toMatch(
|
|
|
- `_useModel(__props, "modelValue", { get(v) { return v - 1 }, set: (v) => { return v + 1 }, })`,
|
|
|
+ `_useModel(__props, "modelValue", {
|
|
|
+ get(v) { return v - 1 },
|
|
|
+ set: (v) => { return v + 1 },
|
|
|
+ })`,
|
|
|
)
|
|
|
})
|
|
|
+
|
|
|
+ test('usage w/ props destructure', () => {
|
|
|
+ const { content } = compile(
|
|
|
+ `
|
|
|
+ <script setup lang="ts">
|
|
|
+ const { x } = defineProps<{ x: number }>()
|
|
|
+ const modelValue = defineModel({
|
|
|
+ set: (v) => { return v + x }
|
|
|
+ })
|
|
|
+ </script>
|
|
|
+ `,
|
|
|
+ { propsDestructure: true },
|
|
|
+ )
|
|
|
+ assertCode(content)
|
|
|
+ expect(content).toMatch(`set: (v) => { return v + __props.x }`)
|
|
|
+ })
|
|
|
})
|