feat(core): support dynamic component via <component :is> (#320)

This commit is contained in:
宋铄运
2019-10-19 00:09:04 +08:00
committed by Evan You
parent d179918001
commit 7f23eaf661
6 changed files with 133 additions and 22 deletions

View File

@@ -7,7 +7,8 @@ import {
APPLY_DIRECTIVES,
TO_HANDLERS,
helperNameMap,
PORTAL
PORTAL,
RESOLVE_DYNAMIC_COMPONENT
} from '../../src/runtimeHelpers'
import {
CallExpression,
@@ -47,6 +48,14 @@ function parseWithElementTransform(
}
}
function parseWithBind(template: string) {
return parseWithElementTransform(template, {
directiveTransforms: {
bind: transformBind
}
})
}
describe('compiler: element transform', () => {
test('import + resolve component', () => {
const { root } = parseWithElementTransform(`<Foo/>`)
@@ -626,14 +635,6 @@ describe('compiler: element transform', () => {
})
describe('patchFlag analysis', () => {
function parseWithBind(template: string) {
return parseWithElementTransform(template, {
directiveTransforms: {
bind: transformBind
}
})
}
test('TEXT', () => {
const { node } = parseWithBind(`<div>foo</div>`)
expect(node.arguments.length).toBe(3)
@@ -717,4 +718,31 @@ describe('compiler: element transform', () => {
expect(vnodeCall.arguments[3]).toBe(genFlagText(PatchFlags.NEED_PATCH))
})
})
describe('dynamic component', () => {
test('static binding', () => {
const { node, root } = parseWithBind(`<component is="foo" />`)
expect(root.helpers).not.toContain(RESOLVE_DYNAMIC_COMPONENT)
expect(node).toMatchObject({
callee: CREATE_VNODE,
arguments: ['_component_foo']
})
})
test('dynamic binding', () => {
const { node, root } = parseWithBind(`<component :is="foo" />`)
expect(root.helpers).toContain(RESOLVE_DYNAMIC_COMPONENT)
expect(node.arguments).toMatchObject([
{
callee: RESOLVE_DYNAMIC_COMPONENT,
arguments: [
{
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'foo'
}
]
}
])
})
})
})