From ba29b4c89af223a3d3a0945561d18ff2acedd793 Mon Sep 17 00:00:00 2001 From: FireBushtree Date: Wed, 21 Feb 2024 17:08:20 +0800 Subject: [PATCH] test(compiler-vapor): v-model (#132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 三咲智子 Kevin Deng --- .../__snapshots__/vModel.spec.ts.snap | 236 ++++++++++++++++++ .../__tests__/transforms/vModel.spec.ts | 176 ++++++++++++- packages/compiler-vapor/src/index.ts | 1 + 3 files changed, 410 insertions(+), 3 deletions(-) create mode 100644 packages/compiler-vapor/__tests__/transforms/__snapshots__/vModel.spec.ts.snap diff --git a/packages/compiler-vapor/__tests__/transforms/__snapshots__/vModel.spec.ts.snap b/packages/compiler-vapor/__tests__/transforms/__snapshots__/vModel.spec.ts.snap new file mode 100644 index 000000000..2e968e8d5 --- /dev/null +++ b/packages/compiler-vapor/__tests__/transforms/__snapshots__/vModel.spec.ts.snap @@ -0,0 +1,236 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`compiler: vModel transform > modifiers > .lazy 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model, void 0, { lazy: true }]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > modifiers > .number 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model, void 0, { number: true }]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > modifiers > .trim 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model, void 0, { trim: true }]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support input (checkbox) 1`] = ` +"import { children as _children, vModelCheckbox as _vModelCheckbox, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelCheckbox, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support input (dynamic type) 1`] = ` +"import { children as _children, vModelDynamic as _vModelDynamic, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelDynamic, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support input (radio) 1`] = ` +"import { children as _children, vModelRadio as _vModelRadio, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelRadio, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support input (text) 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support select 1`] = ` +"import { children as _children, vModelSelect as _vModelSelect, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelSelect, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support simple expression 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support textarea 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support w/ dynamic v-bind 1`] = ` +"import { children as _children, vModelDynamic as _vModelDynamic, withDirectives as _withDirectives, on as _on, renderEffect as _renderEffect, setDynamicProps as _setDynamicProps, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelDynamic, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + _renderEffect(() => _setDynamicProps(n1, _ctx.obj)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should support w/ dynamic v-bind 2`] = ` +"import { children as _children, vModelDynamic as _vModelDynamic, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelDynamic, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with input (checkbox) 1`] = ` +"import { children as _children, vModelCheckbox as _vModelCheckbox, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelCheckbox, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with input (dynamic type) 1`] = ` +"import { children as _children, vModelDynamic as _vModelDynamic, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelDynamic, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with input (radio) 1`] = ` +"import { children as _children, vModelRadio as _vModelRadio, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelRadio, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with input (text) 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with select 1`] = ` +"import { children as _children, vModelSelect as _vModelSelect, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelSelect, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; + +exports[`compiler: vModel transform > should work with simple expression 1`] = ` +"import { children as _children, vModelText as _vModelText, withDirectives as _withDirectives, on as _on, template as _template } from 'vue/vapor'; +const t0 = _template("") + +export function render(_ctx) { + const n0 = t0() + const n1 = _children(n0, 0) + _withDirectives(n1, [[_vModelText, () => _ctx.model]]) + _on(n1, "update:modelValue", $event => (_ctx.model = $event)) + return n0 +}" +`; diff --git a/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts b/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts index 52fe1a038..81f43f795 100644 --- a/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts @@ -1,4 +1,174 @@ -// TODO: add tests for this transform -describe('compiler: vModel transform', () => { - test.todo('basic') +import { makeCompile } from './_utils' +import { transformElement, transformVModel } from '../../src' +import { DOMErrorCodes } from '@vue/compiler-dom' + +const compileWithVModel = makeCompile({ + nodeTransforms: [transformElement], + directiveTransforms: { + model: transformVModel, + }, +}) + +describe('compiler: vModel transform', () => { + test('should support simple expression', () => { + const { code, vaporHelpers } = compileWithVModel( + '', + ) + + expect(vaporHelpers).toContain('vModelText') + expect(code).toMatchSnapshot() + }) + + test('should support input (text)', () => { + const { code, vaporHelpers } = compileWithVModel( + '', + ) + expect(vaporHelpers).toContain('vModelText') + expect(code).toMatchSnapshot() + }) + + test('should support input (radio)', () => { + const { code, vaporHelpers } = compileWithVModel( + '', + ) + expect(vaporHelpers).toContain('vModelRadio') + expect(code).toMatchSnapshot() + }) + + test('should support input (checkbox)', () => { + const { code, vaporHelpers } = compileWithVModel( + '', + ) + expect(vaporHelpers).toContain('vModelCheckbox') + expect(code).toMatchSnapshot() + }) + + test('should support select', () => { + const { code, vaporHelpers } = compileWithVModel( + '