-
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathEleventyVirtualTemplatesTest.js
222 lines (182 loc) · 7.19 KB
/
EleventyVirtualTemplatesTest.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
import test from "ava";
import fs from "fs";
import { rimrafSync } from "rimraf";
import { feedPlugin } from "@11ty/eleventy-plugin-rss";
import Eleventy from "../src/Eleventy.js";
import DuplicatePermalinkOutputError from "../src/Errors/DuplicatePermalinkOutputError.js";
test("Virtual templates, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`)
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});
test("Virtual templates with front matter, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("./virtual.md", `---
myKey: myValue
---
# {{ myKey }}`)
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>myValue</h1>`);
t.deepEqual(results[0].rawInput, `# {{ myKey }}`);
});
test("Virtual templates with supplemental data, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# {{ myKey }}`, { myKey: "myValue" })
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>myValue</h1>`);
t.deepEqual(results[0].rawInput, `# {{ myKey }}`);
});
// Supplemental data overrides front matter.
test("Virtual templates with front matter and supplemental data, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `---
myKey1: myValue1
myKey3: myValueFm
---
# {{ myKey1 }}{{ myKey2 }}{{ myKey3 }}`, { myKey2: "myValue2", myKey3: "myValueData" })
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>myValue1myValue2myValueData</h1>`);
t.deepEqual(results[0].rawInput, `# {{ myKey1 }}{{ myKey2 }}{{ myKey3 }}`);
});
test("Virtual template conflicts with file on file system, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs/stubs-virtual-conflict", "./test/stubs/stubs-virtual-conflict/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Virtual template`)
},
});
elev.disableLogger();
await t.throwsAsync(elev.toJSON(), {
message: `A virtual template had the same path as a file on the file system: "./test/stubs/stubs-virtual-conflict/virtual.md"`
});
});
test("Virtual templates try to output to the same file, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual-one.md", "", {
permalink: "/output.html"
})
eleventyConfig.addTemplate("virtual-two.md", "", {
permalink: "/output.html"
})
},
});
elev.disableLogger();
await t.throwsAsync(elev.toJSON(), {
instanceOf: DuplicatePermalinkOutputError,
});
});
// Warning: this test writes to the file system
test("Virtual template writes to file system, issue #1612", async (t) => {
let elev = new Eleventy("./test/stubs-virtual", "./test/stubs-virtual/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`)
},
});
elev.disableLogger();
let [,results] = await elev.write();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
t.true(fs.existsSync("./test/stubs-virtual/_site/virtual/index.html"));
rimrafSync("./test/stubs-virtual/_site/");
});
test("Virtual templates conflict", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`);
eleventyConfig.addTemplate("virtual.md", `# Hello`);
},
});
let e = await t.throwsAsync(async () => {
await elev.toJSON();
});
t.is(e.message, "Virtual template conflict: you can’t add multiple virtual templates that have the same inputPath: virtual.md");
});
// https://github.com/11ty/eleventy-plugin-rss/issues/50
test("RSS virtual templates plugin", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })
eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: "/feed.xml",
collection: {
name: "posts", // iterate over `collections.posts`
limit: 10, // 0 means no limit
},
});
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 2);
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
});
test("Virtual templates as layouts, issue #2307", async (t) => {
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`, {
layout: "virtual.html"
});
let layoutPath = eleventyConfig.directories.getLayoutPathRelativeToInputDirectory("virtual.html");
eleventyConfig.addTemplate(layoutPath, `<!-- Layout -->{{ content }}`);
},
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<!-- Layout --><h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});
test("11ty.js Virtual Templates (object), issue #3347", async (t) => {
let templateDefinition = {
data: () => {
return { var: 2 };
},
render: function(data) {
return `this is a test ${data.var}.`;
}
};
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.11ty.js", templateDefinition);
}
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `this is a test 2.`);
// TODO support rawInput on 11ty.js?
// t.deepEqual(results[0].rawInput, templateDefinition);
});
test("11ty.js Virtual Templates (function), issue #3347", async (t) => {
let templateDefinition = function(data) {
return `this is a test ${data.page.url}.`;
};
let elev = new Eleventy("./test/stubs-virtual-nowrite", "./test/stubs-virtual-nowrite/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.11ty.js", templateDefinition);
}
});
let results = await elev.toJSON();
t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `this is a test /virtual/.`);
// TODO support rawInput on 11ty.js?
// t.deepEqual(results[0].rawInput, templateDefinition);
});