refactor(template-ref): improve template ref handling

close #836, close #839
This commit is contained in:
Evan You
2020-03-16 12:40:58 -04:00
parent 8a58dce603
commit 9ad65b1653
6 changed files with 55 additions and 114 deletions

View File

@@ -1,49 +0,0 @@
import { baseParse as parse } from '../../src/parse'
import { transform } from '../../src/transform'
import { transformRef } from '../../src/transforms/transformRef'
import { ElementNode, NodeTypes } from '../../src/ast'
function transformWithRef(template: string) {
const ast = parse(template)
transform(ast, {
nodeTransforms: [transformRef]
})
return ast.children[0] as ElementNode
}
describe('compiler: transform ref', () => {
const getExpected = (key: any) => ({
type: NodeTypes.DIRECTIVE,
name: 'bind',
arg: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `ref`
},
exp: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`[_ctx, `, key, `]`]
}
})
test('static', () => {
const node = transformWithRef(`<div ref="test"/>`)
expect(node.props[0]).toMatchObject(
getExpected({
type: NodeTypes.SIMPLE_EXPRESSION,
content: `test`,
isStatic: true
})
)
})
test('dynamic', () => {
const node = transformWithRef(`<div :ref="test"/>`)
expect(node.props[0]).toMatchObject(
getExpected({
type: NodeTypes.SIMPLE_EXPRESSION,
content: `test`,
isStatic: false
})
)
})
})