vue3-yuanma/packages/compiler-dom/__tests__/transforms/transformStyle.spec.ts

84 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-09-26 09:02:46 +08:00
import {
baseParse as parse,
2019-09-26 09:02:46 +08:00
transform,
CompilerOptions,
ElementNode,
2019-10-04 11:30:25 +08:00
NodeTypes,
CallExpression
} from '@vue/compiler-core'
import { transformBind } from '../../../compiler-core/src/transforms/vBind'
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
2019-09-26 09:02:46 +08:00
import { transformStyle } from '../../src/transforms/transformStyle'
function transformWithStyleTransform(
template: string,
options: CompilerOptions = {}
) {
const ast = parse(template)
transform(ast, {
nodeTransforms: [transformStyle],
...options
})
return {
root: ast,
node: ast.children[0] as ElementNode
}
}
describe('compiler: style transform', () => {
test('should transform into directive node and hoist value', () => {
const { root, node } = transformWithStyleTransform(
`<div style="color: red"/>`
)
expect(root.hoists).toMatchObject([
{
type: NodeTypes.SIMPLE_EXPRESSION,
2019-09-26 09:02:46 +08:00
content: `{"color":"red"}`,
isStatic: false
}
])
expect(node.props[0]).toMatchObject({
type: NodeTypes.DIRECTIVE,
name: `bind`,
arg: {
type: NodeTypes.SIMPLE_EXPRESSION,
2019-09-26 09:02:46 +08:00
content: `style`,
isStatic: true
},
exp: {
type: NodeTypes.SIMPLE_EXPRESSION,
2019-09-26 09:02:46 +08:00
content: `_hoisted_1`,
isStatic: false
}
})
})
test('working with v-bind transform', () => {
const { node } = transformWithStyleTransform(`<div style="color: red"/>`, {
nodeTransforms: [transformStyle, transformElement],
directiveTransforms: {
bind: transformBind
}
})
2019-10-04 11:30:25 +08:00
expect((node.codegenNode as CallExpression).arguments[1]).toMatchObject({
2019-09-26 09:02:46 +08:00
type: NodeTypes.JS_OBJECT_EXPRESSION,
properties: [
{
key: {
type: NodeTypes.SIMPLE_EXPRESSION,
2019-09-26 09:02:46 +08:00
content: `style`,
isStatic: true
},
value: {
type: NodeTypes.SIMPLE_EXPRESSION,
2019-09-26 09:02:46 +08:00
content: `_hoisted_1`,
isStatic: false
}
}
]
})
// should not cause the STYLE patchFlag to be attached
expect((node.codegenNode as CallExpression).arguments.length).toBe(2)
2019-09-26 09:02:46 +08:00
})
})