-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathTemplateLayoutTest.js
156 lines (131 loc) · 4.77 KB
/
TemplateLayoutTest.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
import test from "ava";
import TemplateLayout from "../src/TemplateLayout.js";
import EleventyExtensionMap from "../src/EleventyExtensionMap.js";
import { renderLayoutViaLayout } from "./_getRenderedTemplates.js";
import { getTemplateConfigInstance } from "./_testHelpers.js";
async function getTemplateLayoutInstance(key, inputDir, map) {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: inputDir,
}
});
if (!map) {
map = new EleventyExtensionMap(eleventyConfig);
map.setFormats(["liquid", "md", "njk", "html", "11ty.js"]);
}
let layout = new TemplateLayout(key, map, eleventyConfig);
return layout;
}
test("Creation", async (t) => {
let tl = await getTemplateLayoutInstance("base", "./test/stubs");
t.is(tl.getInputPath(), "./test/stubs/_includes/base.njk");
await t.throwsAsync(async () => {
await getTemplateLayoutInstance("doesnotexist", "./test/stubs");
});
});
test("Get Layout Chain", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-inherit-a.njk", "./test/stubs");
await tl.getData();
t.deepEqual(await tl.getLayoutChain(), [
"./test/stubs/_includes/layouts/layout-inherit-a.njk",
"./test/stubs/_includes/layouts/layout-inherit-b.njk",
"./test/stubs/_includes/layouts/layout-inherit-c.njk",
]);
});
test("Get Front Matter Data", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-inherit-a.njk", "./test/stubs");
t.is(tl.getInputPath(), "./test/stubs/_includes/layouts/layout-inherit-a.njk");
let data = await tl.getData();
t.deepEqual(data, {
inherits: "a",
secondinherits: "b",
thirdinherits: "c",
});
t.deepEqual(await tl.getLayoutChain(), [
"./test/stubs/_includes/layouts/layout-inherit-a.njk",
"./test/stubs/_includes/layouts/layout-inherit-b.njk",
"./test/stubs/_includes/layouts/layout-inherit-c.njk",
]);
t.deepEqual(await tl.getData(), {
inherits: "a",
secondinherits: "b",
thirdinherits: "c",
});
});
test("Render Layout", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-inherit-a.njk", "./test/stubs");
t.is(
(
await renderLayoutViaLayout(tl, {
inherits: "a",
secondinherits: "b",
thirdinherits: "c",
})
).trim(),
"a b a c"
);
});
test("Render Layout (Pass in template content)", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-inherit-a.njk", "./test/stubs");
t.is(
(
await renderLayoutViaLayout(tl,
{ inherits: "a", secondinherits: "b", thirdinherits: "c" },
"TEMPLATE_CONTENT"
)
).trim(),
"TEMPLATE_CONTENT a b a c"
);
});
test("Render Layout (Pass in undefined template content)", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-contentdump.njk", "./test/stubs");
t.is(
await renderLayoutViaLayout(tl, { inherits: "a", secondinherits: "b", thirdinherits: "c" }, undefined),
"this is bad a b a c"
);
});
test("Render Layout (Pass in null template content)", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-contentdump.njk", "./test/stubs");
t.is(
await renderLayoutViaLayout(tl, { inherits: "a", secondinherits: "b", thirdinherits: "c" }, null),
" a b a c"
);
});
test("Render Layout (Pass in empty template content)", async (t) => {
let tl = await getTemplateLayoutInstance("layouts/layout-contentdump.njk", "./test/stubs");
t.is(await renderLayoutViaLayout(tl, { inherits: "a", secondinherits: "b", thirdinherits: "c" }, ""), " a b a c");
});
test("Cache Duplicates (use full key for cache)", async (t) => {
// if two different layouts used the same filename but had different inputdirs, make sure templatelayout cache is unique
let tla = await getTemplateLayoutInstance(
"layout.njk",
"./test/stubs/templateLayoutCacheDuplicates"
);
t.is((await renderLayoutViaLayout(tla, {})).trim(), "Hello A");
let tlb = await getTemplateLayoutInstance(
"layout.njk",
"./test/stubs/templateLayoutCacheDuplicates-b"
);
t.is((await renderLayoutViaLayout(tlb, {})).trim(), "Hello B");
t.is((await renderLayoutViaLayout(tla, {})).trim(), "Hello A");
});
test("Throw an error if a layout references itself as the layout", async (t) => {
await t.throwsAsync(async () => {
const tl = await getTemplateLayoutInstance(
"layout-cycle-self.njk",
"./test/stubs-circular-layout"
);
const layoutChain = await tl._testGetLayoutChain();
return layoutChain;
});
});
test("Throw an error if a circular layout chain is detected", async (t) => {
await t.throwsAsync(async () => {
const tl = await getTemplateLayoutInstance(
"layout-cycle-a.njk",
"./test/stubsstubs-circular-layout"
);
const layoutChain = await tl._testGetLayoutChain();
return layoutChain;
});
});