-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathTemplateRenderHandlebarsTest.js
211 lines (181 loc) · 5.71 KB
/
TemplateRenderHandlebarsTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import test from "ava";
import TemplateRender from "../src/TemplateRender";
// Handlebars
test("Handlebars", t => {
t.is(new TemplateRender("hbs").getEngineName(), "hbs");
});
test("Handlebars Render", async t => {
let fn = await new TemplateRender("hbs").getCompiledTemplate(
"<p>{{name}}</p>"
);
t.is(await fn({ name: "Zach" }), "<p>Zach</p>");
});
test("Handlebars Render Unescaped Output (no HTML)", async t => {
let fn = await new TemplateRender("hbs").getCompiledTemplate(
"<p>{{{name}}}</p>"
);
t.is(await fn({ name: "Zach" }), "<p>Zach</p>");
});
test("Handlebars Render Escaped Output", async t => {
let fn = await new TemplateRender("hbs").getCompiledTemplate(
"<p>{{name}}</p>"
);
t.is(await fn({ name: "<b>Zach</b>" }), "<p><b>Zach</b></p>");
});
test("Handlebars Render Unescaped Output (HTML)", async t => {
let fn = await new TemplateRender("hbs").getCompiledTemplate(
"<p>{{{name}}}</p>"
);
t.is(await fn({ name: "<b>Zach</b>" }), "<p><b>Zach</b></p>");
});
test("Handlebars Render Partial", async t => {
let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate(
"<p>{{> included}}</p>"
);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Handlebars Render Partial (Subdirectory)", async t => {
let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate(
"<p>{{> subfolder/included}}</p>"
);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Handlebars Render Partial with variable", async t => {
let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate(
"<p>{{> includedvar}}</p>"
);
t.is(await fn({ name: "Zach" }), "<p>This is a Zach.</p>");
});
test("Handlebars Render: with Library Override", async t => {
let tr = new TemplateRender("hbs");
let lib = require("handlebars");
tr.engine.setLibrary(lib);
let fn = await tr.getCompiledTemplate("<p>{{name}}</p>");
t.is(await fn({ name: "Zach" }), "<p>Zach</p>");
});
test("Handlebars Render Helper", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addHelpers({
helpername: function() {
return "Zach";
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{helpername}} {{name}}.</p>"
);
t.is(await fn({ name: "Zach" }), "<p>This is a Zach Zach.</p>");
});
test("Handlebars Render Helper (uses argument)", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addHelpers({
helpername2: function(name) {
return "Zach";
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{helpername2 name}}.</p>"
);
t.is(await fn({ name: "Zach" }), "<p>This is a Zach.</p>");
});
test("Handlebars Render Shortcode", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addShortcodes({
shortcodename: function(name) {
return name.toUpperCase();
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{shortcodename name}}.</p>"
);
t.is(await fn({ name: "Howdy" }), "<p>This is a HOWDY.</p>");
});
test("Handlebars Render Shortcode (Multiple args)", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addShortcodes({
shortcodename2: function(name, name2) {
return name.toUpperCase() + name2.toUpperCase();
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{shortcodename2 name name2}}.</p>"
);
t.is(
await fn({ name: "Howdy", name2: "Zach" }),
"<p>This is a HOWDYZACH.</p>"
);
});
test("Handlebars Render Paired Shortcode", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addPairedShortcodes({
shortcodename3: function(content, name, options) {
return (content + name).toUpperCase();
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{#shortcodename3 name}}Testing{{/shortcodename3}}.</p>"
);
t.is(await fn({ name: "Howdy" }), "<p>This is a TESTINGHOWDY.</p>");
});
test("Handlebars Render Paired Shortcode (Spaces)", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addPairedShortcodes({
shortcodename4: function(content, name, options) {
return (content + name).toUpperCase();
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{# shortcodename4 name }}Testing{{/ shortcodename4 }}.</p>"
);
t.is(await fn({ name: "Howdy" }), "<p>This is a TESTINGHOWDY.</p>");
});
test("Handlebars Render Paired Shortcode with a Nested Single Shortcode", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addShortcodes({
shortcodechild: function(txt, options) {
return txt;
}
});
tr.engine.addPairedShortcodes({
shortcodeparent: function(content, name, name2, options) {
return (content + name + name2).toUpperCase();
}
});
let fn = await tr.getCompiledTemplate(
"<p>This is a {{# shortcodeparent name name2 }}{{shortcodechild 'CHILD CONTENT'}}{{/ shortcodeparent }}.</p>"
);
t.is(
await fn({ name: "Howdy", name2: "Zach" }),
"<p>This is a CHILD CONTENTHOWDYZACH.</p>"
);
});
test("Handlebars Render Raw Output (Issue #436)", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addHelpers({
"raw-helper": function(options) {
return options.fn();
}
});
let fn = await tr.getCompiledTemplate(
"{{{{raw-helper}}}}{{bar}}{{{{/raw-helper}}}}"
);
t.is(await fn({ name: "Zach" }), "{{bar}}");
});
test("Handlebars Render Raw Output (Issue #436 with if statement)", async t => {
let tr = new TemplateRender("hbs");
tr.engine.addHelpers({
"raw-helper": function(options) {
return options.fn();
}
});
let fn = await tr.getCompiledTemplate(
`{{{{raw-helper}}}}{{#if ready}}
<p>Ready</p>
{{/if}}{{{{/raw-helper}}}}`
);
t.is(
await fn({ name: "Zach" }),
`{{#if ready}}
<p>Ready</p>
{{/if}}`
);
});