-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.test.js
171 lines (153 loc) · 4.77 KB
/
build.test.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
const crypto = require("crypto");
const path = require("path");
const assert = require("assert");
const fse = require("fs-extra");
const webpack = require("webpack");
const {
Runtime,
createTemplateLoader,
createScriptResolver
} = require("@adobe/htlengine");
async function createTestRoot() {
const dir = path.resolve(
__dirname,
"tmp",
crypto.randomBytes(16).toString("hex")
);
await fse.ensureDir(dir);
return dir;
}
async function compile(dist, fixture, options = {}) {
const compiler = webpack({
context: path.resolve(__dirname, "fixtures", fixture),
entry: `./entry.js`,
mode: "development",
output: {
path: dist,
filename: "bundle.js",
libraryTarget: "commonjs2"
},
module: {
rules: [
{
test: /\.htl$/,
use: {
loader: path.resolve(__dirname, "../index.js"),
options
}
}
]
}
});
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) reject(err);
if (stats.hasErrors()) reject(new Error(stats.toJson().errors));
resolve(stats);
});
});
}
describe("Build Tests", () => {
let testRoot;
beforeEach(async () => {
testRoot = await createTestRoot();
});
afterEach(async () => {
fse.remove(testRoot);
});
it("Compiles and evaluates simple htl with data.", async () => {
await compile(testRoot, "simple", {
data: {
title: "Hello"
}
});
const html = require(path.resolve(testRoot, "bundle.js")).default;
assert.equal(html, "<h1>Hello</h1>");
});
it("Compiles and evaluates simple htl with data in query.", async () => {
await compile(testRoot, "simple_with_query");
const html = require(path.resolve(testRoot, "bundle.js")).default;
assert.equal(html, "<h1>Hello, world.</h1>");
});
it("Compiles and evaluates htl using templates.", async () => {
// use special script resolver that also respects 2nd project root
const resolver = createScriptResolver([
path.resolve(__dirname, "fixtures", "secondary_project"),
path.resolve(__dirname, "fixtures")
]);
await compile(testRoot, "templates", {
globalName: "properties",
scriptResolver: resolver,
data: {
title: "Hello",
text: "Footer Text"
}
});
const html = require(path.resolve(testRoot, "bundle.js")).default;
assert.equal(html.trim(), "<h1>Hello</h1>\n\n\n <p>Footer Text</p>");
});
it("Compiles and evaluates htl using custom template loader.", async () => {
// use special script resolver that also respects 2nd project root
const resolver = createScriptResolver([
path.resolve(__dirname, "fixtures", "secondary_project"),
path.resolve(__dirname, "fixtures")
]);
const loader = createTemplateLoader();
await compile(testRoot, "templates", {
globalName: "properties",
scriptResolver: resolver,
templateLoader: loader,
data: {
title: "Hello",
text: "Footer Text"
}
});
const html = require(path.resolve(testRoot, "bundle.js")).default;
assert.equal(html.trim(), "<h1>Hello</h1>\n\n\n <p>Footer Text</p>");
});
it("Allows to exclude runtime", async () => {
await compile(testRoot, "simple", {
includeRuntime: false
});
const template = require(path.resolve(testRoot, "bundle.js")).default;
const runtime = new Runtime().setGlobal({
title: "Hello"
});
const html = await template(runtime);
assert.equal(html, "<h1>Hello</h1>");
});
it("Supports runtime vars", async () => {
await compile(testRoot, "runtimevars", {
includeRuntime: false,
runtimeVars: ["wcmmode"]
});
const template = require(path.resolve(testRoot, "bundle.js")).default;
const runtime = new Runtime().setGlobal({
title: "Hello",
wcmmode: {
edit: true
}
});
const html = await template(runtime);
assert.equal(html.trim(), "<h1>Hello</h1>\n<div>click here to edit</div>");
});
it("Can set custom module import generator", async () => {
await compile(testRoot, "useclasses", {
includeRuntime: false,
// very simple import generator. a better usecase would be to defer loading the module with
// custom function, e.g. one that automatically injects some data into the use-class.
moduleImportGenerator: (baseDir, varName, id) => {
return `const ${varName} = require('./${id}');`;
}
});
const template = require(path.resolve(testRoot, "bundle.js")).default;
const runtime = new Runtime().setGlobal({
properties: {
title: "Jupiter",
radius: 3000
}
});
const html = await template(runtime);
assert.equal(html.trim(), "<h1>Jupiter</h1>\n Surface Area: 113097336");
});
});