forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.js
More file actions
58 lines (52 loc) · 1.83 KB
/
plot.js
File metadata and controls
58 lines (52 loc) · 1.83 KB
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
import {promises as fs} from "fs";
import * as path from "path";
import {JSDOM} from "jsdom";
import {html as beautify} from "js-beautify";
import tape from "tape-await";
import * as plots from "./plots/index.js";
(async () => {
for (const [name, plot] of Object.entries(plots)) {
tape(`plot ${name}`, async test => {
try {
// Not recommended, but this is only our test code, so should be fine?
global.document = new JSDOM("").window.document;
// Not fully functional, but only used to fetch data files, so should be fine?
global.fetch = async (href) => new Response(path.resolve("./test", href));
const svg = await plot();
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
const actual = beautify(svg.outerHTML, {indent_size: 2});
const outfile = path.resolve("./test/output", path.basename(name, ".js") + ".svg");
let expected;
try {
expected = await fs.readFile(outfile, "utf8");
} catch (error) {
if (error.code === "ENOENT" && process.env.CI !== "true") {
console.warn(`! generating ${outfile}`);
await fs.writeFile(outfile, actual, "utf8");
return;
} else {
throw error;
}
}
test.ok(actual === expected, `${name} must match snapshot`);
} finally {
global.document = undefined;
global.fetch = undefined;
}
});
}
})();
class Response {
constructor(href) {
this._href = href;
this.ok = true;
this.status = 200;
}
async text() {
return fs.readFile(this._href, {encoding: "utf-8"});
}
async json() {
return JSON.parse(await this.text());
}
}