-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodule-jsdom.test.ts
96 lines (81 loc) · 2.96 KB
/
module-jsdom.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
import { ok, strictEqual } from "node:assert";
import { Server, createServer } from "node:http";
import { createReadStream, existsSync } from "node:fs";
import { isBrowserWindow } from "./env.ts";
import { importScript, importStylesheet } from "./module.ts";
import { extname, resolve as resolvePath } from "./path.ts";
import { getMIME } from "./filetype.ts";
import { until } from "./async.ts";
// Mock `window.matchMedia` for JSDOM.
globalThis.matchMedia = window.matchMedia = (query) => {
return {
matches: false,
media: query,
onchange: null,
addListener: () => { },
addEventListener: () => { },
removeListener: () => { },
removeEventListener: () => { },
dispatchEvent: () => true,
};
};
describe("module", () => {
if (!isBrowserWindow) {
return;
}
let server: Server;
let port = 12345;
before(async () => {
await new Promise<void>(resolve => {
server = createServer((req, res) => {
const pathname = req.url!;
const filename = resolvePath(pathname.slice(1));
if (existsSync(filename)) {
res.writeHead(200, { "Content-Type": getMIME(extname(filename)) });
createReadStream(filename).pipe(res);
} else {
res.writeHead(404);
res.end();
}
}).listen(port, () => {
resolve();
});
});
});
after(() => {
server.close();
});
it("importScript", async () => {
const url = `http://localhost:${port}/bundle/jsext.js`;
// Don't wait here because JSDOM doesn't actually load the script.
importScript(url);
const script = await until(
() => document.querySelector<HTMLScriptElement>(`script[data-src='${url}']`)
);
ok(script !== null);
ok(script.src.startsWith("blob:"));
strictEqual(script.type, "text/javascript");
});
it("importScript (module)", async () => {
const url = `http://localhost:${port}/examples/worker.mjs`;
// Don't wait here because JSDOM doesn't actually load the script.
importScript(url, { type: "module" });
const script = await until(
() => document.querySelector<HTMLScriptElement>(`script[data-src='${url}']`)
);
ok(script !== null);
ok(script.src.startsWith("blob:"));
strictEqual(script.type, "module");
});
it("importStylesheet", async () => {
const url = `http://localhost:${port}/examples/styles.css`;
// Don't wait here because JSDOM doesn't actually load the stylesheet.
importStylesheet(url);
const link = await until(
() => document.querySelector<HTMLLinkElement>(`link[data-src='${url}']`)
);
ok(link !== null);
ok(link.href.startsWith("blob:"));
strictEqual(link.rel, "stylesheet");
});
});