]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(compiler-core): add v-if with comments test case (#1389)
authorJoseph Xia <15906475@qq.com>
Wed, 17 Jun 2020 20:01:12 +0000 (04:01 +0800)
committerGitHub <noreply@github.com>
Wed, 17 Jun 2020 20:01:12 +0000 (16:01 -0400)
packages/compiler-core/__tests__/transforms/vIf.spec.ts

index 0ded2ac20671507c8ea4b117829a9a40c1fce7f9..927e2d1d291456a9dea01b1f5f8e55978578788f 100644 (file)
@@ -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(`
+          <template v-if="ok">
+            <!--comment1-->
+            <div v-if="ok2">
+              <!--comment2-->
+            </div>
+            <!--comment3-->
+            <b v-else/>
+            <!--comment4-->
+            <p/>
+          </template>
+        `)
+      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`)
+    })
   })
 })