From d57749cbf7e092d9ee6b6685d140600758ee59b0 Mon Sep 17 00:00:00 2001 From: Joseph Xia <15906475@qq.com> Date: Thu, 18 Jun 2020 04:01:12 +0800 Subject: [PATCH] test(compiler-core): add v-if with comments test case (#1389) --- .../__tests__/transforms/vIf.spec.ts | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index 0ded2ac20..927e2d1d2 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -13,7 +13,8 @@ import { ConditionalExpression, IfConditionalExpression, VNodeCall, - ElementTypes + ElementTypes, + IfBranchNode } from '../../src/ast' import { ErrorCodes } from '../../src/errors' import { CompilerOptions, generate } from '../../src' @@ -527,6 +528,50 @@ describe('compiler: v-if', () => { expect(generate(root).code).toMatchSnapshot() }) - test.todo('with comments') + test('with comments', () => { + const { node } = parseWithIfTransform(` + + `) + expect(node.type).toBe(NodeTypes.IF) + expect(node.branches.length).toBe(1) + + const b1 = node.branches[0] + expect((b1.condition as SimpleExpressionNode).content).toBe(`ok`) + expect(b1.children.length).toBe(4) + + expect(b1.children[0].type).toBe(NodeTypes.COMMENT) + expect((b1.children[0] as CommentNode).content).toBe(`comment1`) + + expect(b1.children[1].type).toBe(NodeTypes.IF) + expect((b1.children[1] as IfNode).branches.length).toBe(2) + const b1b1: ElementNode = (b1.children[1] as IfNode).branches[0] + .children[0] as ElementNode + expect(b1b1.type).toBe(NodeTypes.ELEMENT) + expect(b1b1.tag).toBe('div') + expect(b1b1.children[0].type).toBe(NodeTypes.COMMENT) + expect((b1b1.children[0] as CommentNode).content).toBe('comment2') + + const b1b2: IfBranchNode = (b1.children[1] as IfNode) + .branches[1] as IfBranchNode + expect(b1b2.children[0].type).toBe(NodeTypes.COMMENT) + expect((b1b2.children[0] as CommentNode).content).toBe(`comment3`) + expect(b1b2.children[1].type).toBe(NodeTypes.ELEMENT) + expect((b1b2.children[1] as ElementNode).tag).toBe(`b`) + + expect(b1.children[2].type).toBe(NodeTypes.COMMENT) + expect((b1.children[2] as CommentNode).content).toBe(`comment4`) + + expect(b1.children[3].type).toBe(NodeTypes.ELEMENT) + expect((b1.children[3] as ElementNode).tag).toBe(`p`) + }) }) })