@@ -6,10 +6,12 @@ import {
6
6
resolveComponent ,
7
7
ComponentOptions
8
8
} from 'vue'
9
- import { escapeHtml } from '@vue/shared'
9
+ import { escapeHtml , mockWarn } from '@vue/shared'
10
10
import { renderToString , renderComponent } from '../src/renderToString'
11
11
import { ssrRenderSlot } from '../src/helpers/ssrRenderSlot'
12
12
13
+ mockWarn ( )
14
+
13
15
describe ( 'ssr: renderToString' , ( ) => {
14
16
test ( 'should apply app context' , async ( ) => {
15
17
const app = createApp ( {
@@ -56,6 +58,31 @@ describe('ssr: renderToString', () => {
56
58
) . toBe ( `<div>hello</div>` )
57
59
} )
58
60
61
+ describe ( 'template components' , ( ) => {
62
+ test ( 'render' , async ( ) => {
63
+ expect (
64
+ await renderToString (
65
+ createApp ( {
66
+ data ( ) {
67
+ return { msg : 'hello' }
68
+ } ,
69
+ template : `<div>{{ msg }}</div>`
70
+ } )
71
+ )
72
+ ) . toBe ( `<div>hello</div>` )
73
+ } )
74
+
75
+ test ( 'handle compiler errors' , async ( ) => {
76
+ await renderToString ( createApp ( { template : `<` } ) )
77
+
78
+ expect (
79
+ '[Vue warn]: Template compilation error: Unexpected EOF in tag.\n' +
80
+ '1 | <\n' +
81
+ ' | ^'
82
+ ) . toHaveBeenWarned ( )
83
+ } )
84
+ } )
85
+
59
86
test ( 'nested vnode components' , async ( ) => {
60
87
const Child = {
61
88
props : [ 'msg' ] ,
@@ -96,7 +123,22 @@ describe('ssr: renderToString', () => {
96
123
) . toBe ( `<div>parent<div>hello</div></div>` )
97
124
} )
98
125
99
- test ( 'mixing optimized / vnode components' , async ( ) => {
126
+ test ( 'nested template components' , async ( ) => {
127
+ const Child = {
128
+ props : [ 'msg' ] ,
129
+ template : `<div>{{ msg }}</div>`
130
+ }
131
+ const app = createApp ( {
132
+ template : `<div>parent<Child msg="hello" /></div>`
133
+ } )
134
+ app . component ( 'Child' , Child )
135
+
136
+ expect ( await renderToString ( app ) ) . toBe (
137
+ `<div>parent<div>hello</div></div>`
138
+ )
139
+ } )
140
+
141
+ test ( 'mixing optimized / vnode / template components' , async ( ) => {
100
142
const OptimizedChild = {
101
143
props : [ 'msg' ] ,
102
144
ssrRender ( ctx : any , push : any ) {
@@ -111,6 +153,11 @@ describe('ssr: renderToString', () => {
111
153
}
112
154
}
113
155
156
+ const TemplateChild = {
157
+ props : [ 'msg' ] ,
158
+ template : `<div>{{ msg }}</div>`
159
+ }
160
+
114
161
expect (
115
162
await renderToString (
116
163
createApp ( {
@@ -120,11 +167,21 @@ describe('ssr: renderToString', () => {
120
167
renderComponent ( OptimizedChild , { msg : 'opt' } , null , parent )
121
168
)
122
169
push ( renderComponent ( VNodeChild , { msg : 'vnode' } , null , parent ) )
170
+ push (
171
+ renderComponent (
172
+ TemplateChild ,
173
+ { msg : 'template' } ,
174
+ null ,
175
+ parent
176
+ )
177
+ )
123
178
push ( `</div>` )
124
179
}
125
180
} )
126
181
)
127
- ) . toBe ( `<div>parent<div>opt</div><div>vnode</div></div>` )
182
+ ) . toBe (
183
+ `<div>parent<div>opt</div><div>vnode</div><div>template</div></div>`
184
+ )
128
185
} )
129
186
130
187
test ( 'nested components with optimized slots' , async ( ) => {
@@ -236,6 +293,50 @@ describe('ssr: renderToString', () => {
236
293
)
237
294
} )
238
295
296
+ test ( 'nested components with template slots' , async ( ) => {
297
+ const Child = {
298
+ props : [ 'msg' ] ,
299
+ template : `<div class="child"><slot msg="from slot"></slot></div>`
300
+ }
301
+
302
+ const app = createApp ( {
303
+ template : `<div>parent<Child v-slot="{ msg }"><span>{{ msg }}</span></Child></div>`
304
+ } )
305
+ app . component ( 'Child' , Child )
306
+
307
+ expect ( await renderToString ( app ) ) . toBe (
308
+ `<div>parent<div class="child">` +
309
+ `<!----><span>from slot</span><!---->` +
310
+ `</div></div>`
311
+ )
312
+ } )
313
+
314
+ test ( 'nested render fn components with template slots' , async ( ) => {
315
+ const Child = {
316
+ props : [ 'msg' ] ,
317
+ render ( this : any ) {
318
+ return h (
319
+ 'div' ,
320
+ {
321
+ class : 'child'
322
+ } ,
323
+ this . $slots . default ( { msg : 'from slot' } )
324
+ )
325
+ }
326
+ }
327
+
328
+ const app = createApp ( {
329
+ template : `<div>parent<Child v-slot="{ msg }"><span>{{ msg }}</span></Child></div>`
330
+ } )
331
+ app . component ( 'Child' , Child )
332
+
333
+ expect ( await renderToString ( app ) ) . toBe (
334
+ `<div>parent<div class="child">` +
335
+ `<span>from slot</span>` +
336
+ `</div></div>`
337
+ )
338
+ } )
339
+
239
340
test ( 'async components' , async ( ) => {
240
341
const Child = {
241
342
// should wait for resovled render context from setup()
0 commit comments