@@ -273,6 +273,84 @@ test('reply.view for mustache engine with data-parameter and reply.locals and de
273
273
await fastify . close ( )
274
274
} )
275
275
276
+ test ( 'reply.view with mustache engine with global partials' , async t => {
277
+ t . plan ( 4 )
278
+ const fastify = Fastify ( )
279
+ const mustache = require ( 'mustache' )
280
+ const data = { text : 'text' }
281
+
282
+ fastify . register ( require ( '../index' ) , {
283
+ engine : {
284
+ mustache
285
+ } ,
286
+ options : {
287
+ partials : {
288
+ body : './templates/body.mustache'
289
+ }
290
+ }
291
+ } )
292
+
293
+ fastify . get ( '/' , ( _req , reply ) => {
294
+ reply . view ( './templates/index.mustache' , data )
295
+ } )
296
+
297
+ await fastify . listen ( { port : 0 } )
298
+
299
+ const result = await fetch ( 'http://127.0.0.1:' + fastify . server . address ( ) . port )
300
+ const responseContent = await result . text ( )
301
+
302
+ t . assert . strictEqual ( result . status , 200 )
303
+ t . assert . strictEqual ( result . headers . get ( 'content-length' ) , '' + responseContent . length )
304
+ t . assert . strictEqual ( result . headers . get ( 'content-type' ) , 'text/html; charset=utf-8' )
305
+ t . assert . strictEqual ( mustache . render ( fs . readFileSync ( './templates/index.mustache' , 'utf8' ) , data , { body : '<p>{{ text }}</p>' } ) , responseContent )
306
+
307
+ await fastify . close ( )
308
+ } )
309
+
310
+ test ( 'reply.view with mustache engine with global and local partials' , async t => {
311
+ t . plan ( 4 )
312
+ const fastify = Fastify ( )
313
+ const mustache = require ( 'mustache' )
314
+ const data = { }
315
+
316
+ fastify . register ( require ( '../index' ) , {
317
+ engine : {
318
+ mustache
319
+ } ,
320
+ options : {
321
+ partials : {
322
+ partial1 : './templates/partial-1.mustache'
323
+ }
324
+ }
325
+ } )
326
+
327
+ fastify . get ( '/' , ( _req , reply ) => {
328
+ reply . view ( './templates/index-with-2-partials.mustache' , data , { partials : { partial2 : './templates/partial-2.mustache' } } )
329
+ } )
330
+
331
+ await fastify . listen ( { port : 0 } )
332
+
333
+ const result = await fetch ( 'http://127.0.0.1:' + fastify . server . address ( ) . port )
334
+ const responseContent = await result . text ( )
335
+
336
+ t . assert . strictEqual ( result . status , 200 )
337
+ t . assert . strictEqual ( result . headers . get ( 'content-length' ) , '' + responseContent . length )
338
+ t . assert . strictEqual ( result . headers . get ( 'content-type' ) , 'text/html; charset=utf-8' )
339
+ t . assert . strictEqual (
340
+ mustache . render (
341
+ fs . readFileSync ( './templates/index-with-2-partials.mustache' , 'utf8' ) ,
342
+ data ,
343
+ {
344
+ partial1 : 'Partial 1 - b4d932b9-4baa-4c99-8d14-d45411b9361e\n' , // defined globally
345
+ partial2 : 'Partial 2 - fdab0fe2-6dab-4429-ae9f-dfcb791d1d3d\n' // defined locally
346
+ }
347
+ ) ,
348
+ responseContent
349
+ )
350
+
351
+ await fastify . close ( )
352
+ } )
353
+
276
354
test ( 'reply.view with mustache engine with partials' , async t => {
277
355
t . plan ( 4 )
278
356
const fastify = Fastify ( )
0 commit comments