-
Notifications
You must be signed in to change notification settings - Fork 67
/
express-handlebars.test.ts
806 lines (726 loc) · 29.5 KB
/
express-handlebars.test.ts
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
import * as path from "node:path";
import * as expressHandlebars from "../lib/index";
import type {
TemplateDelegateObject,
EngineOptions,
} from "../types";
function fixturePath (filePath = "") {
return path.resolve(__dirname, "./fixtures", filePath);
}
// allow access to private functions for testing
// https://github.com/microsoft/TypeScript/issues/19335
/* eslint-disable dot-notation, @typescript-eslint/no-empty-function */
describe("express-handlebars", () => {
test("ExpressHandlebars instance", () => {
const exphbs = new expressHandlebars.ExpressHandlebars();
expect(exphbs).toBeDefined();
});
test("should nomalize extname", () => {
const exphbs1 = expressHandlebars.create({ extname: "ext" });
const exphbs2 = expressHandlebars.create({ extname: ".ext" });
expect(exphbs1.extname).toBe(".ext");
expect(exphbs2.extname).toBe(".ext");
});
describe("getPartials", () => {
test("should throw if partialsDir is not correct type", async () => {
// @ts-expect-error partialsDir is invalid
const exphbs = expressHandlebars.create({ partialsDir: 1 });
let error: Error | undefined;
try {
await exphbs.getPartials();
} catch (e) {
error = e;
}
expect(error).toEqual(expect.any(Error));
expect(error?.message).toBe("A partials dir must be a string or config object");
});
test("should return empty object if no partialsDir is defined", async () => {
const exphbs = expressHandlebars.create();
const partials = await exphbs.getPartials();
expect(partials).toEqual({});
});
test("should return empty object partialsDir does not exist", async () => {
const exphbs = expressHandlebars.create({ partialsDir: "does-not-exist" });
const partials = await exphbs.getPartials();
expect(partials).toEqual({});
});
test("should return partials on string", async () => {
const exphbs = expressHandlebars.create({ partialsDir: fixturePath("partials") });
const partials = await exphbs.getPartials();
expect(partials).toEqual({
partial: expect.any(Function),
"partial-latin1": expect.any(Function),
"subdir/partial-subdir": expect.any(Function),
});
});
test("should return partials on array", async () => {
const exphbs = expressHandlebars.create({ partialsDir: [fixturePath("partials")] });
const partials = await exphbs.getPartials();
expect(partials).toEqual({
partial: expect.any(Function),
"partial-latin1": expect.any(Function),
"subdir/partial-subdir": expect.any(Function),
});
});
test("should return partials on object", async () => {
const fn = jest.fn();
const exphbs = expressHandlebars.create({
partialsDir: {
templates: { "partial template": fn },
namespace: "partial namespace",
dir: fixturePath("partials"),
},
});
const partials = await exphbs.getPartials();
expect(partials).toEqual({
"partial namespace/partial template": fn,
});
});
test("should return renamed partials with rename function", async () => {
const fn = jest.fn();
const exphbs = expressHandlebars.create({
partialsDir: {
templates: { "partial/template": fn },
namespace: "partial namespace",
dir: fixturePath("partials"),
rename: (filePath, namespace) => {
return `${namespace}/${filePath.split("/")[0]}`;
},
},
});
const partials = await exphbs.getPartials();
expect(partials).toEqual({
"partial namespace/partial": fn,
});
});
test("should return partials on path relative to cwd", async () => {
const exphbs = expressHandlebars.create({ partialsDir: "spec/fixtures/partials" });
const partials = await exphbs.getPartials();
expect(partials).toEqual({
partial: expect.any(Function),
"partial-latin1": expect.any(Function),
"subdir/partial-subdir": expect.any(Function),
});
});
test("should return template function", async () => {
const exphbs = expressHandlebars.create({ partialsDir: "spec/fixtures/partials" });
const partials = await exphbs.getPartials() as TemplateDelegateObject;
const html = partials.partial({ text: "test text" });
expect(html).toBe("partial test text");
});
test("should return a template with encoding", async () => {
const exphbs = expressHandlebars.create({ partialsDir: "spec/fixtures/partials" });
const partials = await exphbs.getPartials({ encoding: "latin1" }) as TemplateDelegateObject;
const html = partials["partial-latin1"]({});
expect(html).toContain("ñáéíóú");
});
test("should return a template with default encoding", async () => {
const exphbs = expressHandlebars.create({
encoding: "latin1",
partialsDir: "spec/fixtures/partials",
});
const partials = await exphbs.getPartials() as TemplateDelegateObject;
const html = partials["partial-latin1"]({});
expect(html).toContain("ñáéíóú");
});
});
describe("getTemplate", () => {
test("should return cached template", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template.handlebars");
const compiledCachedFunction = (() => "compiled") as Handlebars.TemplateDelegate;
exphbs.compiled[filePath] = Promise.resolve(compiledCachedFunction);
const precompiledCachedFunction = (() => "precompiled") as TemplateSpecification;
exphbs.precompiled[filePath] = Promise.resolve(precompiledCachedFunction);
const template = await exphbs.getTemplate(filePath, { cache: true });
expect(template).toBe(compiledCachedFunction);
});
test("should return precompiled cached template", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template.handlebars");
const compiledCachedFunction = (() => "compiled") as Handlebars.TemplateDelegate;
exphbs.compiled[filePath] = Promise.resolve(compiledCachedFunction);
const precompiledCachedFunction = (() => "precompiled") as TemplateSpecification;
exphbs.precompiled[filePath] = Promise.resolve(precompiledCachedFunction);
const template = await exphbs.getTemplate(filePath, { precompiled: true, cache: true });
expect(template).toBe(precompiledCachedFunction);
});
test("should store in precompiled cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template.handlebars");
expect(exphbs.compiled[filePath]).toBeUndefined();
expect(exphbs.precompiled[filePath]).toBeUndefined();
await exphbs.getTemplate(filePath, { precompiled: true });
expect(exphbs.compiled[filePath]).toBeUndefined();
expect(exphbs.precompiled[filePath]).toBeDefined();
});
test("should store in compiled cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template.handlebars");
expect(exphbs.compiled[filePath]).toBeUndefined();
expect(exphbs.precompiled[filePath]).toBeUndefined();
await exphbs.getTemplate(filePath);
expect(exphbs.compiled[filePath]).toBeDefined();
expect(exphbs.precompiled[filePath]).toBeUndefined();
});
test("should return a template", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template.handlebars");
const template = await exphbs.getTemplate(filePath) as HandlebarsTemplateDelegate;
const html = template({ text: "test text" });
expect(html).toBe("<p>test text</p>");
});
test("should return a template with encoding", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates/template-latin1.handlebars");
const template = await exphbs.getTemplate(filePath, { encoding: "latin1" }) as HandlebarsTemplateDelegate;
const html = template({});
expect(html).toContain("ñáéíóú");
});
test("should return a template with default encoding", async () => {
const exphbs = expressHandlebars.create({ encoding: "latin1" });
const filePath = fixturePath("templates/template-latin1.handlebars");
const template = await exphbs.getTemplate(filePath) as HandlebarsTemplateDelegate;
const html = template({});
expect(html).toContain("ñáéíóú");
});
test("should not store in cache on error", async () => {
const exphbs = expressHandlebars.create();
const filePath = "does-not-exist";
expect(exphbs.compiled[filePath]).toBeUndefined();
let error: Error | undefined;
try {
await exphbs.getTemplate(filePath);
} catch (e) {
error = e;
}
expect(error?.message).toEqual(expect.stringContaining("no such file or directory"));
expect(exphbs.compiled[filePath]).toBeUndefined();
});
});
describe("getTemplates", () => {
test("should return cached templates", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const fsCache = Promise.resolve([]);
exphbs._fsCache[dirPath] = fsCache;
const templates = await exphbs.getTemplates(dirPath, { cache: true });
expect(templates).toEqual({});
});
test("should return templates", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const templates = await exphbs.getTemplates(dirPath);
const html = templates["template.handlebars"]({ text: "test text" });
expect(html).toBe("<p>test text</p>");
});
test("should get templates in sub directories", async () => {
const exphbs = expressHandlebars.create();
const dirPath = fixturePath("templates");
const templates = await exphbs.getTemplates(dirPath);
const paths = Object.keys(templates);
expect(paths).toEqual([
"template.handlebars",
"template-latin1.handlebars",
"subdir/template.handlebars",
]);
});
});
describe("render", () => {
test("should return cached templates", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("render-cached.handlebars");
exphbs.compiled[filePath] = Promise.resolve(() => "cached");
const html = await exphbs.render(filePath, undefined, { cache: true });
expect(html).toBe("cached");
});
test("should use helpers", async () => {
const exphbs = expressHandlebars.create({
helpers: {
help: () => "help",
},
});
const filePath = fixturePath("render-helper.handlebars");
const html = await exphbs.render(filePath, { text: "test text" });
expect(html).toBe("<p>help</p>");
});
test("should override helpers", async () => {
const exphbs = expressHandlebars.create({
helpers: {
help: () => "help",
},
});
const filePath = fixturePath("render-helper.handlebars");
const html = await exphbs.render(filePath, { text: "test text" }, {
helpers: {
help: (text: string) => text,
},
});
expect(html).toBe("<p>test text</p>");
});
test("should return html", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("render-text.handlebars");
const html = await exphbs.render(filePath, { text: "test text" });
expect(html).toBe("<p>test text</p>");
});
test("should return html with encoding", async () => {
const exphbs = expressHandlebars.create({
partialsDir: fixturePath("partials"),
});
const filePath = fixturePath("render-latin1.handlebars");
const html = await exphbs.render(filePath, undefined, { encoding: "latin1" });
expect(html).toContain("partial ñáéíóú");
expect(html).toContain("render ñáéíóú");
});
test("should return html with default encoding", async () => {
const exphbs = expressHandlebars.create({
encoding: "latin1",
partialsDir: fixturePath("partials"),
});
const filePath = fixturePath("render-latin1.handlebars");
const html = await exphbs.render(filePath);
expect(html).toContain("partial ñáéíóú");
expect(html).toContain("render ñáéíóú");
});
test("should render with partial", async () => {
const exphbs = expressHandlebars.create({
partialsDir: fixturePath("partials"),
});
const filePath = fixturePath("render-partial.handlebars");
const html = await exphbs.render(filePath, { text: "test text" });
expect(html.replace(/\r/g, "")).toBe("<h1>partial test text</h1>\n<p>test text</p>");
});
test("should render with subdir/partial", async () => {
const exphbs = expressHandlebars.create({
partialsDir: fixturePath("partials"),
});
const filePath = fixturePath("render-subdir-partial.handlebars");
const html = await exphbs.render(filePath, { text: "test text" });
expect(html.replace(/\r/g, "")).toBe("<h1>subdir partial test text</h1>\n<p>test text</p>");
});
test("should render with runtimeOptions", async () => {
const exphbs = expressHandlebars.create({
runtimeOptions: { allowProtoPropertiesByDefault: true },
});
const filePath = fixturePath("test");
const spy = jest.fn(() => { return "test"; }) as Handlebars.TemplateDelegate;
exphbs.compiled[filePath] = Promise.resolve(spy);
await exphbs.render(filePath, undefined, { cache: true });
expect(spy).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({ allowProtoPropertiesByDefault: true }));
});
test("should override runtimeOptions", async () => {
const exphbs = expressHandlebars.create({
runtimeOptions: { allowProtoPropertiesByDefault: true },
});
const filePath = fixturePath("test");
const spy = jest.fn(() => { return "test"; }) as Handlebars.TemplateDelegate;
exphbs.compiled[filePath] = Promise.resolve(spy);
await exphbs.render(filePath, undefined, {
cache: true,
runtimeOptions: { allowProtoPropertiesByDefault: false },
});
expect(spy).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({ allowProtoPropertiesByDefault: false }));
});
});
describe("engine", () => {
test("should call renderView", async () => {
jest.spyOn(expressHandlebars.ExpressHandlebars.prototype, "renderView").mockImplementation(() => Promise.resolve(null));
const exphbs = expressHandlebars.create();
const cb = () => { /* empty */ };
exphbs.engine("view", {}, cb);
expect(expressHandlebars.ExpressHandlebars.prototype.renderView).toHaveBeenCalledWith("view", {}, cb);
});
test("should call engine", async () => {
jest.spyOn(expressHandlebars.ExpressHandlebars.prototype, "renderView").mockImplementation(() => Promise.resolve(null));
const cb = () => { /* empty */ };
expressHandlebars.engine()("view", {}, cb);
expect(expressHandlebars.ExpressHandlebars.prototype.renderView).toHaveBeenCalledWith("view", {}, cb);
});
test("should render html", async () => {
const renderView = expressHandlebars.engine({ defaultLayout: undefined });
const viewPath = fixturePath("render-text.handlebars");
const html = await renderView(viewPath, { text: "test text" } as EngineOptions);
expect(html).toBe("<p>test text</p>");
});
});
describe("renderView", () => {
test("should use settings.views", async () => {
const exphbs = expressHandlebars.create();
const viewPath = fixturePath("render-partial.handlebars");
const viewsPath = fixturePath();
const html = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: viewsPath },
});
expect(html.replace(/\r/g, "")).toBe("<body>\n<h1>partial test text</h1>\n<p>test text</p>\n</body>");
});
test("should use settings.views array", async () => {
const exphbs = expressHandlebars.create();
const viewPath = fixturePath("render-partial.handlebars");
const viewsPath = fixturePath();
const html = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: [viewsPath] },
});
expect(html.replace(/\r/g, "")).toBe("<body>\n<h1>partial test text</h1>\n<p>test text</p>\n</body>");
});
test("should not use settings.views array when no parent found", async () => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = fixturePath("render-text.handlebars");
const viewsPath = "does-not-exist";
const html = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: [viewsPath] },
});
expect(html).toBe("<p>test text</p>");
});
test("should use settings.views when it changes", async () => {
const exphbs = expressHandlebars.create();
const viewPath = fixturePath("render-partial.handlebars");
const viewsPath = fixturePath();
const html = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: viewsPath },
});
expect(html.replace(/\r/g, "")).toBe("<body>\n<h1>partial test text</h1>\n<p>test text</p>\n</body>");
const otherViewsPath = fixturePath("other-views");
const otherhtml = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: otherViewsPath },
});
expect(otherhtml.replace(/\r/g, "")).toBe("<body>\nother layout\n<h1>other partial test text</h1>\n<p>test text</p>\n</body>");
});
test("should not overwrite config with settings.views", async () => {
const exphbs = expressHandlebars.create({
layoutsDir: fixturePath("layouts"),
partialsDir: fixturePath("partials"),
});
const viewPath = fixturePath("render-partial.handlebars");
const viewsPath = fixturePath("other-views");
const html = await exphbs.renderView(viewPath, {
text: "test text",
settings: { views: viewsPath },
});
expect(html.replace(/\r/g, "")).toBe("<body>\n<h1>partial test text</h1>\n<p>test text</p>\n</body>");
});
test("should merge helpers", async () => {
const exphbs = expressHandlebars.create({
defaultLayout: undefined,
helpers: {
help: () => "help",
},
});
const viewPath = fixturePath("render-helper.handlebars");
const html = await exphbs.renderView(viewPath, {
text: "test text",
helpers: {
help: (text: string) => text,
},
});
expect(html).toBe("<p>test text</p>");
});
test("should use layout option", async () => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const layoutPath = fixturePath("layouts/main.handlebars");
const viewPath = fixturePath("render-text.handlebars");
const html = await exphbs.renderView(viewPath, {
text: "test text",
layout: layoutPath,
});
expect(html.replace(/\r/g, "")).toBe("<body>\n<p>test text</p>\n</body>");
});
test("should render html", async () => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = fixturePath("render-text.handlebars");
const html = await exphbs.renderView(viewPath, { text: "test text" });
expect(html).toBe("<p>test text</p>");
});
test("should render html with encoding", async () => {
const exphbs = expressHandlebars.create({
defaultLayout: "main-latin1",
partialsDir: fixturePath("partials"),
layoutsDir: fixturePath("layouts"),
});
const viewPath = fixturePath("render-latin1.handlebars");
const html = await exphbs.renderView(viewPath, { encoding: "latin1" });
expect(html).toContain("layout ñáéíóú");
expect(html).toContain("partial ñáéíóú");
expect(html).toContain("render ñáéíóú");
});
test("should render html with default encoding", async () => {
const exphbs = expressHandlebars.create({
encoding: "latin1",
defaultLayout: "main-latin1",
partialsDir: fixturePath("partials"),
layoutsDir: fixturePath("layouts"),
});
const viewPath = fixturePath("render-latin1.handlebars");
const html = await exphbs.renderView(viewPath);
expect(html).toContain("layout ñáéíóú");
expect(html).toContain("partial ñáéíóú");
expect(html).toContain("render ñáéíóú");
});
test("should call callback with html", (done) => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = fixturePath("render-text.handlebars");
exphbs.renderView(viewPath, { text: "test text" }, (err: Error|null, html: string|undefined) => {
expect(err).toBe(null);
expect(html).toBe("<p>test text</p>");
done();
});
});
test("should call callback as second parameter", (done) => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = fixturePath("render-text.handlebars");
exphbs.renderView(viewPath, (err: Error|null, html: string|undefined) => {
expect(err).toBe(null);
expect(html).toBe("<p></p>");
done();
});
});
test("should call callback with error", (done) => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = "does-not-exist";
exphbs.renderView(viewPath, {}, (err: Error|null, html: string | undefined) => {
expect(err?.message).toEqual(expect.stringContaining("no such file or directory"));
expect(html).toBeUndefined();
done();
});
});
test("should reject with error", async () => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const viewPath = "does-not-exist";
let error: Error | undefined;
try {
await exphbs.renderView(viewPath);
} catch (e) {
error = e;
}
expect(error?.message).toEqual(expect.stringContaining("no such file or directory"));
});
test("should use runtimeOptions", async () => {
const exphbs = expressHandlebars.create({ defaultLayout: undefined });
const filePath = fixturePath("test");
const spy = jest.fn(() => { return "test"; }) as Handlebars.TemplateDelegate;
exphbs.compiled[filePath] = Promise.resolve(spy);
await exphbs.renderView(filePath, {
cache: true,
runtimeOptions: { allowProtoPropertiesByDefault: true },
});
expect(spy).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({ allowProtoPropertiesByDefault: true }));
});
});
describe("hooks", () => {
describe("_compileTemplate", () => {
test("should call template with context and options", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error empty function
jest.spyOn(exphbs.handlebars, "compile").mockImplementation(() => {});
const template = "template";
const options = {};
exphbs["_compileTemplate"](template, options);
expect(exphbs.handlebars.compile).toHaveBeenCalledWith(template, options);
});
test("should trim template", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error empty function
jest.spyOn(exphbs.handlebars, "compile").mockImplementation(() => {});
const template = " template\n";
const options = {};
exphbs["_compileTemplate"](template, options);
expect(exphbs.handlebars.compile).toHaveBeenCalledWith("template", options);
});
});
describe("_precompileTemplate", () => {
test("should call template with context and options", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error empty function
jest.spyOn(exphbs.handlebars, "precompile").mockImplementation(() => {});
const template = "template";
const options = {};
exphbs["_precompileTemplate"](template, options);
expect(exphbs.handlebars.precompile).toHaveBeenCalledWith(template, options);
});
test("should trim template", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error empty function
jest.spyOn(exphbs.handlebars, "precompile").mockImplementation(() => {});
const template = " template\n";
const options = {};
exphbs["_precompileTemplate"](template, options);
expect(exphbs.handlebars.precompile).toHaveBeenCalledWith("template", options);
});
});
describe("_renderTemplate", () => {
test("should call template with context and options", () => {
const exphbs = expressHandlebars.create();
const template = jest.fn(() => "");
const context = {};
const options = {};
exphbs["_renderTemplate"](template, context, options);
expect(template).toHaveBeenCalledWith(context, options);
});
test("should trim html", () => {
const exphbs = expressHandlebars.create();
const template = () => " \n";
const html = exphbs["_renderTemplate"](template);
expect(html).toBe("");
});
});
describe("_getDir", () => {
test("should get from cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("test");
exphbs._fsCache[filePath] = Promise.resolve(["test"]);
const file = await exphbs["_getDir"](filePath, { cache: true });
expect(file).toEqual(["test"]);
});
test("should store in cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("templates");
expect(exphbs._fsCache[filePath]).toBeUndefined();
const expected = await exphbs["_getDir"](filePath);
expect(exphbs._fsCache[filePath]).toBeInstanceOf(Promise);
expect(await exphbs._fsCache[filePath]).toEqual(expected);
});
test("should not store in cache on error", async () => {
const exphbs = expressHandlebars.create();
const filePath = "test";
expect(exphbs._fsCache[filePath]).toBeUndefined();
let error: Error | undefined;
try {
await exphbs["_getDir"](filePath, {
// @ts-expect-error Add this just for testing
_throwTestError: true,
});
} catch (e) {
error = e;
}
expect(error).toBeTruthy();
expect(exphbs._fsCache[filePath]).toBeUndefined();
});
});
describe("_getFile", () => {
test("should get from cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("test");
exphbs._fsCache[filePath] = "test";
const file = await exphbs["_getFile"](filePath, { cache: true });
expect(file).toBe("test");
});
test("should store in cache", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("render-text.handlebars");
expect(exphbs._fsCache[filePath]).toBeUndefined();
await exphbs["_getFile"](filePath);
expect(exphbs._fsCache[filePath]).toBeDefined();
});
test("should not store in cache on error", async () => {
const exphbs = expressHandlebars.create();
const filePath = "does-not-exist";
expect(exphbs._fsCache[filePath]).toBeUndefined();
let error: Error | undefined;
try {
await exphbs["_getFile"](filePath);
} catch (e) {
error = e;
}
expect(error?.message).toEqual(expect.stringContaining("no such file or directory"));
expect(exphbs._fsCache[filePath]).toBeUndefined();
});
test("should read as utf8", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("render-text.handlebars");
const text = await exphbs["_getFile"](filePath);
expect(text.trim()).toBe("<p>{{text}}</p>");
});
test("should read as utf8 by default", async () => {
const exphbs = expressHandlebars.create({ encoding: undefined });
const filePath = fixturePath("render-text.handlebars");
const text = await exphbs["_getFile"](filePath);
expect(text.trim()).toBe("<p>{{text}}</p>");
});
test("should read as latin1", async () => {
const exphbs = expressHandlebars.create();
const filePath = fixturePath("render-latin1.handlebars");
const text = await exphbs["_getFile"](filePath, { encoding: "latin1" });
expect(text).toContain("ñáéíóú");
});
test("should read as default encoding", async () => {
const exphbs = expressHandlebars.create({ encoding: "latin1" });
const filePath = fixturePath("render-latin1.handlebars");
const text = await exphbs["_getFile"](filePath);
expect(text).toContain("ñáéíóú");
});
});
describe("_getTemplateName", () => {
test("should remove extension", () => {
const exphbs = expressHandlebars.create();
const name = exphbs["_getTemplateName"]("filePath.handlebars");
expect(name).toBe("filePath");
});
test("should leave if no extension", () => {
const exphbs = expressHandlebars.create();
const name = exphbs["_getTemplateName"]("filePath");
expect(name).toBe("filePath");
});
test("should add namespace", () => {
const exphbs = expressHandlebars.create();
const name = exphbs["_getTemplateName"]("filePath.handlebars", "namespace");
expect(name).toBe("namespace/filePath");
});
});
describe("_resolveViewsPath", () => {
test("should return closest parent", () => {
const file = "/root/views/file.hbs";
const exphbs = expressHandlebars.create();
const viewsPath = exphbs["_resolveViewsPath"]([
"/root",
"/root/views",
"/root/views/file",
], file);
expect(viewsPath).toBe("/root/views");
});
test("should return string views", () => {
const exphbs = expressHandlebars.create();
const viewsPath = exphbs["_resolveViewsPath"]("./views", "filePath.hbs");
expect(viewsPath).toBe("./views");
});
test("should return null views", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error shouldn't expect null parameter
const viewsPath = exphbs["_resolveViewsPath"](null, "filePath.hbs");
expect(viewsPath).toBe(null);
});
test("should return null if not found", () => {
const file = "/file.hbs";
const exphbs = expressHandlebars.create();
const viewsPath = exphbs["_resolveViewsPath"]([
"/views",
], file);
expect(viewsPath).toBe(null);
});
});
describe("_resolveLayoutPath", () => {
test("should add extension", () => {
const exphbs = expressHandlebars.create();
const layoutPath = exphbs["_resolveLayoutPath"]("filePath");
expect(layoutPath).toEqual(expect.stringMatching(/filePath\.handlebars$/));
});
test("should use layoutsDir", () => {
const layoutsDir = fixturePath("layouts");
const filePath = "filePath.handlebars";
const exphbs = expressHandlebars.create({ layoutsDir });
const layoutPath = exphbs["_resolveLayoutPath"](filePath);
expect(layoutPath).toBe(path.resolve(layoutsDir, filePath));
});
test("should return null", () => {
const exphbs = expressHandlebars.create();
// @ts-expect-error shouldn't expect null parameter
const layoutPath = exphbs["_resolveLayoutPath"](null);
expect(layoutPath).toBe(null);
});
});
});
});