From 6daba537502427c3c46357438355dd088e61e4d9 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Tue, 17 May 2016 01:56:53 +0900 Subject: [PATCH] add optimizer tests (#2882) ref: #2872 --- test/unit/modules/compiler/optimizer.spec.js | 210 +++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/test/unit/modules/compiler/optimizer.spec.js b/test/unit/modules/compiler/optimizer.spec.js index e69de29b..d3d069c4 100644 --- a/test/unit/modules/compiler/optimizer.spec.js +++ b/test/unit/modules/compiler/optimizer.spec.js @@ -0,0 +1,210 @@ +import { parse } from 'compiler/parser/index' +import { optimize } from 'compiler/optimizer' +import directives from 'web/compiler/directives/index' +import { isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from 'web/util/index' + +describe('optimizer', () => { + const baseOptions = { + expectHTML: true, + preserveWhitespace: true, + directives, + isReservedTag, + isUnaryTag, + mustUseProp, + getTagNamespace + } + + it('simple', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(true) // h1 + expect(ast.staticRoot).toBe(true) + expect(ast.children[0].static).toBe(true) // text node + }) + + it('interpolation', () => { + const ast = parse('

{{msg}}

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // h1 + expect(ast.children[0].static).toBe(false) // text node with interpolation + }) + + it('nested elements', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + // ul + expect(ast.static).toBe(true) + expect(ast.staticRoot).toBe(true) + // li + expect(ast.children[0].static).toBe(true) // first + expect(ast.children[1].static).toBe(true) // second + // text node inside li + expect(ast.children[0].children[0].static).toBe(true) // first + expect(ast.children[1].children[0].static).toBe(true) // second + }) + + it('nested complex elements', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + // ul + expect(ast.static).toBe(false) // ul + // li + expect(ast.children[0].static).toBe(false) // firts + expect(ast.children[1].static).toBe(true) // second + expect(ast.children[2].static).toBe(false) // third + // text node inside li + expect(ast.children[0].children[0].static).toBe(false) // first + expect(ast.children[1].children[0].static).toBe(true) // second + expect(ast.children[2].children[0].static).toBe(false) // third + }) + + it('v-if directive', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(true) + }) + + it('v-else directive', () => { + const ast = parse('

hello world

foo bar

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(false) + expect(ast.children[0].elseBlock.static).toBeUndefined() + }) + + it('v-pre directive', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(true) + expect(ast.staticRoot).toBe(true) + expect(ast.children[0].static).toBe(true) + expect(ast.children[1].static).toBe(true) + expect(ast.children[0].children[0].static).toBe(true) + expect(ast.children[1].children[0].static).toBe(true) + }) + + it('v-for directive', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + // ul + expect(ast.static).toBe(false) + // li with v-for + expect(ast.children[0].static).toBe(false) + expect(ast.children[0].children[0].static).toBe(false) + }) + + it('v-once directive', () => { + const ast = parse('

{{msg}}

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // p + expect(ast.children[0].static).toBe(false) // text node + }) + + it('render tag', () => { + const ast = parse('

hello

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(true) + expect(ast.children[0].children[0].static).toBe(true) + }) + + it('single slot', () => { + const ast = parse('hello', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // slot + expect(ast.children[0].static).toBe(true) // text node + }) + + it('named slot', () => { + const ast = parse('hello world', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // slot + expect(ast.children[0].static).toBe(true) // text node + }) + + it('slot target', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // slot + expect(ast.children[0].static).toBe(true) // text node + }) + + it('component', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) // component + }) + + it('component for inline-template', () => { + const ast = parse('

hello world

{{msg}}

', baseOptions) + optimize(ast, baseOptions) + // component + expect(ast.static).toBe(false) // component + // p + expect(ast.children[0].static).toBe(true) // first + expect(ast.children[1].static).toBe(false) // second + // text node inside p + expect(ast.children[0].children[0].static).toBe(true) // first + expect(ast.children[1].children[0].static).toBe(false) // second + }) + + it('class binding', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(true) + }) + + it('style binding', () => { + const ast = parse('

{{msg}}

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(false) + }) + + it('transition', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(true) + }) + + it('v-bind directive', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + }) + + it('v-ref directive', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(true) + }) + + it('v-on directive', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + }) + + it('custom directive', () => { + const ast = parse('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(false) + expect(ast.children[0].static).toBe(false) + }) + + it('not root ast', () => { + const ast = null + optimize(ast, baseOptions) + expect(ast).toBe(null) + }) + + it('not specified isReservedTag option', () => { + const ast = parse('

hello world

', baseOptions) + optimize(ast, {}) + expect(ast.static).toBe(false) + }) +})