]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: test for transformStyle
authorEvan You <yyx990803@gmail.com>
Thu, 26 Sep 2019 01:02:46 +0000 (21:02 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 26 Sep 2019 01:02:46 +0000 (21:02 -0400)
packages/compiler-core/__tests__/transforms/transformElement.spec.ts
packages/compiler-core/__tests__/transforms/transformStyle.spec.ts [new file with mode: 0644]

index eb11cda61faf317f52e00a4f07fdffc2da30215d..138676b929f9b243c534ef12b67c8de119960e20 100644 (file)
@@ -5,7 +5,6 @@ import {
   transform,
   ErrorCodes
 } from '../../src'
-import { transformElement } from '../../src/transforms/transformElement'
 import {
   RESOLVE_COMPONENT,
   CREATE_VNODE,
@@ -21,6 +20,10 @@ import {
   DirectiveNode,
   RootNode
 } from '../../src/ast'
+import { transformElement } from '../../src/transforms/transformElement'
+import { transformOn } from '../../src/transforms/vOn'
+import { transformStyle } from '../../src/transforms/transformStyle'
+import { transformBind } from '../../src/transforms/vBind'
 
 function parseWithElementTransform(
   template: string,
@@ -466,7 +469,84 @@ describe('compiler: element transform', () => {
     ])
   })
 
-  test.todo(`props dedupe`)
+  test(`props merging: event handlers`, () => {
+    const { node } = parseWithElementTransform(
+      `<div @click.foo="a" @click.bar="b" />`,
+      {
+        directiveTransforms: {
+          on: transformOn
+        }
+      }
+    )
+    expect(node.arguments[1]).toMatchObject({
+      type: NodeTypes.JS_OBJECT_EXPRESSION,
+      properties: [
+        {
+          type: NodeTypes.JS_PROPERTY,
+          key: {
+            type: NodeTypes.EXPRESSION,
+            content: `onClick`,
+            isStatic: true
+          },
+          value: {
+            type: NodeTypes.JS_ARRAY_EXPRESSION,
+            elements: [
+              {
+                type: NodeTypes.EXPRESSION,
+                content: `a`,
+                isStatic: false
+              },
+              {
+                type: NodeTypes.EXPRESSION,
+                content: `b`,
+                isStatic: false
+              }
+            ]
+          }
+        }
+      ]
+    })
+  })
+
+  test(`props merging: style`, () => {
+    const { node } = parseWithElementTransform(
+      `<div style="color: red" :style="{ color: 'red' }" />`,
+      {
+        nodeTransforms: [transformStyle, transformElement],
+        directiveTransforms: {
+          bind: transformBind
+        }
+      }
+    )
+    expect(node.arguments[1]).toMatchObject({
+      type: NodeTypes.JS_OBJECT_EXPRESSION,
+      properties: [
+        {
+          type: NodeTypes.JS_PROPERTY,
+          key: {
+            type: NodeTypes.EXPRESSION,
+            content: `style`,
+            isStatic: true
+          },
+          value: {
+            type: NodeTypes.JS_ARRAY_EXPRESSION,
+            elements: [
+              {
+                type: NodeTypes.EXPRESSION,
+                content: `_hoisted_1`,
+                isStatic: false
+              },
+              {
+                type: NodeTypes.EXPRESSION,
+                content: `{ color: 'red' }`,
+                isStatic: false
+              }
+            ]
+          }
+        }
+      ]
+    })
+  })
 
   test.todo('slot outlets')
 })
diff --git a/packages/compiler-core/__tests__/transforms/transformStyle.spec.ts b/packages/compiler-core/__tests__/transforms/transformStyle.spec.ts
new file mode 100644 (file)
index 0000000..1981277
--- /dev/null
@@ -0,0 +1,80 @@
+import {
+  parse,
+  transform,
+  CompilerOptions,
+  ElementNode,
+  NodeTypes
+} from '../../src'
+import { transformStyle } from '../../src/transforms/transformStyle'
+import { transformBind } from '../../src/transforms/vBind'
+import { transformElement } from '../../src/transforms/transformElement'
+
+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.EXPRESSION,
+        content: `{"color":"red"}`,
+        isStatic: false
+      }
+    ])
+    expect(node.props[0]).toMatchObject({
+      type: NodeTypes.DIRECTIVE,
+      name: `bind`,
+      arg: {
+        type: NodeTypes.EXPRESSION,
+        content: `style`,
+        isStatic: true
+      },
+      exp: {
+        type: NodeTypes.EXPRESSION,
+        content: `_hoisted_1`,
+        isStatic: false
+      }
+    })
+  })
+
+  test('working with v-bind transform', () => {
+    const { node } = transformWithStyleTransform(`<div style="color: red"/>`, {
+      nodeTransforms: [transformStyle, transformElement],
+      directiveTransforms: {
+        bind: transformBind
+      }
+    })
+    expect(node.codegenNode!.arguments[1]).toMatchObject({
+      type: NodeTypes.JS_OBJECT_EXPRESSION,
+      properties: [
+        {
+          key: {
+            type: NodeTypes.EXPRESSION,
+            content: `style`,
+            isStatic: true
+          },
+          value: {
+            type: NodeTypes.EXPRESSION,
+            content: `_hoisted_1`,
+            isStatic: false
+          }
+        }
+      ]
+    })
+  })
+})