|
|
@@ -1,28 +1,109 @@
|
|
|
-import { parse, transform, ElementNode, CallExpression } from '../../src'
|
|
|
+import {
|
|
|
+ parse,
|
|
|
+ transform,
|
|
|
+ NodeTypes,
|
|
|
+ generate,
|
|
|
+ CompilerOptions
|
|
|
+} from '../../src'
|
|
|
import { transformOnce } from '../../src/transforms/vOnce'
|
|
|
import { transformElement } from '../../src/transforms/transformElement'
|
|
|
-import { createObjectMatcher } from '../testUtils'
|
|
|
+import {
|
|
|
+ CREATE_VNODE,
|
|
|
+ RENDER_SLOT,
|
|
|
+ SET_BLOCK_TRACKING
|
|
|
+} from '../../src/runtimeHelpers'
|
|
|
+import { transformBind } from '../../src/transforms/vBind'
|
|
|
+import { transformSlotOutlet } from '../../src/transforms/transformSlotOutlet'
|
|
|
|
|
|
-function transformWithOnce(template: string) {
|
|
|
+function transformWithOnce(template: string, options: CompilerOptions = {}) {
|
|
|
const ast = parse(template)
|
|
|
transform(ast, {
|
|
|
- nodeTransforms: [transformElement],
|
|
|
+ nodeTransforms: [transformOnce, transformElement, transformSlotOutlet],
|
|
|
directiveTransforms: {
|
|
|
- once: transformOnce
|
|
|
- }
|
|
|
+ bind: transformBind
|
|
|
+ },
|
|
|
+ ...options
|
|
|
})
|
|
|
- return ast.children[0] as ElementNode
|
|
|
+ return ast
|
|
|
}
|
|
|
|
|
|
describe('compiler: v-once transform', () => {
|
|
|
- test('should add no props to DOM', () => {
|
|
|
- const node = transformWithOnce(`<div v-once />`)
|
|
|
- const codegenArgs = (node.codegenNode as CallExpression).arguments
|
|
|
+ test('as root node', () => {
|
|
|
+ const root = transformWithOnce(`<div :id="foo" v-once />`)
|
|
|
+ expect(root.cached).toBe(1)
|
|
|
+ expect(root.helpers).toContain(SET_BLOCK_TRACKING)
|
|
|
+ expect(root.codegenNode).toMatchObject({
|
|
|
+ type: NodeTypes.JS_CACHE_EXPRESSION,
|
|
|
+ index: 1,
|
|
|
+ value: {
|
|
|
+ type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
+ callee: CREATE_VNODE
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('on nested plain element', () => {
|
|
|
+ const root = transformWithOnce(`<div><div :id="foo" v-once /></div>`)
|
|
|
+ expect(root.cached).toBe(1)
|
|
|
+ expect(root.helpers).toContain(SET_BLOCK_TRACKING)
|
|
|
+ expect((root.children[0] as any).children[0].codegenNode).toMatchObject({
|
|
|
+ type: NodeTypes.JS_CACHE_EXPRESSION,
|
|
|
+ index: 1,
|
|
|
+ value: {
|
|
|
+ type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
+ callee: CREATE_VNODE
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('on component', () => {
|
|
|
+ const root = transformWithOnce(`<div><Comp :id="foo" v-once /></div>`)
|
|
|
+ expect(root.cached).toBe(1)
|
|
|
+ expect(root.helpers).toContain(SET_BLOCK_TRACKING)
|
|
|
+ expect((root.children[0] as any).children[0].codegenNode).toMatchObject({
|
|
|
+ type: NodeTypes.JS_CACHE_EXPRESSION,
|
|
|
+ index: 1,
|
|
|
+ value: {
|
|
|
+ type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
+ callee: CREATE_VNODE
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('on slot outlet', () => {
|
|
|
+ const root = transformWithOnce(`<div><slot v-once /></div>`)
|
|
|
+ expect(root.cached).toBe(1)
|
|
|
+ expect(root.helpers).toContain(SET_BLOCK_TRACKING)
|
|
|
+ expect((root.children[0] as any).children[0].codegenNode).toMatchObject({
|
|
|
+ type: NodeTypes.JS_CACHE_EXPRESSION,
|
|
|
+ index: 1,
|
|
|
+ value: {
|
|
|
+ type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
+ callee: RENDER_SLOT
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
|
|
|
- expect(codegenArgs[1]).toMatchObject(
|
|
|
- createObjectMatcher({
|
|
|
- $once: `[true]`
|
|
|
- })
|
|
|
- )
|
|
|
+ // cached nodes should be ignored by hoistStatic transform
|
|
|
+ test('with hoistStatic: true', () => {
|
|
|
+ const root = transformWithOnce(`<div><div v-once /></div>`, {
|
|
|
+ hoistStatic: true
|
|
|
+ })
|
|
|
+ expect(root.cached).toBe(1)
|
|
|
+ expect(root.helpers).toContain(SET_BLOCK_TRACKING)
|
|
|
+ expect(root.hoists.length).toBe(0)
|
|
|
+ expect((root.children[0] as any).children[0].codegenNode).toMatchObject({
|
|
|
+ type: NodeTypes.JS_CACHE_EXPRESSION,
|
|
|
+ index: 1,
|
|
|
+ value: {
|
|
|
+ type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
+ callee: CREATE_VNODE
|
|
|
+ }
|
|
|
+ })
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
})
|
|
|
})
|