-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_site.js
96 lines (76 loc) · 3.29 KB
/
test_site.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
/* global describe, it, beforeEach, afterEach */
let Site = require("../lib/site");
let { assertSame, fixturesPath, fixturesDir, expectationsPath, MockAssetManager } = require("./util");
let fs = require("fs");
let assert = require("assert");
let { promisify } = require("util");
let rimraf = promisify(require("rimraf"));
let readFile = promisify(fs.readFile);
let assetManager = new MockAssetManager(fixturesDir);
describe("site model", () => {
let { exit } = process;
beforeEach(() => {
process.exit = code => {
throw new Error(`exit ${code}`);
};
});
afterEach(() => {
process.exit = exit;
return rimraf(fixturesPath("dist"));
});
it("generates HTML files", async () => {
let config = {
source: require(fixturesPath("./pages.js")),
target: "./dist"
};
let site = new Site(config, assetManager);
await site.generate();
// XXX: inefficient and somewhat innacurate (due to potential unwanted
// files); check directory hierarchy instead
assertSame(await readFile(fixturesPath("dist/index.html"), "utf8"),
await readFile(expectationsPath("index.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/index.html"), "utf8"),
await readFile(expectationsPath("atoms/index.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/index.html"), "utf8"),
await readFile(expectationsPath("atoms/index.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/button/index.html"), "utf8"),
await readFile(expectationsPath("atoms/button/index.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/button/0.html"), "utf8"),
await readFile(expectationsPath("atoms/button/0.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/strong/index.html"), "utf8"),
await readFile(expectationsPath("atoms/strong/index.html"), "utf8"));
assertSame(await readFile(fixturesPath("dist/atoms/strong/0.html"), "utf8"),
await readFile(expectationsPath("atoms/strong/0.html"), "utf8"));
});
it("enforces essential configuration", async () => {
let config = {};
let fn = () => new Site(config, assetManager);
assert.throws(fn, /exit 1/); // aborts due to missing `source` configuration
config.source = require(fixturesPath("./pages.js"));
assert.throws(fn, /exit 1/); // aborts due to missing `target` configuration
config.target = "dist";
assert.throws(fn, /exit 1/); // aborts due to non-relative `target` configuration
config.target = "./dist";
fn(); // does not abort
});
it("generates URIs", async () => {
let config = {
source: require(fixturesPath("./pages.js")),
target: "./dist"
};
let site = new Site(config, assetManager);
assertSame(site.pageURI("atoms/button"), "/atoms/button.html");
assertSame(site.assetURI("bundle.css"), "/assets/bundle.css");
config.baseURI = "/path/to";
site = new Site(config, assetManager);
assertSame(site.pageURI("atoms/button"), "/path/to/atoms/button.html");
assertSame(site.assetURI("styles/bundle.css"),
"/path/to/assets/styles/bundle.css");
config.baseURI += "/"; // trailing slash
config.assetPath = "/static/";
site = new Site(config, assetManager);
assertSame(site.pageURI("atoms/button"), "/path/to/atoms/button.html");
assertSame(site.assetURI("scripts/bundle.js"),
"/path/to/static/scripts/bundle.js");
});
});