-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
TemplateTest.js
1953 lines (1629 loc) · 56.7 KB
/
TemplateTest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import test from "ava";
import fs from "fs";
import pretty from "pretty";
import TOML from "@iarna/toml";
import TemplateData from "../src/Data/TemplateData.js";
import FileSystemSearch from "../src/FileSystemSearch.js";
import EleventyExtensionMap from "../src/EleventyExtensionMap.js";
import EleventyErrorUtil from "../src/Errors/EleventyErrorUtil.js";
import TemplateContentPrematureUseError from "../src/Errors/TemplateContentPrematureUseError.js";
import { normalizeNewLines } from "./Util/normalizeNewLines.js";
import eventBus from "../src/EventBus.js";
import getNewTemplate from "./_getNewTemplateForTests.js";
import { getRenderedTemplates as getRenderedTmpls, renderLayout, renderTemplate } from "./_getRenderedTemplates.js";
import { getTemplateConfigInstance, getTemplateConfigInstanceCustomCallback } from "./_testHelpers.js";
const fsp = fs.promises;
async function getRenderedData(tmpl, pageNumber = 0) {
let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
return templates[pageNumber].data;
}
function cleanHtml(str) {
return pretty(str, { ocd: true });
}
async function _testCompleteRender(tmpl) {
let data = await tmpl.getData();
let entries = await tmpl.getTemplateMapEntries(data);
let nestedContent = await Promise.all(
entries.map(async (entry) => {
entry._pages = await entry.template.getTemplates(entry.data);
return Promise.all(
entry._pages.map(async (page) => {
page.templateContent = await page.template.renderPageEntryWithoutLayout(page);
return page.template.renderPageEntry(page);
})
);
})
);
let contents = [].concat(...nestedContent);
return contents;
}
test("getTemplateSubFolder", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
t.is(tmpl.getTemplateSubfolder(), "");
});
test("getTemplateSubFolder, output is a subdir of input", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/template.liquid",
"./test/stubs/",
"./test/stubs/_site"
);
t.is(tmpl.getTemplateSubfolder(), "");
});
test("output path maps to an html file", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
t.is(tmpl.inputDir, "./test/stubs/");
t.is(tmpl.outputDir, "./dist/");
t.is(tmpl.getTemplateSubfolder(), "");
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./dist/template/index.html");
});
test("subfolder outputs to a subfolder", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/subfolder/subfolder.liquid",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(tmpl.parsed.dir, "./test/stubs/subfolder");
t.is(tmpl.getTemplateSubfolder(), "subfolder");
t.is(await tmpl.getOutputPath(data), "./dist/subfolder/index.html");
});
test("subfolder outputs to double subfolder", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/subfolder/subfolder/subfolder.liquid",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(tmpl.parsed.dir, "./test/stubs/subfolder/subfolder");
t.is(tmpl.getTemplateSubfolder(), "subfolder/subfolder");
t.is(await tmpl.getOutputPath(data), "./dist/subfolder/subfolder/index.html");
});
test("HTML files output to the same as the input directory have a file suffix added (only if index, this is not index).", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/testing.html", "./test/stubs", "./test/stubs");
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./test/stubs/testing/index.html");
});
test("HTML files output to the same as the input directory have a file suffix added (only if index, this _is_ index).", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/index.html", "./test/stubs", "./test/stubs");
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./test/stubs/index.html");
});
test("HTML files output to the same as the input directory have a file suffix added (only if index, this _is_ index, subfolder).", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/subfolder/index.html",
"./test/stubs",
"./test/stubs"
);
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./test/stubs/subfolder/index.html");
});
test("Test raw front matter from template (yaml)", async (t) => {
// https://github.com/jonschlinkert/gray-matter/blob/master/examples/yaml.js
let tmpl = await getNewTemplate(
"./test/stubs/templateFrontMatter.liquid",
"./test/stubs/",
"./dist"
);
t.truthy(await tmpl.getInputContent(), "template exists and can be opened.");
t.is((await tmpl._testGetFrontMatter()).data.key1, "value1");
t.is((await tmpl._testGetFrontMatter()).data.key3, "value3");
let data = await tmpl.getData();
t.is(data.key1, "value1");
t.is(data.key3, "value3");
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), "c:value1:value2:value3");
});
test("Test raw front matter from template (json)", async (t) => {
// https://github.com/jonschlinkert/gray-matter/blob/master/examples/json.js
let tmpl = await getNewTemplate(
"./test/stubs/templateFrontMatterJson.liquid",
"./test/stubs/",
"./dist"
);
t.is((await tmpl._testGetFrontMatter()).data.key1, "value1");
t.is((await tmpl._testGetFrontMatter()).data.key3, "value3");
let data = await tmpl.getData();
t.is(data.key1, "value1");
t.is(data.key3, "value3");
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), "c:value1:value2:value3");
});
test("Test raw front matter from template (js)", async (t) => {
// https://github.com/jonschlinkert/gray-matter/blob/master/examples/javascript.js
let tmpl = await getNewTemplate(
"./test/stubs/templateFrontMatterJs.njk",
"./test/stubs/",
"./dist"
);
t.is((await tmpl._testGetFrontMatter()).data.key1, "value1");
t.is((await tmpl._testGetFrontMatter()).data.key3, "value3");
let data = await tmpl.getData();
t.is(data.key1, "value1");
t.is(data.key3, "value3");
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), "c:value1:VALUE2:value3");
});
test("Test that getData() works", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/templateFrontMatter.liquid",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(data.key1, "value1");
t.is(data.key3, "value3");
});
test("One Layout (using new content var)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/templateWithLayoutKey.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is((await tmpl._testGetFrontMatter()).data[tmpl.config.keys.layout], "defaultLayout");
let data = await tmpl.getData();
t.is(data[tmpl.config.keys.layout], "defaultLayout");
t.is(
normalizeNewLines(cleanHtml(await renderLayout(tmpl, data))),
`<div id="layout">
<p>Hello.</p>
</div>`
);
t.is(data.keymain, "valuemain");
t.is(data.keylayout, "valuelayout");
});
test("One Layout (using content)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/templateWithLayoutContent.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is((await tmpl._testGetFrontMatter()).data[tmpl.config.keys.layout], "defaultLayoutLayoutContent");
let data = await tmpl.getData();
t.is(data[tmpl.config.keys.layout], "defaultLayoutLayoutContent");
t.is(
normalizeNewLines(cleanHtml(await renderLayout(tmpl, data))),
`<div id="layout">
<p>Hello.</p>
</div>`
);
t.is(data.keymain, "valuemain");
t.is(data.keylayout, "valuelayout");
});
test("One Layout (layouts disabled)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/templateWithLayoutContent.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is((await tmpl._testGetFrontMatter()).data[tmpl.config.keys.layout], "defaultLayoutLayoutContent");
let data = await tmpl.getData();
t.is(data[tmpl.config.keys.layout], "defaultLayoutLayoutContent");
t.is(cleanHtml(await tmpl.renderPageEntryWithoutLayout({
rawInput: await tmpl.getPreRender(),
data: data
})), "<p>Hello.</p>");
t.is(data.keymain, "valuemain");
t.is(data.keylayout, "valuelayout");
});
test("One Layout (liquid test)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/templateWithLayout.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is((await tmpl._testGetFrontMatter()).data[tmpl.config.keys.layout], "layoutLiquid.liquid");
let data = await tmpl.getData();
t.is(data[tmpl.config.keys.layout], "layoutLiquid.liquid");
t.is(
normalizeNewLines(cleanHtml(await renderLayout(tmpl, data))),
`<div id="layout">
<p>Hello.</p>
</div>`
);
t.is(data.keymain, "valuemain");
t.is(data.keylayout, "valuelayout");
});
test("Two Layouts", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/templateTwoLayouts.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is((await tmpl._testGetFrontMatter()).data[tmpl.config.keys.layout], "layout-a");
let data = await tmpl.getData();
t.is(data[tmpl.config.keys.layout], "layout-a");
t.is(data.key1, "value1");
t.is(
normalizeNewLines(cleanHtml(await renderLayout(tmpl, data))),
`<div id="layout-b">
<div id="layout-a">
<p>value2-a</p>
</div>
</div>`
);
t.is(data.daysPosted, 152);
});
test("Liquid template", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/formatTest.liquid",
"./test/stubs/",
"dist",
dataObj,
null,
eleventyConfig
);
t.is(await renderTemplate(tmpl, await tmpl.getData()), "<p>Zach</p>");
});
test("Liquid template with include", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/includer.liquid", "./test/stubs/", "dist");
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<p>This is an include.</p>");
});
test("Permalink output directory", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/permalinked.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./dist/permalinksubfolder/index.html");
});
test("Permalink output directory from layout", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/permalink-in-layout.liquid",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./dist/hello/index.html");
});
test("Permalink output directory from layout (fileslug)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/permalink-in-layout-fileslug.liquid",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(await tmpl.getOutputPath(data), "./dist/test/permalink-in-layout-fileslug/index.html");
});
test("Layout from template-data-file that has a permalink (fileslug) Issue #121", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
let tmpl = await getNewTemplate(
"./test/stubs/permalink-data-layout/test.njk",
"./test/stubs/",
"./dist",
dataObj
);
let data = await tmpl.getData();
let renderedTmpl = (await getRenderedTmpls(tmpl, data))[0];
t.is(renderedTmpl.templateContent, "Wrapper:Test 1:test");
t.is(await tmpl.getOutputPath(data), "./dist/test/index.html");
});
test("Fileslug in an 11ty.js template Issue #588", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/fileslug.11ty.cjs", "./test/stubs/", "./dist");
let data = await tmpl.getData();
let renderedTmpl = (await getRenderedTmpls(tmpl, data))[0];
t.is(renderedTmpl.templateContent, "<p>fileslug</p>");
});
test("Local template data file import (without a global data json)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
dataObj.setFileSystemSearch(new FileSystemSearch());
await dataObj.getGlobalData();
let tmpl = await getNewTemplate(
"./test/stubs/component/component.njk",
"./test/stubs/",
"./dist",
dataObj
);
let data = await tmpl.getData();
t.deepEqual(await dataObj.getLocalDataPaths(tmpl.getInputPath()), [
"./test/stubs/stubs.json",
"./test/stubs/stubs.11tydata.json",
"./test/stubs/stubs.11tydata.mjs",
"./test/stubs/stubs.11tydata.cjs",
"./test/stubs/stubs.11tydata.js",
"./test/stubs/component/component.json",
"./test/stubs/component/component.11tydata.json",
"./test/stubs/component/component.11tydata.mjs",
"./test/stubs/component/component.11tydata.cjs",
"./test/stubs/component/component.11tydata.js",
]);
t.is(data.localdatakey1, "localdatavalue1");
t.is(await renderTemplate(tmpl, data), "localdatavalue1");
});
test("Local template data file import (two subdirectories deep)", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
dataObj.setFileSystemSearch(new FileSystemSearch());
await dataObj.getGlobalData();
let tmpl = await getNewTemplate(
"./test/stubs/firstdir/seconddir/component.njk",
"./test/stubs/",
"./dist",
dataObj
);
t.deepEqual(await dataObj.getLocalDataPaths(tmpl.getInputPath()), [
"./test/stubs/stubs.json",
"./test/stubs/stubs.11tydata.json",
"./test/stubs/stubs.11tydata.mjs",
"./test/stubs/stubs.11tydata.cjs",
"./test/stubs/stubs.11tydata.js",
"./test/stubs/firstdir/firstdir.json",
"./test/stubs/firstdir/firstdir.11tydata.json",
"./test/stubs/firstdir/firstdir.11tydata.mjs",
"./test/stubs/firstdir/firstdir.11tydata.cjs",
"./test/stubs/firstdir/firstdir.11tydata.js",
"./test/stubs/firstdir/seconddir/seconddir.json",
"./test/stubs/firstdir/seconddir/seconddir.11tydata.json",
"./test/stubs/firstdir/seconddir/seconddir.11tydata.mjs",
"./test/stubs/firstdir/seconddir/seconddir.11tydata.cjs",
"./test/stubs/firstdir/seconddir/seconddir.11tydata.js",
"./test/stubs/firstdir/seconddir/component.json",
"./test/stubs/firstdir/seconddir/component.11tydata.json",
"./test/stubs/firstdir/seconddir/component.11tydata.mjs",
"./test/stubs/firstdir/seconddir/component.11tydata.cjs",
"./test/stubs/firstdir/seconddir/component.11tydata.js",
]);
});
test("Posts inherits local JSON, layouts", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
dataObj.setFileSystemSearch(new FileSystemSearch());
await dataObj.getGlobalData();
let tmpl = await getNewTemplate(
"./test/stubs/posts/post1.njk",
"./test/stubs/",
"./dist",
dataObj
);
let localDataPaths = await dataObj.getLocalDataPaths(tmpl.getInputPath());
t.deepEqual(localDataPaths, [
"./test/stubs/stubs.json",
"./test/stubs/stubs.11tydata.json",
"./test/stubs/stubs.11tydata.mjs",
"./test/stubs/stubs.11tydata.cjs",
"./test/stubs/stubs.11tydata.js",
"./test/stubs/posts/posts.json",
"./test/stubs/posts/posts.11tydata.json",
"./test/stubs/posts/posts.11tydata.mjs",
"./test/stubs/posts/posts.11tydata.cjs",
"./test/stubs/posts/posts.11tydata.js",
"./test/stubs/posts/post1.json",
"./test/stubs/posts/post1.11tydata.json",
"./test/stubs/posts/post1.11tydata.mjs",
"./test/stubs/posts/post1.11tydata.cjs",
"./test/stubs/posts/post1.11tydata.js",
]);
let localData = await dataObj.getTemplateDirectoryData(tmpl.getInputPath());
t.is(localData.layout, "mylocallayout.njk");
let globalData = await dataObj.getGlobalData();
t.truthy(globalData.pkg);
let data = await tmpl.getData();
t.is(localData.layout, "mylocallayout.njk");
t.is(
normalizeNewLines((await renderTemplate(tmpl, data)).trim()),
`<div id="locallayout">Post1
</div>`
);
});
test("Template and folder name are the same, make sure data imports work ok", async (t) => {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: "test/stubs",
output: "dist"
}
});
let dataObj = new TemplateData(eleventyConfig);
dataObj.setProjectUsingEsm(true);
dataObj.setFileSystemSearch(new FileSystemSearch());
await dataObj.getGlobalData();
let tmpl = await getNewTemplate(
"./test/stubs/posts/posts.njk",
"./test/stubs/",
"./dist",
dataObj
);
let localDataPaths = await dataObj.getLocalDataPaths(tmpl.getInputPath());
t.deepEqual(localDataPaths, [
"./test/stubs/stubs.json",
"./test/stubs/stubs.11tydata.json",
"./test/stubs/stubs.11tydata.mjs",
"./test/stubs/stubs.11tydata.cjs",
"./test/stubs/stubs.11tydata.js",
"./test/stubs/posts/posts.json",
"./test/stubs/posts/posts.11tydata.json",
"./test/stubs/posts/posts.11tydata.mjs",
"./test/stubs/posts/posts.11tydata.cjs",
"./test/stubs/posts/posts.11tydata.js",
]);
let localData = await dataObj.getTemplateDirectoryData(tmpl.getInputPath());
t.is(localData.layout, "mylocallayout.njk");
let globalData = await dataObj.getGlobalData();
t.truthy(globalData.pkg);
let data = await tmpl.getData();
t.is(localData.layout, "mylocallayout.njk");
t.is(
normalizeNewLines((await renderTemplate(tmpl, data)).trim()),
`<div id="locallayout">Posts
</div>`
);
});
test("Clone the template", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/default.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
let cloned = await tmpl.clone();
let clonedData = await cloned.getData();
t.is(await tmpl.getOutputPath(data), "./dist/default/index.html");
t.is(await cloned.getOutputPath(clonedData), "./dist/default/index.html");
t.is(cloned.extensionMap, tmpl.extensionMap);
});
test("getRenderedData() has all the page variables", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
let data = await getRenderedData(tmpl);
t.truthy(data.page.url);
t.is(data.page.url, "/template/");
t.is(data.page.fileSlug, "template");
t.is(data.page.filePathStem, "/template");
t.truthy(data.page.date.getTime());
t.is(data.page.inputPath, "./test/stubs/template.liquid");
t.is(data.page.outputPath, "./dist/template/index.html");
});
test("Issue #603: page.date Liquid", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/pagedate.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
t.truthy(data.page.date);
t.truthy(data.page.date.toUTCString());
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), data.page.date.toString());
});
test("Issue #603: page.date Nunjucks", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/pagedate.njk", "./test/stubs/", "./dist");
let data = await tmpl.getData();
t.truthy(data.page.date);
t.truthy(data.page.date.toUTCString());
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), data.page.date.toString());
});
test("Issue #603: page.date.toUTCString() Nunjucks", async (t) => {
// Note this is not supported in Liquid
let tmpl = await getNewTemplate("./test/stubs/pagedateutc.njk", "./test/stubs/", "./dist");
let data = await tmpl.getData();
t.truthy(data.page.date);
t.truthy(data.page.date.toUTCString());
let pages = await getRenderedTmpls(tmpl, data);
t.is(pages[0].templateContent.trim(), data.page.date.toUTCString());
});
test("getTemplates() data has all the root variables", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
t.is(templates[0].url, "/template/");
t.is(templates[0].fileSlug, "template");
t.is(templates[0].filePathStem, "/template");
t.truthy(templates[0].date.getTime());
t.is(templates[0].inputPath, "./test/stubs/template.liquid");
t.is(templates[0].outputPath, "./dist/template/index.html");
});
test("getTemplates() data has all the page variables", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
t.is(templates[0].data.page.url, "/template/");
t.is(templates[0].data.page.fileSlug, "template");
t.is(templates[0].filePathStem, "/template");
t.truthy(templates[0].data.page.date.getTime());
t.is(templates[0].data.page.inputPath, "./test/stubs/template.liquid");
t.is(templates[0].data.page.outputPath, "./dist/template/index.html");
});
// Warning `getRenderedTemplates()` is a test function now, so this might be a test testing the tests
test("getRenderedTemplates() data has all the page variables", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/template.liquid", "./test/stubs/", "./dist");
let data = await tmpl.getData();
let templates = await getRenderedTmpls(tmpl, data);
t.is(templates[0].data.page.url, "/template/");
t.is(templates[0].data.page.fileSlug, "template");
t.is(templates[0].filePathStem, "/template");
t.truthy(templates[0].data.page.date.getTime());
t.is(templates[0].data.page.inputPath, "./test/stubs/template.liquid");
t.is(templates[0].data.page.outputPath, "./dist/template/index.html");
});
test("getRenderedData() has good slug (empty, index)", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/index.liquid", "./test/stubs/", "./dist");
let data = await getRenderedData(tmpl);
t.is(data.page.fileSlug, "");
t.is(data.page.filePathStem, "/index");
});
test("getRenderedData() has good slug", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/includer.liquid", "./test/stubs/", "./dist");
let data = await getRenderedData(tmpl);
t.is(data.page.fileSlug, "includer");
t.is(data.page.filePathStem, "/includer");
});
test("Override base templating engine from .liquid to njk", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-njk.liquid",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "My Title");
});
test("Override base templating engine from markdown to 11ty.js, then markdown", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/test-override-js-markdown.11ty.cjs",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h1>This is markdown</h1>");
});
test("Override base templating engine from .liquid to md", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-md.liquid",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h1>My Title</h1>");
});
test("Override base templating engine from .liquid to njk,md", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-multiple.md",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h1>My Title</h1>");
});
test("Override base templating engine from .njk to liquid,md", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-multiple2.njk",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h1>My Title</h1>");
});
test("Override base templating engine from .html to njk", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/overrides/test.html", "./test/stubs/", "./dist");
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h2>My Title</h2>");
});
test("Override base templating engine from .html to (nothing)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-empty.html",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "<h2>{{ title }}</h2>");
});
test("Override base templating engine should error with bad string", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-error.njk",
"./test/stubs/",
"./dist"
);
await t.throwsAsync(async () => {
await renderTemplate(tmpl, await tmpl.getData());
});
});
test("Override base templating engine (bypasses markdown)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-bypass.md",
"./test/stubs/",
"./dist"
);
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "# My Title");
});
test("Override base templating engine to (nothing)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/test-empty.md",
"./test/stubs/",
"./dist"
);
// not parsed
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), "# {{ title }}");
});
test("Override base templating engine from .njk to liquid (with a layout that uses njk)", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/overrides/layout.njk", "./test/stubs/", "./dist");
t.is((await renderTemplate(tmpl, await tmpl.getData())).trim(), `<div id="layoutvalue"><h2>8</h2></div>`);
});
test("Override base templating engine from .njk to nothing (with a layout that uses njk)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/overrides/layoutfalse.njk",
"./test/stubs/",
"./dist"
);
t.is(
(await renderTemplate(tmpl, await tmpl.getData())).trim(),
`<div id="layoutvalue"><h2><%= title %></h2></div>`
);
});
test("Using a markdown source file (with a layout that uses njk), markdown shouldn’t render in layout file", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/overrides/test.md", "./test/stubs/", "./dist");
t.is(
normalizeNewLines((await renderTemplate(tmpl, await tmpl.getData())).trim()),
`# Layout header
<div id="layoutvalue"><h1>My Title</h1>
</div>`
);
});
test("renderDirect on a markdown file, permalink should not render markdown", async (t) => {
let tmpl = await getNewTemplate("./test/stubs/permalink-markdown.md", "./test/stubs/", "./dist");
let data = await tmpl.getData();
t.is(
await tmpl.renderDirect("/news/my-test-file/index.html", {}, true),
"/news/my-test-file/index.html"
);
t.is(await tmpl.getRawOutputPath(data), "/news/my-test-file/index.html");
});
test("renderDirect on a markdown file, permalink should not render markdown (with variable)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/permalink-markdown-var.md",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(
await tmpl.renderDirect("/news/{{ slug }}/index.html", { slug: "my-title" }, true),
"/news/my-title/index.html"
);
t.is(await tmpl.getRawOutputPath(data), "/news/my-title/index.html");
});
test("renderDirect on a markdown file, permalink should not render markdown (has override)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/permalink-markdown-override.md",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getData();
t.is(
await tmpl.renderDirect("/news/my-test-file/index.html", {}, true),
"/news/my-test-file/index.html"
);
t.is(await tmpl.getRawOutputPath(data), "/news/my-test-file/index.html");
});
/* Transforms */
test("Test a transform", async (t) => {
t.plan(2);
let tmpl = await getNewTemplate(
"./test/stubs/template.liquid",
"./test/stubs/",
"./test/stubs/_site"
);
tmpl.setTransforms({
transformName: function (content, outputPath) {
t.true(outputPath.endsWith(".html"));
return "OVERRIDE BY A TRANSFORM";
}
});
let renders = await _testCompleteRender(tmpl);
t.is(renders[0], "OVERRIDE BY A TRANSFORM");
});
// #789: https://github.com/11ty/eleventy/issues/789
test("Test a transform (does it have this.inputPath?)", async (t) => {
t.plan(3);
let tmpl = await getNewTemplate(
"./test/stubs/template.liquid",
"./test/stubs/",
"./test/stubs/_site"
);
tmpl.setTransforms({
transformName: function (content, outputPath) {
t.true(outputPath.endsWith(".html"));
t.true(!!this.inputPath);
return "OVERRIDE BY A TRANSFORM";
}
});
let renders = await _testCompleteRender(tmpl);
t.is(renders[0], "OVERRIDE BY A TRANSFORM");
});
test("Test a transform with pages", async (t) => {
t.plan(5);
let tmpl = await getNewTemplate(
"./test/stubs/transform-pages/template.njk",
"./test/stubs/",
"./test/stubs/_site"
);
tmpl.setTransforms({
transformName: function (content, outputPath) {
// should run twice, one for each page
t.true(content.length > 0);
t.true(outputPath.endsWith(".html"));
return "OVERRIDE BY A TRANSFORM";
}
});
let renders = await _testCompleteRender(tmpl);
t.is(renders[0], "OVERRIDE BY A TRANSFORM");
});
test("Test a transform with a layout", async (t) => {
t.plan(3);
let tmpl = await getNewTemplate(
"./test/stubs-475/transform-layout/transform-layout.njk",
"./test/stubs-475/",
"./test/stubs-475/_site"
);
tmpl.setTransforms({
transformName: function (content, outputPath) {
t.is(content, "<html><body>This is content.</body></html>");
t.true(outputPath.endsWith(".html"));
return "OVERRIDE BY A TRANSFORM";
}