Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68ded05

Browse files
author
Guillaume Chau
committed
test(ssr): context.onRenderComplete
1 parent fa286b0 commit 68ded05

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

‎test/ssr/ssr-stream.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,26 @@ describe('SSR: renderToStream', () => {
102102
stream1.read(1)
103103
stream2.read(1)
104104
})
105+
106+
it('should call context.onRenderComplete', done => {
107+
let a = 0
108+
const stream = renderToStream(new Vue({
109+
template: `
110+
<div>Hello</div>
111+
`
112+
}), {
113+
onRenderComplete: () => {
114+
a = 42
115+
}
116+
})
117+
let res = ''
118+
stream.on('data', chunk => {
119+
res += chunk
120+
})
121+
stream.on('end', () => {
122+
expect(res).toContain('<div data-server-rendered="true">Hello</div>')
123+
expect(a).toBe(42)
124+
done()
125+
})
126+
})
105127
})

‎test/ssr/ssr-string.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,22 @@ describe('SSR: renderToString', () => {
14491449
done()
14501450
})
14511451
})
1452+
1453+
it('should call context.onRenderComplete', done => {
1454+
let a = 0
1455+
renderToString(new Vue({
1456+
template: '<div>Hello</div>'
1457+
}), {
1458+
onRenderComplete: () => {
1459+
a = 42
1460+
}
1461+
}, (err, res) => {
1462+
expect(err).toBeNull()
1463+
expect(res).toContain('<div data-server-rendered="true">Hello</div>')
1464+
expect(a).toBe(42)
1465+
done()
1466+
})
1467+
})
14521468
})
14531469

14541470
function renderVmWithOptions (options, cb) {

‎test/ssr/ssr-template.spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,41 @@ describe('SSR: template option', () => {
9999
})
100100
})
101101

102+
it('renderToString with interpolation and context.onRenderComplete', done => {
103+
const renderer = createRenderer({
104+
template: interpolateTemplate
105+
})
106+
107+
const context = {
108+
title: '<script>hacks</script>',
109+
snippet: '<div>foo</div>',
110+
head: '<meta name="viewport" content="width=device-width">',
111+
styles: '<style>h1 { color: red }</style>',
112+
state: { a: 0 },
113+
onRenderComplete: context => {
114+
context.state.a = 1
115+
}
116+
}
117+
118+
renderer.renderToString(new Vue({
119+
template: '<div>hi</div>'
120+
}), context, (err, res) => {
121+
expect(err).toBeNull()
122+
expect(res).toContain(
123+
`<html><head>` +
124+
// double mustache should be escaped
125+
`<title>&lt;script&gt;hacks&lt;/script&gt;</title>` +
126+
`${context.head}${context.styles}</head><body>` +
127+
`<div data-server-rendered="true">hi</div>` +
128+
`<script>window.__INITIAL_STATE__={"a":1}</script>` +
129+
// triple should be raw
130+
`<div>foo</div>` +
131+
`</body></html>`
132+
)
133+
done()
134+
})
135+
})
136+
102137
it('renderToStream', done => {
103138
const renderer = createRenderer({
104139
template: defaultTemplate
@@ -166,6 +201,46 @@ describe('SSR: template option', () => {
166201
})
167202
})
168203

204+
it('renderToStream with interpolation and context.onRenderComplete', done => {
205+
const renderer = createRenderer({
206+
template: interpolateTemplate
207+
})
208+
209+
const context = {
210+
title: '<script>hacks</script>',
211+
snippet: '<div>foo</div>',
212+
head: '<meta name="viewport" content="width=device-width">',
213+
styles: '<style>h1 { color: red }</style>',
214+
state: { a: 0 },
215+
onRenderComplete: context => {
216+
context.state.a = 1
217+
}
218+
}
219+
220+
const stream = renderer.renderToStream(new Vue({
221+
template: '<div>hi</div>'
222+
}), context)
223+
224+
let res = ''
225+
stream.on('data', chunk => {
226+
res += chunk
227+
})
228+
stream.on('end', () => {
229+
expect(res).toContain(
230+
`<html><head>` +
231+
// double mustache should be escaped
232+
`<title>&lt;script&gt;hacks&lt;/script&gt;</title>` +
233+
`${context.head}${context.styles}</head><body>` +
234+
`<div data-server-rendered="true">hi</div>` +
235+
`<script>window.__INITIAL_STATE__={"a":1}</script>` +
236+
// triple should be raw
237+
`<div>foo</div>` +
238+
`</body></html>`
239+
)
240+
done()
241+
})
242+
})
243+
169244
it('bundleRenderer + renderToString', done => {
170245
createBundleRenderer('app.js', {
171246
asBundle: true,

0 commit comments

Comments
 (0)