deno.land / x / eta@v3.4.0 / test / config.spec.ts

config.spec.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
/* global it, expect, describe */
import { Eta } from "../src/index";
describe("Config Tests", () => { it("custom tags", () => { const eta = new Eta({ tags: ["<<", ">>"] }); const res = eta.renderString("hi <<= it.name >>", { name: "Ben" }); expect(res).toEqual("hi Ben"); });
it("no autoescape", () => { const eta = new Eta({ autoEscape: false }); const res = eta.renderString("<%= it.html %>", { html: "<p>Hi</p>" }); expect(res).toEqual("<p>Hi</p>"); // not escaped });
it("default filter function stringifies data", () => { const eta = new Eta();
expect(eta.config.filterFunction({ a: 1 })).toEqual("[object Object]"); });
it("filter function", () => { const template = "My favorite food is <%= it.fav %>"; const baseEta = new Eta();
expect(baseEta.renderString(template, {})).toEqual("My favorite food is undefined");
const etaWithSimpleFilter = new Eta({ autoFilter: true, // turn every value into "apples" filterFunction: (_val) => "apples", });
expect(etaWithSimpleFilter.renderString(template, {})).toEqual("My favorite food is apples"); });
it("complex filter function", () => { let timesFilterCalled = 0; const eta = new Eta({ autoFilter: true, filterFunction: function () { timesFilterCalled++; if (timesFilterCalled <= 1) { return "The first"; } else { return "another"; } }, });
expect(eta.renderString("<%= it.val1 %>, <%~ it.val2 %>, <%~ it.val3 %>", {})).toEqual( "The first, another, another" ); });
it("withConfig", () => { const eta = new Eta();
const res = eta .withConfig({ tags: ["{{", "}}"] }) .renderString("{{= it.name }}", { name: "John Appleseed" });
expect(res).toEqual("John Appleseed");
// the original tags should remain unchanged expect(eta.config.tags).toEqual(["<%", "%>"]); });
it("configure", () => { const eta = new Eta();
eta.configure({ tags: ["{{", "}}"] });
const res = eta.renderString("{{= it.name }}", { name: "John Appleseed" });
expect(res).toEqual("John Appleseed");
// the original tags should have changed expect(eta.config.tags).toEqual(["{{", "}}"]); });});
eta

Version Info

Tagged at
2 months ago