1
1
import { jest } from '@jest/globals' ;
2
+ import path from 'path' ;
3
+ import { promisify } from 'util' ;
2
4
import PencilResponse from './pencil-response.js' ;
5
+ import templateAssembler from '../../../../lib/template-assembler.js' ;
3
6
4
7
describe ( 'PencilResponse' , ( ) => {
5
8
const assembler = {
6
- getTemplates : ( path ) => new Promise ( ( resolve ) => resolve ( { path } ) ) ,
9
+ getTemplates : ( p ) => new Promise ( ( resolve ) => resolve ( { path : p } ) ) ,
7
10
getTranslations : ( ) => new Promise ( ( resolve ) => resolve ( [ ] ) ) ,
8
11
} ;
9
12
let data ;
@@ -13,7 +16,10 @@ describe('PencilResponse', () => {
13
16
beforeEach ( ( ) => {
14
17
data = {
15
18
context : {
16
- settings : { } ,
19
+ settings : {
20
+ base_url : 'http://localhost:3000' ,
21
+ secure_base_url : 'https://localhost:3000' ,
22
+ } ,
17
23
theme_settings : { } ,
18
24
template_engine : 'handlebars-v3' ,
19
25
} ,
@@ -60,4 +66,49 @@ describe('PencilResponse', () => {
60
66
await pencilResponse . respond ( request , h ) ;
61
67
expect ( h . response ) . toHaveBeenCalledTimes ( 1 ) ;
62
68
} ) ;
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
+ } ) ;
63
114
} ) ;
0 commit comments