11import { jest } from '@jest/globals' ;
2+ import path from 'path' ;
3+ import { promisify } from 'util' ;
24import PencilResponse from './pencil-response.js' ;
5+ import templateAssembler from '../../../../lib/template-assembler.js' ;
36
47describe ( 'PencilResponse' , ( ) => {
58 const assembler = {
6- getTemplates : ( path ) => new Promise ( ( resolve ) => resolve ( { path } ) ) ,
9+ getTemplates : ( p ) => new Promise ( ( resolve ) => resolve ( { path : p } ) ) ,
710 getTranslations : ( ) => new Promise ( ( resolve ) => resolve ( [ ] ) ) ,
811 } ;
912 let data ;
@@ -13,7 +16,10 @@ describe('PencilResponse', () => {
1316 beforeEach ( ( ) => {
1417 data = {
1518 context : {
16- settings : { } ,
19+ settings : {
20+ base_url : 'http://localhost:3000' ,
21+ secure_base_url : 'https://localhost:3000' ,
22+ } ,
1723 theme_settings : { } ,
1824 template_engine : 'handlebars-v3' ,
1925 } ,
@@ -60,4 +66,49 @@ describe('PencilResponse', () => {
6066 await pencilResponse . respond ( request , h ) ;
6167 expect ( h . response ) . toHaveBeenCalledTimes ( 1 ) ;
6268 } ) ;
69+
70+ describe ( 'it should successfully render a tempalte with dynamic partials' , ( ) => {
71+ it ( 'should render a template with dynamic partials' , async ( ) => {
72+ let result = '' ;
73+ data . template_file = 'pages/page3' ;
74+ data . context . template_engine = 'handlebars-v4' ;
75+
76+ h . response = ( output ) => {
77+ result = output ;
78+ return response ;
79+ } ;
80+ const themeAssembler = {
81+ async getTemplates ( templatesPath , processor ) {
82+ const templates = await promisify ( templateAssembler . assemble ) (
83+ path . join ( process . cwd ( ) , 'test/_mocks/themes/valid' , 'templates' ) ,
84+ templatesPath ,
85+ ) ;
86+ return processor ( templates ) ;
87+ } ,
88+ getTranslations : async ( ) => {
89+ return { } ;
90+ } ,
91+ } ;
92+ const pencilResponse = new PencilResponse ( data , themeAssembler ) ;
93+ await pencilResponse . respond ( request , h ) ;
94+ expect ( result . content ) . toEqual ( `<!DOCTYPE html>
95+ <html>
96+ <head>
97+ <title>page3.html</title>
98+
99+ </head>
100+ <body>
101+ <h1></h1>
102+ Here is the list:
103+ <ul>
104+ <li>
105+ <a href="item_link_1">Item 1</a>
106+ </li> <li>
107+ <a href="item_link_2">Item 2</a>
108+ </li> <li>
109+ <a href="item_link_3">Item 3</a>
110+ </li><ul></body>
111+ </html>` ) ;
112+ } ) ;
113+ } ) ;
63114} ) ;
0 commit comments