2021-07-16 22:40:06 +08:00
|
|
|
/**
|
|
|
|
* @jest-environment node
|
|
|
|
*/
|
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
import { createApp, h, Teleport } from 'vue'
|
2020-06-26 23:09:47 +08:00
|
|
|
import { renderToString } from '../src/renderToString'
|
|
|
|
import { SSRContext } from '../src/render'
|
2020-03-31 22:52:42 +08:00
|
|
|
import { ssrRenderTeleport } from '../src/helpers/ssrRenderTeleport'
|
2020-02-27 03:59:53 +08:00
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
describe('ssrRenderTeleport', () => {
|
|
|
|
test('teleport rendering (compiled)', async () => {
|
2020-03-28 08:49:01 +08:00
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
2020-02-27 03:59:53 +08:00
|
|
|
createApp({
|
|
|
|
data() {
|
|
|
|
return { msg: 'hello' }
|
|
|
|
},
|
|
|
|
ssrRender(_ctx, _push, _parent) {
|
2020-03-31 22:52:42 +08:00
|
|
|
ssrRenderTeleport(
|
2020-03-28 08:49:01 +08:00
|
|
|
_push,
|
2020-02-27 03:59:53 +08:00
|
|
|
_push => {
|
|
|
|
_push(`<div>content</div>`)
|
|
|
|
},
|
|
|
|
'#target',
|
2020-03-28 11:45:50 +08:00
|
|
|
false,
|
2020-02-27 03:59:53 +08:00
|
|
|
_parent
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-31 22:52:42 +08:00
|
|
|
expect(html).toBe('<!--teleport start--><!--teleport end-->')
|
|
|
|
expect(ctx.teleports!['#target']).toBe(`<div>content</div><!---->`)
|
2020-02-27 03:59:53 +08:00
|
|
|
})
|
2020-03-11 03:23:14 +08:00
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
test('teleport rendering (compiled + disabled)', async () => {
|
2020-03-28 11:45:50 +08:00
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
createApp({
|
|
|
|
data() {
|
|
|
|
return { msg: 'hello' }
|
|
|
|
},
|
|
|
|
ssrRender(_ctx, _push, _parent) {
|
2020-03-31 22:52:42 +08:00
|
|
|
ssrRenderTeleport(
|
2020-03-28 11:45:50 +08:00
|
|
|
_push,
|
|
|
|
_push => {
|
|
|
|
_push(`<div>content</div>`)
|
|
|
|
},
|
|
|
|
'#target',
|
|
|
|
true,
|
|
|
|
_parent
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-31 22:52:42 +08:00
|
|
|
expect(html).toBe(
|
|
|
|
'<!--teleport start--><div>content</div><!--teleport end-->'
|
|
|
|
)
|
|
|
|
expect(ctx.teleports!['#target']).toBe(`<!---->`)
|
2020-03-28 11:45:50 +08:00
|
|
|
})
|
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
test('teleport rendering (vnode)', async () => {
|
2020-03-11 03:23:14 +08:00
|
|
|
const ctx: SSRContext = {}
|
2020-03-28 08:49:01 +08:00
|
|
|
const html = await renderToString(
|
2020-03-11 03:23:14 +08:00
|
|
|
h(
|
2020-03-31 22:52:42 +08:00
|
|
|
Teleport,
|
2020-03-11 03:23:14 +08:00
|
|
|
{
|
2020-03-31 22:52:42 +08:00
|
|
|
to: `#target`
|
2020-03-11 03:23:14 +08:00
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-31 22:52:42 +08:00
|
|
|
expect(html).toBe('<!--teleport start--><!--teleport end-->')
|
|
|
|
expect(ctx.teleports!['#target']).toBe('<span>hello</span><!---->')
|
2020-03-28 08:49:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
test('teleport rendering (vnode + disabled)', async () => {
|
2020-03-28 11:45:50 +08:00
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
h(
|
2020-03-31 22:52:42 +08:00
|
|
|
Teleport,
|
2020-03-28 11:45:50 +08:00
|
|
|
{
|
2020-03-31 22:52:42 +08:00
|
|
|
to: `#target`,
|
2020-03-28 11:45:50 +08:00
|
|
|
disabled: true
|
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-31 22:52:42 +08:00
|
|
|
expect(html).toBe(
|
|
|
|
'<!--teleport start--><span>hello</span><!--teleport end-->'
|
|
|
|
)
|
|
|
|
expect(ctx.teleports!['#target']).toBe(`<!---->`)
|
2020-03-28 11:45:50 +08:00
|
|
|
})
|
|
|
|
|
2020-03-31 22:52:42 +08:00
|
|
|
test('multiple teleports with same target', async () => {
|
2020-03-28 08:49:01 +08:00
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
h('div', [
|
|
|
|
h(
|
2020-03-31 22:52:42 +08:00
|
|
|
Teleport,
|
2020-03-28 08:49:01 +08:00
|
|
|
{
|
2020-03-31 22:52:42 +08:00
|
|
|
to: `#target`
|
2020-03-28 08:49:01 +08:00
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
2020-03-31 22:52:42 +08:00
|
|
|
h(Teleport, { to: `#target` }, 'world')
|
2020-03-28 08:49:01 +08:00
|
|
|
]),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-28 11:45:50 +08:00
|
|
|
expect(html).toBe(
|
2020-03-31 22:52:42 +08:00
|
|
|
'<div><!--teleport start--><!--teleport end--><!--teleport start--><!--teleport end--></div>'
|
2020-03-28 11:45:50 +08:00
|
|
|
)
|
2020-03-31 22:52:42 +08:00
|
|
|
expect(ctx.teleports!['#target']).toBe(
|
2020-03-28 08:49:01 +08:00
|
|
|
'<span>hello</span><!---->world<!---->'
|
|
|
|
)
|
2020-03-11 03:23:14 +08:00
|
|
|
})
|
2022-05-13 15:57:10 +08:00
|
|
|
|
|
|
|
test('teleport inside async component', async () => {
|
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const asyncComponent = {
|
|
|
|
template: '<teleport to="#target"><div>content</div></teleport>',
|
|
|
|
async setup() {}
|
|
|
|
}
|
|
|
|
const html = await renderToString(
|
|
|
|
h({
|
|
|
|
template: '<async-component />',
|
|
|
|
components: { asyncComponent }
|
|
|
|
}),
|
|
|
|
ctx
|
|
|
|
)
|
|
|
|
expect(html).toBe('<!--teleport start--><!--teleport end-->')
|
|
|
|
expect(ctx.teleports!['#target']).toBe(`<div>content</div><!---->`)
|
|
|
|
})
|
2020-02-27 03:59:53 +08:00
|
|
|
})
|