forked from gov-suite/governed-text-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
169 lines (156 loc) · 4.93 KB
/
mod_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
import { testingAsserts as ta } from "./deps-test.ts";
import { path } from "./deps.ts";
import * as mod from "./mod.ts";
function testFilePath(relTestFileName: string): string {
return path.join(
path.relative(
Deno.cwd(),
path.dirname(import.meta.url).substr("file://".length),
),
relTestFileName,
);
}
Deno.test(`HTML escaped string literal`, async () => {
const escaped = mod.htmlEscape`This should be <escaped>`;
ta.assertEquals(escaped, "This should be <escaped>");
});
Deno.test(`Non-HTML escaped string literal with HTML escaped placeholders`, async () => {
const escaped = "<be escaped>";
const phe = mod.htmlEscapePlaceholders
`This should be <not be escaped> ${escaped}`;
ta.assertEquals(phe, "This should be <not be escaped> <be escaped>");
});
Deno.test(`HTML tag raw`, async () => {
const div = mod.xmlTag("div");
const phe = div`This should be in a div with <no escapes>`;
ta.assertEquals(phe, "<div>This should be in a div with <no escapes></div>");
});
Deno.test(`HTML tag with results escaped`, async () => {
const div = mod.xmlTag(
"div",
{ style: "color: white" },
{ escapeResult: true },
);
const phe = div`This should be in a div with <escaped tag>`;
ta.assertEquals(
phe,
`<div style="color: white">This should be in a div with <escaped tag></div>`,
);
});
Deno.test(`invalid HTML template module: mod_test.tmpl-invalid-no-default.ts`, async () => {
const result = await mod.executeTemplateModule({
srcURL: "./mod_test.tmpl-invalid-no-default.ts",
content: {},
});
ta.assertStrictEquals(
result,
"No module.default found in ./mod_test.tmpl-invalid-no-default.ts",
);
});
Deno.test(`invalid HTML template module: mod_test.tmpl-invalid-default-type.ts`, async () => {
const result = await mod.executeTemplateModule({
srcURL: "./mod_test.tmpl-invalid-default-type.ts",
content: {},
});
ta.assertStrictEquals(
result,
"module.default is not an array or function: ./mod_test.tmpl-invalid-default-type.ts",
);
});
Deno.test(`HTML template module: mod_test.single-tmpl.ts produces mod_test.tmpl.html-output.golden`, async () => {
const content = "variable";
const result = await mod.executeTemplateModule({
srcURL: "./mod_test.single-tmpl.ts",
content: {
heading: "<title>Page Title</title>",
body:
`This is my body content, which can contain a ${content} or anything else that can go into a TypeScript template literal.`,
},
});
ta.assertStrictEquals(
result,
Deno.readTextFileSync(
testFilePath("mod_test.single-tmpl.html-output.golden"),
),
);
});
Deno.test(`HTML template module: mod_test.single-in.json produces mod_test.single-tmpl.html-output.golden`, async () => {
const result = await mod.transformJsonInput(
Deno.readTextFileSync(testFilePath("mod_test.single-in.json")),
{
allowArbitraryModule: (templateUrl) => {
return true;
},
},
);
ta.assertStrictEquals(
result,
Deno.readTextFileSync(
testFilePath("mod_test.single-tmpl.html-output.golden"),
),
);
});
Deno.test(`HTML template module: mod_test.multiple-in.json`, async () => {
const result = await mod.transformJsonInput(
Deno.readTextFileSync(testFilePath("mod_test.multiple-in.json")),
{
allowArbitraryModule: (templateUrl) => {
return true;
},
},
);
ta.assertStrictEquals(result, "Template 2: Heading Text, Body Text");
});
Deno.test(`HTML template module: mod_test-email-message-01.in.json`, async () => {
const result = await mod.transformJsonInput(
Deno.readTextFileSync(testFilePath("mod_test-email-message-01.in.json")),
{
allowArbitraryModule: (templateUrl) => {
return true;
},
},
);
ta.assertStrictEquals(
result,
Deno.readTextFileSync(
testFilePath("mod_test-email-message-01.html-output.golden"),
),
);
});
Deno.test(`HTML template module: mod_test-email-message-01.in-name.json`, async () => {
const result = await mod.transformJsonInput(
Deno.readTextFileSync(
testFilePath("mod_test-email-message-01.in-name.json"),
),
{
namedTemplateModuleURL: (name: string): string | undefined => {
if (name == "lookupUrlForName") {
return "./mod_test-html-email-messages.tmpl.ts";
}
return undefined;
},
},
);
ta.assertStrictEquals(
result,
Deno.readTextFileSync(
testFilePath("mod_test-email-message-01.html-output.golden"),
),
);
});
Deno.test(`HTML template module: mod_test-email-message-01.in-default.json`, async () => {
const result = await mod.transformJsonInput(
Deno.readTextFileSync(
testFilePath("mod_test-email-message-01.in-default.json"),
),
{
defaultTemplateModuleURL: (): string => {
return "./template-module-debug.ts";
},
},
);
ta.assertStrictEquals(
result,
`{"authnUrl":"https://www.medigy.com/x/reset-password"}`,
);
});