Skip to content

Commit 338d869

Browse files
committed
test(compiler-core): test expression transform w/ bindingMetadata
1 parent d2d27b2 commit 338d869

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`compiler: expression transform bindingMetadata inline mode 1`] = `
4+
"(_ctx, _cache) => {
5+
return (_openBlock(), _createBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options), 1 /* TEXT */))
6+
}"
7+
`;
8+
9+
exports[`compiler: expression transform bindingMetadata non-inline mode 1`] = `
10+
"const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
11+
12+
return function render(_ctx, _cache, $props, $setup, $data, $options) {
13+
return (_openBlock(), _createBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options), 1 /* TEXT */))
14+
}"
15+
`;

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

+49-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
NodeTypes,
77
CompilerOptions,
88
InterpolationNode,
9-
ConstantTypes
9+
ConstantTypes,
10+
BindingTypes,
11+
baseCompile
1012
} from '../../src'
1113
import { transformIf } from '../../src/transforms/vIf'
1214
import { transformExpression } from '../../src/transforms/transformExpression'
@@ -457,4 +459,50 @@ describe('compiler: expression transform', () => {
457459
})
458460
})
459461
})
462+
463+
describe('bindingMetadata', () => {
464+
const bindingMetadata = {
465+
props: BindingTypes.PROPS,
466+
setup: BindingTypes.SETUP_MAYBE_REF,
467+
setupConst: BindingTypes.SETUP_CONST,
468+
data: BindingTypes.DATA,
469+
options: BindingTypes.OPTIONS
470+
}
471+
472+
function compileWithBindingMetadata(
473+
template: string,
474+
options?: CompilerOptions
475+
) {
476+
return baseCompile(template, {
477+
prefixIdentifiers: true,
478+
bindingMetadata,
479+
...options
480+
})
481+
}
482+
483+
test('non-inline mode', () => {
484+
const { code } = compileWithBindingMetadata(
485+
`<div>{{ props }} {{ setup }} {{ data }} {{ options }}</div>`
486+
)
487+
expect(code).toMatch(`$props.props`)
488+
expect(code).toMatch(`$setup.setup`)
489+
expect(code).toMatch(`$data.data`)
490+
expect(code).toMatch(`$options.options`)
491+
expect(code).toMatch(`_ctx, _cache, $props, $setup, $data, $options`)
492+
expect(code).toMatchSnapshot()
493+
})
494+
495+
test('inline mode', () => {
496+
const { code } = compileWithBindingMetadata(
497+
`<div>{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }}</div>`,
498+
{ inline: true }
499+
)
500+
expect(code).toMatch(`__props.props`)
501+
expect(code).toMatch(`_unref(setup)`)
502+
expect(code).toMatch(`_toDisplayString(setupConst)`)
503+
expect(code).toMatch(`_ctx.data`)
504+
expect(code).toMatch(`_ctx.options`)
505+
expect(code).toMatchSnapshot()
506+
})
507+
})
460508
})

0 commit comments

Comments
 (0)