2020-03-11 03:23:14 +08:00
|
|
|
import { createApp, h, Portal } from 'vue'
|
2020-02-27 03:59:53 +08:00
|
|
|
import { renderToString, SSRContext } from '../src/renderToString'
|
|
|
|
import { ssrRenderPortal } from '../src/helpers/ssrRenderPortal'
|
|
|
|
|
|
|
|
describe('ssrRenderPortal', () => {
|
2020-03-11 03:23:14 +08:00
|
|
|
test('portal 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) {
|
|
|
|
ssrRenderPortal(
|
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-28 11:45:50 +08:00
|
|
|
expect(html).toBe('<!--portal start--><!--portal end-->')
|
2020-03-28 08:49:01 +08:00
|
|
|
expect(ctx.portals!['#target']).toBe(`<div>content</div><!---->`)
|
2020-02-27 03:59:53 +08:00
|
|
|
})
|
2020-03-11 03:23:14 +08:00
|
|
|
|
2020-03-28 11:45:50 +08:00
|
|
|
test('portal rendering (compiled + disabled)', async () => {
|
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
createApp({
|
|
|
|
data() {
|
|
|
|
return { msg: 'hello' }
|
|
|
|
},
|
|
|
|
ssrRender(_ctx, _push, _parent) {
|
|
|
|
ssrRenderPortal(
|
|
|
|
_push,
|
|
|
|
_push => {
|
|
|
|
_push(`<div>content</div>`)
|
|
|
|
},
|
|
|
|
'#target',
|
|
|
|
true,
|
|
|
|
_parent
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
ctx
|
|
|
|
)
|
|
|
|
expect(html).toBe('<!--portal start--><div>content</div><!--portal end-->')
|
|
|
|
expect(ctx.portals!['#target']).toBe(`<!---->`)
|
|
|
|
})
|
|
|
|
|
2020-03-11 03:23:14 +08:00
|
|
|
test('portal rendering (vnode)', async () => {
|
|
|
|
const ctx: SSRContext = {}
|
2020-03-28 08:49:01 +08:00
|
|
|
const html = await renderToString(
|
2020-03-11 03:23:14 +08:00
|
|
|
h(
|
|
|
|
Portal,
|
|
|
|
{
|
|
|
|
target: `#target`
|
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-28 11:45:50 +08:00
|
|
|
expect(html).toBe('<!--portal start--><!--portal end-->')
|
2020-03-28 08:49:01 +08:00
|
|
|
expect(ctx.portals!['#target']).toBe('<span>hello</span><!---->')
|
|
|
|
})
|
|
|
|
|
2020-03-28 11:45:50 +08:00
|
|
|
test('portal rendering (vnode + disabled)', async () => {
|
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
h(
|
|
|
|
Portal,
|
|
|
|
{
|
|
|
|
target: `#target`,
|
|
|
|
disabled: true
|
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
|
|
|
ctx
|
|
|
|
)
|
|
|
|
expect(html).toBe('<!--portal start--><span>hello</span><!--portal end-->')
|
|
|
|
expect(ctx.portals!['#target']).toBe(`<!---->`)
|
|
|
|
})
|
|
|
|
|
2020-03-28 08:49:01 +08:00
|
|
|
test('multiple portals with same target', async () => {
|
|
|
|
const ctx: SSRContext = {}
|
|
|
|
const html = await renderToString(
|
|
|
|
h('div', [
|
|
|
|
h(
|
|
|
|
Portal,
|
|
|
|
{
|
|
|
|
target: `#target`
|
|
|
|
},
|
|
|
|
h('span', 'hello')
|
|
|
|
),
|
|
|
|
h(Portal, { target: `#target` }, 'world')
|
|
|
|
]),
|
|
|
|
ctx
|
|
|
|
)
|
2020-03-28 11:45:50 +08:00
|
|
|
expect(html).toBe(
|
|
|
|
'<div><!--portal start--><!--portal end--><!--portal start--><!--portal end--></div>'
|
|
|
|
)
|
2020-03-28 08:49:01 +08:00
|
|
|
expect(ctx.portals!['#target']).toBe(
|
|
|
|
'<span>hello</span><!---->world<!---->'
|
|
|
|
)
|
2020-03-11 03:23:14 +08:00
|
|
|
})
|
2020-02-27 03:59:53 +08:00
|
|
|
})
|