Skip to content

Commit d281d62

Browse files
committed
wip: vdom hydration interop
1 parent 4253b0c commit d281d62

File tree

1 file changed

+123
-13
lines changed

1 file changed

+123
-13
lines changed

packages/runtime-vapor/__tests__/hydration.spec.ts

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
FOR_ANCHOR_LABEL,
1010
IF_ANCHOR_LABEL,
1111
SLOT_ANCHOR_LABEL,
12+
isString,
1213
} from '@vue/shared'
1314

1415
const Vue = { ...runtimeDom, ...runtimeVapor }
@@ -17,7 +18,7 @@ function compile(
1718
sfc: string,
1819
data: runtimeDom.Ref<any>,
1920
components: Record<string, any> = {},
20-
ssr = false,
21+
{ vapor = true, ssr = false } = {},
2122
) {
2223
if (!sfc.includes(`<script`)) {
2324
sfc =
@@ -31,7 +32,7 @@ function compile(
3132
isProd: true,
3233
inlineTemplate: true,
3334
genDefaultAs: '__sfc__',
34-
vapor: true,
35+
vapor,
3536
templateOptions: {
3637
ssr,
3738
},
@@ -55,26 +56,45 @@ function compile(
5556

5657
async function testHydration(
5758
code: string,
58-
components: Record<string, string> = {},
59+
components: Record<string, string | { code: string; vapor: boolean }> = {},
5960
data: any = ref('foo'),
61+
{ interop = false, vapor = true } = {},
6062
) {
6163
const ssrComponents: any = {}
6264
const clientComponents: any = {}
6365
for (const key in components) {
64-
clientComponents[key] = compile(components[key], data, clientComponents)
65-
ssrComponents[key] = compile(components[key], data, ssrComponents, true)
66+
const comp = components[key]
67+
const code = isString(comp) ? comp : comp.code
68+
const isVaporComp = !isString(comp) ? comp.vapor : true
69+
clientComponents[key] = compile(code, data, clientComponents, {
70+
vapor: isVaporComp,
71+
ssr: false,
72+
})
73+
ssrComponents[key] = compile(code, data, ssrComponents, {
74+
vapor: isVaporComp,
75+
ssr: true,
76+
})
6677
}
6778

68-
const serverComp = compile(code, data, ssrComponents, true)
79+
const serverComp = compile(code, data, ssrComponents, { vapor, ssr: true })
6980
const html = await VueServerRenderer.renderToString(
7081
runtimeDom.createSSRApp(serverComp),
7182
)
7283
const container = document.createElement('div')
7384
document.body.appendChild(container)
7485
container.innerHTML = html
7586

76-
const clientComp = compile(code, data, clientComponents)
77-
const app = createVaporSSRApp(clientComp)
87+
const clientComp = compile(code, data, clientComponents, {
88+
vapor,
89+
ssr: false,
90+
})
91+
let app
92+
if (interop) {
93+
app = runtimeDom.createSSRApp(clientComp)
94+
app.use(runtimeVapor.vaporInteropPlugin)
95+
} else {
96+
app = createVaporSSRApp(clientComp)
97+
}
7898
app.mount(container)
7999
return { data, container }
80100
}
@@ -84,13 +104,13 @@ const triggerEvent = (type: string, el: Element) => {
84104
el.dispatchEvent(event)
85105
}
86106

87-
describe('Vapor Mode hydration', () => {
88-
delegateEvents('click')
107+
delegateEvents('click')
89108

90-
beforeEach(() => {
91-
document.body.innerHTML = ''
92-
})
109+
beforeEach(() => {
110+
document.body.innerHTML = ''
111+
})
93112

113+
describe('Vapor Mode hydration', () => {
94114
describe('text', () => {
95115
test('root text', async () => {
96116
const { data, container } = await testHydration(`
@@ -3816,3 +3836,93 @@ describe('Vapor Mode hydration', () => {
38163836
test.todo('Teleport')
38173837
test.todo('Suspense')
38183838
})
3839+
3840+
describe('VDOM hydration interop', () => {
3841+
test('basic component', async () => {
3842+
const data = ref(true)
3843+
const { container } = await testHydration(
3844+
`<script setup>const data = _data; const components = _components;</script>
3845+
<template>
3846+
<components.VaporChild/>
3847+
</template>`,
3848+
{
3849+
VaporChild: {
3850+
code: `<template>{{ data }}</template>`,
3851+
vapor: true,
3852+
},
3853+
},
3854+
data,
3855+
{ interop: true, vapor: false },
3856+
)
3857+
3858+
expect(container.innerHTML).toMatchInlineSnapshot(`"true"`)
3859+
3860+
data.value = false
3861+
await nextTick()
3862+
expect(container.innerHTML).toMatchInlineSnapshot(`"false"`)
3863+
})
3864+
3865+
test('nested components (VDOM -> Vapor -> VDOM)', async () => {
3866+
const data = ref(true)
3867+
const { container } = await testHydration(
3868+
`<script setup>const data = _data; const components = _components;</script>
3869+
<template>
3870+
<components.VaporChild/>
3871+
</template>`,
3872+
{
3873+
VaporChild: {
3874+
code: `<template><components.VdomChild/></template>`,
3875+
vapor: true,
3876+
},
3877+
VdomChild: {
3878+
code: `<script setup>const data = _data;</script>
3879+
<template>{{ data }}</template>`,
3880+
vapor: false,
3881+
},
3882+
},
3883+
data,
3884+
{ interop: true, vapor: false },
3885+
)
3886+
3887+
expect(container.innerHTML).toMatchInlineSnapshot(`"true"`)
3888+
3889+
data.value = false
3890+
await nextTick()
3891+
expect(container.innerHTML).toMatchInlineSnapshot(`"false"`)
3892+
})
3893+
3894+
test.todo('slots', async () => {
3895+
const data = ref(true)
3896+
const { container } = await testHydration(
3897+
`<script setup>const data = _data; const components = _components;</script>
3898+
<template>
3899+
<components.VaporChild>
3900+
<components.VdomChild/>
3901+
</components.VaporChild>
3902+
</template>`,
3903+
{
3904+
VaporChild: {
3905+
code: `<template><div><slot/></div></template>`,
3906+
vapor: true,
3907+
},
3908+
VdomChild: {
3909+
code: `<script setup>const data = _data;</script>
3910+
<template>{{ data }}</template>`,
3911+
vapor: false,
3912+
},
3913+
},
3914+
data,
3915+
{ interop: true, vapor: false },
3916+
)
3917+
3918+
expect(container.innerHTML).toMatchInlineSnapshot(
3919+
`"<div><!--[-->true<!--]--><!--slot--></div>"`,
3920+
)
3921+
3922+
data.value = false
3923+
await nextTick()
3924+
expect(container.innerHTML).toMatchInlineSnapshot(
3925+
`"<div><!--[-->false<!--]--><!--slot--></div>"`,
3926+
)
3927+
})
3928+
})

0 commit comments

Comments
 (0)