Skip to content

Commit b5d21ae

Browse files
committed
test: test for transformStyle
1 parent 6c8f226 commit b5d21ae

File tree

2 files changed

+162
-2
lines changed

2 files changed

+162
-2
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
transform,
66
ErrorCodes
77
} from '../../src'
8-
import { transformElement } from '../../src/transforms/transformElement'
98
import {
109
RESOLVE_COMPONENT,
1110
CREATE_VNODE,
@@ -21,6 +20,10 @@ import {
2120
DirectiveNode,
2221
RootNode
2322
} from '../../src/ast'
23+
import { transformElement } from '../../src/transforms/transformElement'
24+
import { transformOn } from '../../src/transforms/vOn'
25+
import { transformStyle } from '../../src/transforms/transformStyle'
26+
import { transformBind } from '../../src/transforms/vBind'
2427

2528
function parseWithElementTransform(
2629
template: string,
@@ -466,7 +469,84 @@ describe('compiler: element transform', () => {
466469
])
467470
})
468471

469-
test.todo(`props dedupe`)
472+
test(`props merging: event handlers`, () => {
473+
const { node } = parseWithElementTransform(
474+
`<div @click.foo="a" @click.bar="b" />`,
475+
{
476+
directiveTransforms: {
477+
on: transformOn
478+
}
479+
}
480+
)
481+
expect(node.arguments[1]).toMatchObject({
482+
type: NodeTypes.JS_OBJECT_EXPRESSION,
483+
properties: [
484+
{
485+
type: NodeTypes.JS_PROPERTY,
486+
key: {
487+
type: NodeTypes.EXPRESSION,
488+
content: `onClick`,
489+
isStatic: true
490+
},
491+
value: {
492+
type: NodeTypes.JS_ARRAY_EXPRESSION,
493+
elements: [
494+
{
495+
type: NodeTypes.EXPRESSION,
496+
content: `a`,
497+
isStatic: false
498+
},
499+
{
500+
type: NodeTypes.EXPRESSION,
501+
content: `b`,
502+
isStatic: false
503+
}
504+
]
505+
}
506+
}
507+
]
508+
})
509+
})
510+
511+
test(`props merging: style`, () => {
512+
const { node } = parseWithElementTransform(
513+
`<div style="color: red" :style="{ color: 'red' }" />`,
514+
{
515+
nodeTransforms: [transformStyle, transformElement],
516+
directiveTransforms: {
517+
bind: transformBind
518+
}
519+
}
520+
)
521+
expect(node.arguments[1]).toMatchObject({
522+
type: NodeTypes.JS_OBJECT_EXPRESSION,
523+
properties: [
524+
{
525+
type: NodeTypes.JS_PROPERTY,
526+
key: {
527+
type: NodeTypes.EXPRESSION,
528+
content: `style`,
529+
isStatic: true
530+
},
531+
value: {
532+
type: NodeTypes.JS_ARRAY_EXPRESSION,
533+
elements: [
534+
{
535+
type: NodeTypes.EXPRESSION,
536+
content: `_hoisted_1`,
537+
isStatic: false
538+
},
539+
{
540+
type: NodeTypes.EXPRESSION,
541+
content: `{ color: 'red' }`,
542+
isStatic: false
543+
}
544+
]
545+
}
546+
}
547+
]
548+
})
549+
})
470550

471551
test.todo('slot outlets')
472552
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
parse,
3+
transform,
4+
CompilerOptions,
5+
ElementNode,
6+
NodeTypes
7+
} from '../../src'
8+
import { transformStyle } from '../../src/transforms/transformStyle'
9+
import { transformBind } from '../../src/transforms/vBind'
10+
import { transformElement } from '../../src/transforms/transformElement'
11+
12+
function transformWithStyleTransform(
13+
template: string,
14+
options: CompilerOptions = {}
15+
) {
16+
const ast = parse(template)
17+
transform(ast, {
18+
nodeTransforms: [transformStyle],
19+
...options
20+
})
21+
return {
22+
root: ast,
23+
node: ast.children[0] as ElementNode
24+
}
25+
}
26+
27+
describe('compiler: style transform', () => {
28+
test('should transform into directive node and hoist value', () => {
29+
const { root, node } = transformWithStyleTransform(
30+
`<div style="color: red"/>`
31+
)
32+
expect(root.hoists).toMatchObject([
33+
{
34+
type: NodeTypes.EXPRESSION,
35+
content: `{"color":"red"}`,
36+
isStatic: false
37+
}
38+
])
39+
expect(node.props[0]).toMatchObject({
40+
type: NodeTypes.DIRECTIVE,
41+
name: `bind`,
42+
arg: {
43+
type: NodeTypes.EXPRESSION,
44+
content: `style`,
45+
isStatic: true
46+
},
47+
exp: {
48+
type: NodeTypes.EXPRESSION,
49+
content: `_hoisted_1`,
50+
isStatic: false
51+
}
52+
})
53+
})
54+
55+
test('working with v-bind transform', () => {
56+
const { node } = transformWithStyleTransform(`<div style="color: red"/>`, {
57+
nodeTransforms: [transformStyle, transformElement],
58+
directiveTransforms: {
59+
bind: transformBind
60+
}
61+
})
62+
expect(node.codegenNode!.arguments[1]).toMatchObject({
63+
type: NodeTypes.JS_OBJECT_EXPRESSION,
64+
properties: [
65+
{
66+
key: {
67+
type: NodeTypes.EXPRESSION,
68+
content: `style`,
69+
isStatic: true
70+
},
71+
value: {
72+
type: NodeTypes.EXPRESSION,
73+
content: `_hoisted_1`,
74+
isStatic: false
75+
}
76+
}
77+
]
78+
})
79+
})
80+
})

0 commit comments

Comments
 (0)