Skip to content

Commit 6e0cfd1

Browse files
committed
test(scopeId): add tests for slot wrapping with withVaporCtx
1 parent e7e80ee commit 6e0cfd1

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`scopeId compiler support > should wrap default slot 1`] = `
4+
"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
5+
const t0 = _template("<div></div>")
6+
7+
export function render(_ctx) {
8+
const _component_Child = _resolveComponent("Child")
9+
const n1 = _createComponentWithFallback(_component_Child, null, {
10+
"default": _withVaporCtx(() => {
11+
const n0 = t0()
12+
return n0
13+
})
14+
}, true)
15+
return n1
16+
}"
17+
`;
18+
19+
exports[`scopeId compiler support > should wrap dynamic slots 1`] = `
20+
"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createForSlots as _createForSlots, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
21+
const t0 = _template("<div test></div>")
22+
23+
export function render(_ctx) {
24+
const _component_Child = _resolveComponent("Child")
25+
const n4 = _createComponentWithFallback(_component_Child, null, {
26+
$: [
27+
() => (_ctx.ok
28+
? {
29+
name: "foo",
30+
fn: _withVaporCtx(() => {
31+
const n0 = t0()
32+
return n0
33+
})
34+
}
35+
: void 0),
36+
() => (_createForSlots(_ctx.list, (i) => ({
37+
name: i,
38+
fn: _withVaporCtx(() => {
39+
const n2 = t0()
40+
return n2
41+
})
42+
})))
43+
]
44+
}, true)
45+
return n4
46+
}"
47+
`;
48+
49+
exports[`scopeId compiler support > should wrap named slots 1`] = `
50+
"import { resolveComponent as _resolveComponent, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
51+
const t0 = _template(" ")
52+
const t1 = _template("<div test></div>")
53+
54+
export function render(_ctx) {
55+
const _component_Child = _resolveComponent("Child")
56+
const n4 = _createComponentWithFallback(_component_Child, null, {
57+
"foo": _withVaporCtx((_slotProps0) => {
58+
const n0 = t0()
59+
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["msg"])))
60+
return n0
61+
}),
62+
"bar": _withVaporCtx(() => {
63+
const n2 = t1()
64+
return n2
65+
})
66+
}, true)
67+
return n4
68+
}"
69+
`;
Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
1-
// import { compile } from '../src/compile'
1+
import type { RootNode } from '@vue/compiler-dom'
2+
import { type CompilerOptions, compile as _compile } from '../src'
23

3-
describe.todo('scopeId compiler support', () => {})
4+
function compile(template: string | RootNode, options: CompilerOptions = {}) {
5+
let { code } = _compile(template, {
6+
...options,
7+
mode: 'module',
8+
prefixIdentifiers: true,
9+
})
10+
return code
11+
}
12+
13+
/**
14+
* Ensure all slot functions are wrapped with `withVaporCtx`
15+
* which sets the `currentInstance` to owner when rendering
16+
* the slot.
17+
*/
18+
describe('scopeId compiler support', () => {
19+
test('should wrap default slot', () => {
20+
const code = compile(`<Child><div/></Child>`)
21+
expect(code).toMatch(`"default": _withVaporCtx(() => {`)
22+
expect(code).toMatchSnapshot()
23+
})
24+
25+
test('should wrap named slots', () => {
26+
const code = compile(
27+
`<Child>
28+
<template #foo="{ msg }">{{ msg }}</template>
29+
<template #bar><div/></template>
30+
</Child>
31+
`,
32+
{
33+
mode: 'module',
34+
scopeId: 'test',
35+
},
36+
)
37+
expect(code).toMatch(`"foo": _withVaporCtx((_slotProps0) => {`)
38+
expect(code).toMatch(`"bar": _withVaporCtx(() => {`)
39+
expect(code).toMatchSnapshot()
40+
})
41+
42+
test('should wrap dynamic slots', () => {
43+
const code = compile(
44+
`<Child>
45+
<template #foo v-if="ok"><div/></template>
46+
<template v-for="i in list" #[i]><div/></template>
47+
</Child>
48+
`,
49+
{
50+
mode: 'module',
51+
scopeId: 'test',
52+
},
53+
)
54+
expect(code).toMatch(/name: "foo",\s+fn: _withVaporCtx\(/)
55+
expect(code).toMatch(/name: i,\s+fn: _withVaporCtx\(/)
56+
expect(code).toMatchSnapshot()
57+
})
58+
})

0 commit comments

Comments
 (0)