vue2/packages/server-renderer/test/ssr-stream.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-05-20 16:55:05 +08:00
// @vitest-environment node
2022-05-20 12:01:16 +08:00
import Vue from 'vue'
import { createRenderer } from 'server/index'
const { renderToStream } = createRenderer()
2016-04-26 07:10:33 +08:00
describe('SSR: renderToStream', () => {
it('should render to a stream', done => {
const stream = renderToStream(
new Vue({
2016-04-26 07:10:33 +08:00
template: `
<div>
<p class="hi">yoyo</p>
<div id="ho" :class="[testClass, { red: isRed }]"></div>
2016-04-26 07:10:33 +08:00
<span>{{ test }}</span>
<input :value="test">
<b-comp></b-comp>
<c-comp></c-comp>
2016-04-26 07:10:33 +08:00
</div>
`,
data: {
test: 'hi',
isRed: true,
testClass: 'a'
2016-04-26 07:10:33 +08:00
},
components: {
bComp(resolve) {
return resolve({
2016-08-06 00:43:35 +08:00
render(h) {
return h('test-async-2')
},
components: {
testAsync2(resolve) {
return resolve({
created() {
this.$parent.$parent.testClass = 'b'
},
2016-08-06 00:43:35 +08:00
render(h) {
return h(
'div',
{ class: [this.$parent.$parent.testClass] },
'test'
)
}
})
}
}
})
},
cComp: {
2016-08-06 00:43:35 +08:00
render(h) {
return h('div', { class: [this.$parent.testClass] }, 'test')
}
2016-04-26 07:10:33 +08:00
}
}
})
)
2016-04-26 07:10:33 +08:00
let res = ''
stream.on('data', chunk => {
res += chunk
})
stream.on('end', () => {
expect(res).toContain(
2017-03-29 17:53:46 +08:00
'<div data-server-rendered="true">' +
'<p class="hi">yoyo</p> ' +
'<div id="ho" class="a red"></div> ' +
'<span>hi</span> ' +
'<input value="hi"> ' +
'<div class="b">test</div> ' +
'<div class="b">test</div>' +
'</div>'
)
2016-04-26 07:10:33 +08:00
done()
})
})
2016-05-12 14:49:11 +08:00
it('should catch error', done => {
const stream = renderToStream(
new Vue({
render() {
throw new Error('oops')
}
})
)
2016-05-12 14:49:11 +08:00
stream.on('error', err => {
expect(err.toString()).toMatch(/oops/)
2022-05-20 12:01:16 +08:00
expect(`oops`).toHaveBeenWarned()
2016-05-12 14:49:11 +08:00
done()
})
stream.on('data', _ => _)
})
it('should not mingle two components', done => {
const padding = new Array(20000).join('x')
const component1 = new Vue({
template: `<div>${padding}<div></div></div>`,
_scopeId: '_component1'
})
const component2 = new Vue({
template: `<div></div>`,
_scopeId: '_component2'
})
const stream1 = renderToStream(component1)
const stream2 = renderToStream(component2)
let res = ''
stream1.on('data', text => {
res += text.toString('utf-8').replace(/x/g, '')
})
stream1.on('end', () => {
expect(res).not.toContain('_component2')
done()
})
stream1.read(1)
stream2.read(1)
})
it('should call context.rendered', done => {
let a = 0
const stream = renderToStream(
new Vue({
template: `
<div>Hello</div>
`
}),
{
rendered: () => {
a = 42
}
}
)
let res = ''
stream.on('data', chunk => {
res += chunk
})
stream.on('end', () => {
expect(res).toContain('<div data-server-rendered="true">Hello</div>')
expect(a).toBe(42)
done()
})
})
2016-04-26 07:10:33 +08:00
})