-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
plugins.spec.ts
72 lines (59 loc) · 1.86 KB
/
plugins.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
/* global it, expect, describe */
import { Eta } from "../src/index";
import { EtaConfig } from "../src/config";
import { AstObject } from "../src/parse";
function myPlugin() {
return {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
processAST: function (ast: Array<AstObject>, _env?: EtaConfig) {
ast.push("String to append");
return ast;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
processFnString: function (str: string, _env?: EtaConfig) {
return str.replace(/@@num@@/, "2352.3");
},
};
}
const emojiTransform = () => {
return {
processTemplate: function (str: string) {
return str.replace(":thumbsup:", "👍");
},
};
};
const capitalizeCool = () => {
return {
processTemplate: function (str: string) {
return str.replace("cool", "COOL");
},
};
};
const replaceThumbsUp = () => {
return {
processTemplate: function (str: string) {
return str.replace("👍", "✨");
},
};
};
describe("Plugins", () => {
it("Plugins function properly", () => {
const eta = new Eta({ plugins: [myPlugin()] });
const template = `<%= it.val %> <%= @@num@@ %>.`;
expect(eta.renderString(template, { val: "value" })).toEqual("value 2352.3.String to append");
});
});
describe("processTemplate plugin", () => {
it("Simple plugin works correctly", () => {
const eta = new Eta({ plugins: [emojiTransform()] });
const template = ":thumbsup:";
const res = eta.renderString(template, {});
expect(res).toEqual("👍");
});
it("Multiple chained plugins work correctly", () => {
const eta = new Eta({ plugins: [emojiTransform(), capitalizeCool(), replaceThumbsUp()] });
const template = ":thumbsup: This is a cool template";
const res = eta.renderString(template, {});
expect(res).toEqual("✨ This is a COOL template");
});
});