Skip to content

Commit

Permalink
test(transformElement): add test for transforming replaced nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-LL authored and yyx990803 committed Mar 29, 2021
1 parent 58601db commit ac6af76
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
baseParse as parse,
transform,
ErrorCodes,
BindingTypes
BindingTypes,
NodeTransform
} from '../../src'
import {
RESOLVE_COMPONENT,
Expand Down Expand Up @@ -939,4 +940,35 @@ describe('compiler: element transform', () => {
isBlock: true
})
})

test('should process node when node has been replaced', () => {
// a NodeTransform that swaps out <div id="foo" /> with <span id="foo" />
const customNodeTransform: NodeTransform = (node, context) => {
if (
node.type === NodeTypes.ELEMENT &&
node.tag === 'div' &&
node.props.some(
prop =>
prop.type === NodeTypes.ATTRIBUTE &&
prop.name === 'id' &&
prop.value &&
prop.value.content === 'foo'
)
) {
context.replaceNode({
...node,
tag: 'span'
})
}
}
const ast = parse(`<div><div id="foo" /></div>`)
transform(ast, {
nodeTransforms: [transformElement, transformText, customNodeTransform]
})
expect((ast as any).children[0].children[0].codegenNode).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: '"span"',
isBlock: false
})
})
})

0 comments on commit ac6af76

Please sign in to comment.