From 338d869c70066fc449adf06ce9a6250bbdb1e2af Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 4 Dec 2020 16:46:00 -0500 Subject: [PATCH] test(compiler-core): test expression transform w/ bindingMetadata --- .../transformExpressions.spec.ts.snap | 15 ++++++ .../transforms/transformExpressions.spec.ts | 50 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap new file mode 100644 index 00000000..782d7a5e --- /dev/null +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`compiler: expression transform bindingMetadata inline mode 1`] = ` +"(_ctx, _cache) => { + return (_openBlock(), _createBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options), 1 /* TEXT */)) +}" +`; + +exports[`compiler: expression transform bindingMetadata non-inline mode 1`] = ` +"const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue + +return function render(_ctx, _cache, $props, $setup, $data, $options) { + return (_openBlock(), _createBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options), 1 /* TEXT */)) +}" +`; diff --git a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts index e3d7e505..2466fffe 100644 --- a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts @@ -6,7 +6,9 @@ import { NodeTypes, CompilerOptions, InterpolationNode, - ConstantTypes + ConstantTypes, + BindingTypes, + baseCompile } from '../../src' import { transformIf } from '../../src/transforms/vIf' import { transformExpression } from '../../src/transforms/transformExpression' @@ -457,4 +459,50 @@ describe('compiler: expression transform', () => { }) }) }) + + describe('bindingMetadata', () => { + const bindingMetadata = { + props: BindingTypes.PROPS, + setup: BindingTypes.SETUP_MAYBE_REF, + setupConst: BindingTypes.SETUP_CONST, + data: BindingTypes.DATA, + options: BindingTypes.OPTIONS + } + + function compileWithBindingMetadata( + template: string, + options?: CompilerOptions + ) { + return baseCompile(template, { + prefixIdentifiers: true, + bindingMetadata, + ...options + }) + } + + test('non-inline mode', () => { + const { code } = compileWithBindingMetadata( + `
{{ props }} {{ setup }} {{ data }} {{ options }}
` + ) + expect(code).toMatch(`$props.props`) + expect(code).toMatch(`$setup.setup`) + expect(code).toMatch(`$data.data`) + expect(code).toMatch(`$options.options`) + expect(code).toMatch(`_ctx, _cache, $props, $setup, $data, $options`) + expect(code).toMatchSnapshot() + }) + + test('inline mode', () => { + const { code } = compileWithBindingMetadata( + `
{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }}
`, + { inline: true } + ) + expect(code).toMatch(`__props.props`) + expect(code).toMatch(`_unref(setup)`) + expect(code).toMatch(`_toDisplayString(setupConst)`) + expect(code).toMatch(`_ctx.data`) + expect(code).toMatch(`_ctx.options`) + expect(code).toMatchSnapshot() + }) + }) })