Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): test cases for mixing composition/options api #11679

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,36 @@ describe('SFC analyze <script> bindings', () => {
})
})

it('recognizes data/setup return', () => {
const { bindings } = compile(`
<script>
const bar = 2
const foo = 2
const msg = 2
const hello = 2
export default {
setup() {
return {
foo: 1,
hello: numm
}
},
data() {
return {
foo: null,
msg: null
}
}
}
</script>
`)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_MAYBE_REF,
msg: BindingTypes.DATA,
hello: BindingTypes.SETUP_MAYBE_REF,
})
})

it('recognizes methods', () => {
const { bindings } = compile(`
<script>
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-sfc/src/script/analyzeScriptBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
bodyItem.argument.type === 'ObjectExpression'
) {
for (const key of getObjectExpressionKeys(bodyItem.argument)) {
// use variables in setup first, consistent with runtime
if (bindings[key] === BindingTypes.SETUP_MAYBE_REF) continue
bindings[key] =
property.key.name === 'setup'
? BindingTypes.SETUP_MAYBE_REF
Expand Down
86 changes: 86 additions & 0 deletions packages/runtime-core/apiMixed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { defineComponent, h, renderToString } from '@vue/runtime-test'

describe('api: options', () => {
test('mix api options: setup and data with created', () => {
const mixinA = defineComponent({
setup() {
return {
a: 'from setup',
}
},
data() {
return {
a: 'from data',
}
},
created(this: any) {
this.a = 'from created'
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from created`)
})

test('mix api options: data and setup with created', () => {
const mixinA = defineComponent({
data() {
return {
a: 'from data',
}
},
setup() {
return {
a: 'from setup',
}
},
created(this: any) {
this.a = 'from created'
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from created`)
})

test('mix api options: data and setup', () => {
const mixinA = defineComponent({
data() {
return {
a: 'from data',
}
},
setup() {
return {
a: 'from setup',
}
},
created(this: any) {},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from setup`)
})

test('mix api options: setup and data', () => {
const mixinA = defineComponent({
setup() {
return {
a: 'from setup',
}
},
data() {
return {
a: 'from data',
}
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from setup`)
})
})