import {
createApp,
h,
createCommentVNode,
withScopeId,
resolveComponent,
ComponentOptions,
ref,
defineComponent,
createTextVNode,
createStaticVNode
} from 'vue'
import { escapeHtml, mockWarn } from '@vue/shared'
import { renderToStream as _renderToStream } from '../src/renderToStream'
import { Readable } from 'stream'
import { ssrRenderSlot } from '../src/helpers/ssrRenderSlot'
import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'
mockWarn()
const promisifyStream = (stream: Readable) => {
return new Promise((resolve, reject) => {
let result = ''
stream.on('data', data => {
result += data
})
stream.on('error', () => {
reject(result)
})
stream.on('end', () => {
resolve(result)
})
})
}
const renderToStream = (app: any, context?: any) =>
promisifyStream(_renderToStream(app, context))
describe('ssr: renderToStream', () => {
test('should apply app context', async () => {
const app = createApp({
render() {
const Foo = resolveComponent('foo') as ComponentOptions
return h(Foo)
}
})
app.component('foo', {
render: () => h('div', 'foo')
})
const html = await renderToStream(app)
expect(html).toBe(`
foo
`)
})
describe('components', () => {
test('vnode components', async () => {
expect(
await renderToStream(
createApp({
data() {
return { msg: 'hello' }
},
render(this: any) {
return h('div', this.msg)
}
})
)
).toBe(`hello
`)
})
test('option components returning render from setup', async () => {
expect(
await renderToStream(
createApp({
setup() {
const msg = ref('hello')
return () => h('div', msg.value)
}
})
)
).toBe(`hello
`)
})
test('setup components returning render from setup', async () => {
expect(
await renderToStream(
createApp(
defineComponent(() => {
const msg = ref('hello')
return () => h('div', msg.value)
})
)
)
).toBe(`hello
`)
})
test('optimized components', async () => {
expect(
await renderToStream(
createApp({
data() {
return { msg: 'hello' }
},
ssrRender(ctx, push) {
push(`${ctx.msg}
`)
}
})
)
).toBe(`hello
`)
})
describe('template components', () => {
test('render', async () => {
expect(
await renderToStream(
createApp({
data() {
return { msg: 'hello' }
},
template: `{{ msg }}
`
})
)
).toBe(`hello
`)
})
test('handle compiler errors', async () => {
await renderToStream(createApp({ template: `<` }))
expect(
'Template compilation error: Unexpected EOF in tag.\n' +
'1 | <\n' +
' | ^'
).toHaveBeenWarned()
})
})
test('nested vnode components', async () => {
const Child = {
props: ['msg'],
render(this: any) {
return h('div', this.msg)
}
}
expect(
await renderToStream(
createApp({
render() {
return h('div', ['parent', h(Child, { msg: 'hello' })])
}
})
)
).toBe(``)
})
test('nested optimized components', async () => {
const Child = {
props: ['msg'],
ssrRender(ctx: any, push: any) {
push(`${ctx.msg}
`)
}
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(ssrRenderComponent(Child, { msg: 'hello' }, null, parent))
push(`
`)
}
})
)
).toBe(``)
})
test('nested template components', async () => {
const Child = {
props: ['msg'],
template: `{{ msg }}
`
}
const app = createApp({
template: `parent
`
})
app.component('Child', Child)
expect(await renderToStream(app)).toBe(
``
)
})
test('mixing optimized / vnode / template components', async () => {
const OptimizedChild = {
props: ['msg'],
ssrRender(ctx: any, push: any) {
push(`${ctx.msg}
`)
}
}
const VNodeChild = {
props: ['msg'],
render(this: any) {
return h('div', this.msg)
}
}
const TemplateChild = {
props: ['msg'],
template: `{{ msg }}
`
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(
ssrRenderComponent(OptimizedChild, { msg: 'opt' }, null, parent)
)
push(
ssrRenderComponent(VNodeChild, { msg: 'vnode' }, null, parent)
)
push(
ssrRenderComponent(
TemplateChild,
{ msg: 'template' },
null,
parent
)
)
push(`
`)
}
})
)
).toBe(
``
)
})
test('nested components with optimized slots', async () => {
const Child = {
props: ['msg'],
ssrRender(ctx: any, push: any, parent: any) {
push(``)
ssrRenderSlot(
ctx.$slots,
'default',
{ msg: 'from slot' },
() => {
push(`fallback`)
},
push,
parent
)
push(`
`)
}
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(
ssrRenderComponent(
Child,
{ msg: 'hello' },
{
// optimized slot using string push
default: ({ msg }: any, push: any) => {
push(`${msg}`)
},
// important to avoid slots being normalized
_: 1 as any
},
parent
)
)
push(`
`)
}
})
)
).toBe(
`parent
` +
`from slot` +
`
`
)
// test fallback
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(ssrRenderComponent(Child, { msg: 'hello' }, null, parent))
push(`
`)
}
})
)
).toBe(
``
)
})
test('nested components with vnode slots', async () => {
const Child = {
props: ['msg'],
ssrRender(ctx: any, push: any, parent: any) {
push(``)
ssrRenderSlot(
ctx.$slots,
'default',
{ msg: 'from slot' },
null,
push,
parent
)
push(`
`)
}
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(
ssrRenderComponent(
Child,
{ msg: 'hello' },
{
// bailed slots returning raw vnodes
default: ({ msg }: any) => {
return h('span', msg)
}
},
parent
)
)
push(`
`)
}
})
)
).toBe(
`parent
` +
`from slot` +
`
`
)
})
test('nested components with template slots', async () => {
const Child = {
props: ['msg'],
template: `
`
}
const app = createApp({
components: { Child },
template: `parent{{ msg }}
`
})
expect(await renderToStream(app)).toBe(
`parent
` +
`from slot` +
`
`
)
})
test('nested render fn components with template slots', async () => {
const Child = {
props: ['msg'],
render(this: any) {
return h(
'div',
{
class: 'child'
},
this.$slots.default({ msg: 'from slot' })
)
}
}
const app = createApp({
template: `parent{{ msg }}
`
})
app.component('Child', Child)
expect(await renderToStream(app)).toBe(
`parent
` +
// no comment anchors because slot is used directly as element children
`from slot` +
`
`
)
})
test('async components', async () => {
const Child = {
// should wait for resolved render context from setup()
async setup() {
return {
msg: 'hello'
}
},
ssrRender(ctx: any, push: any) {
push(`${ctx.msg}
`)
}
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(ssrRenderComponent(Child, null, null, parent))
push(`
`)
}
})
)
).toBe(``)
})
test('parallel async components', async () => {
const OptimizedChild = {
props: ['msg'],
async setup(props: any) {
return {
localMsg: props.msg + '!'
}
},
ssrRender(ctx: any, push: any) {
push(`${ctx.localMsg}
`)
}
}
const VNodeChild = {
props: ['msg'],
async setup(props: any) {
return {
localMsg: props.msg + '!'
}
},
render(this: any) {
return h('div', this.localMsg)
}
}
expect(
await renderToStream(
createApp({
ssrRender(_ctx, push, parent) {
push(`parent`)
push(
ssrRenderComponent(OptimizedChild, { msg: 'opt' }, null, parent)
)
push(
ssrRenderComponent(VNodeChild, { msg: 'vnode' }, null, parent)
)
push(`
`)
}
})
)
).toBe(``)
})
})
describe('vnode element', () => {
test('props', async () => {
expect(
await renderToStream(
h('div', { id: 'foo&', class: ['bar', 'baz'] }, 'hello')
)
).toBe(`hello
`)
})
test('text children', async () => {
expect(await renderToStream(h('div', 'hello'))).toBe(`hello
`)
})
test('array children', async () => {
expect(
await renderToStream(
h('div', [
'foo',
h('span', 'bar'),
[h('span', 'baz')],
createCommentVNode('qux')
])
)
).toBe(
`foobarbaz
`
)
})
test('void elements', async () => {
expect(await renderToStream(h('input'))).toBe(``)
})
test('innerHTML', async () => {
expect(
await renderToStream(
h(
'div',
{
innerHTML: `hello`
},
'ignored'
)
)
).toBe(`hello
`)
})
test('textContent', async () => {
expect(
await renderToStream(
h(
'div',
{
textContent: `hello`
},
'ignored'
)
)
).toBe(`${escapeHtml(`hello`)}
`)
})
test('textarea value', async () => {
expect(
await renderToStream(
h(
'textarea',
{
value: `hello`
},
'ignored'
)
)
).toBe(``)
})
})
describe('raw vnode types', () => {
test('Text', async () => {
expect(await renderToStream(createTextVNode('hello '))).toBe(
`hello <div>`
)
})
test('Comment', async () => {
// https://www.w3.org/TR/html52/syntax.html#comments
expect(
await renderToStream(
h('div', [
createCommentVNode('>foo'),
createCommentVNode('->foo'),
createCommentVNode(''),
createCommentVNode('--!>foo
`)
})
test('Static', async () => {
const content = `helloworld
`
expect(await renderToStream(createStaticVNode(content, 1))).toBe(content)
})
})
describe('scopeId', () => {
// note: here we are only testing scopeId handling for vdom serialization.
// compiled srr render functions will include scopeId directly in strings.
const withId = withScopeId('data-v-test')
const withChildId = withScopeId('data-v-child')
test('basic', async () => {
expect(
await renderToStream(
withId(() => {
return h('div')
})()
)
).toBe(``)
})
test('with slots', async () => {
const Child = {
__scopeId: 'data-v-child',
render: withChildId(function(this: any) {
return h('div', this.$slots.default())
})
}
const Parent = {
__scopeId: 'data-v-test',
render: withId(() => {
return h(Child, null, {
default: withId(() => h('span', 'slot'))
})
})
}
expect(await renderToStream(h(Parent))).toBe(
`slot
`
)
})
})
})