From 6e0cfd19d79b88a60ec6efaf032aa02613036824 Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 26 Nov 2025 14:58:33 +0800 Subject: [PATCH] test(scopeId): add tests for slot wrapping with `withVaporCtx` --- .../__snapshots__/scopeId.spec.ts.snap | 69 +++++++++++++++++++ .../compiler-vapor/__tests__/scopeId.spec.ts | 59 +++++++++++++++- 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 packages/compiler-vapor/__tests__/__snapshots__/scopeId.spec.ts.snap diff --git a/packages/compiler-vapor/__tests__/__snapshots__/scopeId.spec.ts.snap b/packages/compiler-vapor/__tests__/__snapshots__/scopeId.spec.ts.snap new file mode 100644 index 00000000000..3d0322ce519 --- /dev/null +++ b/packages/compiler-vapor/__tests__/__snapshots__/scopeId.spec.ts.snap @@ -0,0 +1,69 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`scopeId compiler support > should wrap default slot 1`] = ` +"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue'; +const t0 = _template("
") + +export function render(_ctx) { + const _component_Child = _resolveComponent("Child") + const n1 = _createComponentWithFallback(_component_Child, null, { + "default": _withVaporCtx(() => { + const n0 = t0() + return n0 + }) + }, true) + return n1 +}" +`; + +exports[`scopeId compiler support > should wrap dynamic slots 1`] = ` +"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createForSlots as _createForSlots, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue'; +const t0 = _template("
") + +export function render(_ctx) { + const _component_Child = _resolveComponent("Child") + const n4 = _createComponentWithFallback(_component_Child, null, { + $: [ + () => (_ctx.ok + ? { + name: "foo", + fn: _withVaporCtx(() => { + const n0 = t0() + return n0 + }) + } + : void 0), + () => (_createForSlots(_ctx.list, (i) => ({ + name: i, + fn: _withVaporCtx(() => { + const n2 = t0() + return n2 + }) + }))) + ] + }, true) + return n4 +}" +`; + +exports[`scopeId compiler support > should wrap named slots 1`] = ` +"import { resolveComponent as _resolveComponent, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue'; +const t0 = _template(" ") +const t1 = _template("
") + +export function render(_ctx) { + const _component_Child = _resolveComponent("Child") + const n4 = _createComponentWithFallback(_component_Child, null, { + "foo": _withVaporCtx((_slotProps0) => { + const n0 = t0() + _renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["msg"]))) + return n0 + }), + "bar": _withVaporCtx(() => { + const n2 = t1() + return n2 + }) + }, true) + return n4 +}" +`; diff --git a/packages/compiler-vapor/__tests__/scopeId.spec.ts b/packages/compiler-vapor/__tests__/scopeId.spec.ts index da17ef5552f..8dfcd0c0d3d 100644 --- a/packages/compiler-vapor/__tests__/scopeId.spec.ts +++ b/packages/compiler-vapor/__tests__/scopeId.spec.ts @@ -1,3 +1,58 @@ -// import { compile } from '../src/compile' +import type { RootNode } from '@vue/compiler-dom' +import { type CompilerOptions, compile as _compile } from '../src' -describe.todo('scopeId compiler support', () => {}) +function compile(template: string | RootNode, options: CompilerOptions = {}) { + let { code } = _compile(template, { + ...options, + mode: 'module', + prefixIdentifiers: true, + }) + return code +} + +/** + * Ensure all slot functions are wrapped with `withVaporCtx` + * which sets the `currentInstance` to owner when rendering + * the slot. + */ +describe('scopeId compiler support', () => { + test('should wrap default slot', () => { + const code = compile(`
`) + expect(code).toMatch(`"default": _withVaporCtx(() => {`) + expect(code).toMatchSnapshot() + }) + + test('should wrap named slots', () => { + const code = compile( + ` + + + + `, + { + mode: 'module', + scopeId: 'test', + }, + ) + expect(code).toMatch(`"foo": _withVaporCtx((_slotProps0) => {`) + expect(code).toMatch(`"bar": _withVaporCtx(() => {`) + expect(code).toMatchSnapshot() + }) + + test('should wrap dynamic slots', () => { + const code = compile( + ` + + + + `, + { + mode: 'module', + scopeId: 'test', + }, + ) + expect(code).toMatch(/name: "foo",\s+fn: _withVaporCtx\(/) + expect(code).toMatch(/name: i,\s+fn: _withVaporCtx\(/) + expect(code).toMatchSnapshot() + }) +})