-
-
Notifications
You must be signed in to change notification settings - Fork 471
/
TemplateRenderCustomTest.js
233 lines (200 loc) · 6.24 KB
/
TemplateRenderCustomTest.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const test = require("ava");
const TemplateData = require("../src/TemplateData");
const TemplateRender = require("../src/TemplateRender");
const EleventyExtensionMap = require("../src/EleventyExtensionMap");
const TemplateConfig = require("../src/TemplateConfig");
const getNewTemplate = require("./_getNewTemplateForTests");
const { createSSRApp } = require("vue");
const { renderToString } = require("@vue/server-renderer");
function getNewTemplateRender(name, inputDir, eleventyConfig) {
if (!eleventyConfig) {
eleventyConfig = new TemplateConfig();
}
let tr = new TemplateRender(name, inputDir, eleventyConfig);
tr.extensionMap = new EleventyExtensionMap([], eleventyConfig);
return tr;
}
test("Custom plaintext Render", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "txt",
key: "txt",
compile: function (str, inputPath) {
// plaintext
return function (data) {
return str;
};
},
});
let tr = getNewTemplateRender("txt", null, eleventyConfig);
let fn = await tr.getCompiledTemplate("<p>Paragraph</p>");
t.is(await fn(), "<p>Paragraph</p>");
t.is(await fn({}), "<p>Paragraph</p>");
});
test("Custom Markdown Render with `compile` override", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "md",
key: "md",
compile: function (str, inputPath) {
return function (data) {
return `<not-markdown>${str.trim()}</not-markdown>`;
};
},
});
let tr = getNewTemplateRender("md", null, eleventyConfig);
let fn = await tr.getCompiledTemplate("# Markdown?");
t.is((await fn()).trim(), "<not-markdown># Markdown?</not-markdown>");
t.is((await fn({})).trim(), "<not-markdown># Markdown?</not-markdown>");
});
test("Custom Markdown Render without `compile` override", async (t) => {
let eleventyConfig = new TemplateConfig();
let initCalled = false;
eleventyConfig.userConfig.extensionMap.add({
extension: "md",
key: "md",
init: function () {
initCalled = true;
},
});
let tr = getNewTemplateRender("md", null, eleventyConfig);
let fn = await tr.getCompiledTemplate("# Header");
t.is(initCalled, true);
t.is((await fn()).trim(), "<h1>Header</h1>");
t.is((await fn({})).trim(), "<h1>Header</h1>");
});
test("Custom Markdown Render with `compile` override + call to default compiler", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "md",
key: "md",
compile: function (str, inputPath) {
return async function (data) {
const result = await this.defaultRenderer(data);
return `<custom-wrapper>${result.trim()}</custom-wrapper>`;
};
},
});
let tr = getNewTemplateRender("md", null, eleventyConfig);
let fn = await tr.getCompiledTemplate("Hey {{name}}");
t.is((await fn()).trim(), "<custom-wrapper><p>Hey</p></custom-wrapper>");
t.is(
(await fn({ name: "Zach" })).trim(),
"<custom-wrapper><p>Hey Zach</p></custom-wrapper>"
);
});
test("Custom Vue Render", async (t) => {
let tr = getNewTemplateRender("vue");
tr.eleventyConfig.userConfig.extensionMap.add({
extension: "vue",
key: "vue",
compile: function (str) {
return async function (data) {
const app = createSSRApp({
template: str,
data: function () {
return data;
},
});
return renderToString(app);
};
},
});
let fn = await tr.getCompiledTemplate('<p v-html="test">Paragraph</p>');
t.is(await fn({ test: "Hello" }), "<p>Hello</p>");
});
const sass = require("sass");
test("Custom Sass Render", async (t) => {
let tr = getNewTemplateRender("sass");
tr.eleventyConfig.userConfig.extensionMap.add({
extension: "sass",
key: "sass",
compile: function (str, inputPath) {
// TODO declare data variables as SASS variables?
return async function (data) {
return new Promise(function (resolve, reject) {
sass.render(
{
data: str,
includePaths: [tr.inputDir, tr.includesDir],
style: "expanded",
indentType: "space",
// TODO
// sourcemap: "file",
outFile: "test_this_is_to_not_write_a_file.css",
},
function (error, result) {
if (error) {
reject(error);
} else {
resolve(result.css.toString("utf8"));
}
}
);
});
};
},
});
let fn = await tr.getCompiledTemplate("$color: blue; p { color: $color; }");
t.is(
(await fn({})).trim(),
`p {
color: blue;
}`
);
});
/*
serverPrefetch: function() {
return this.getBlogAuthors().then(response => this.glossary = response)
},
*/
test("JavaScript functions should not be mutable but not *that* mutable", async (t) => {
t.plan(3);
let eleventyConfig = new TemplateConfig();
let instance = {
dataForCascade: function () {
// was mutating this.config.javascriptFunctions!
this.shouldnotmutatethething = 1;
return {};
},
};
eleventyConfig.userConfig.extensionMap.add({
extension: "js1",
key: "js1",
getData: ["dataForCascade"],
getInstanceFromInputPath: function (inputPath) {
t.truthy(true);
return instance;
},
compile: function (str, inputPath) {
t.falsy(this.config.javascriptFunctions.shouldnotmutatethething);
// plaintext
return (data) => {
return str;
};
},
});
let tmpl = getNewTemplate(
"./test/stubs-custom-extension/test.js1",
"./test/stubs-custom-extension/",
"dist",
null,
null,
eleventyConfig
);
let data = await tmpl.getData();
t.is(await tmpl.render(data), "<p>Paragraph</p>");
});
test("Return undefined in compile to ignore #2267", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.extensionMap.add({
extension: "txt",
key: "txt",
compile: function (str, inputPath) {
return;
},
});
let tr = getNewTemplateRender("txt", null, eleventyConfig);
let fn = await tr.getCompiledTemplate("<p>Paragraph</p>");
t.is(fn, undefined);
});