-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
86 lines (86 loc) · 2.62 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
</head>
<body>
<script type="module">
import Template from "./template.js";
class Test {
constructor(name, fn) {
this.name = name;
this.fn = fn;
}
async run() {
await this.fn();
}
}
const html = Template.createElement("<div><h2>Hello Template</h2></div>");
const css = Template.createStyle("<style></style>");
const tests = [
new Test("mounts to page", async () => {
class Mountable extends Template {
constructor() {
super(html, css);
}
}
const instance = new Mountable();
instance.mount(document.body);
if (
document.body.shadowRoot.querySelector("h2") !==
instance.getElement("h2")
) {
throw new Error("Unable to find mounted component");
}
instance.unmount();
}),
new Test("emitters work", async () => {
const t = new Template();
const one = new Promise((resolve, reject) => {
t.on("foo", resolve);
setTimeout(reject, 100);
});
const two = new Promise((resolve, reject) => {
t.on("foo", resolve);
setTimeout(reject, 100);
});
const three = new Promise((resolve, reject) => {
t.on("bar", resolve);
setTimeout(reject, 100);
});
t.emit("foo");
t.emit("bar");
await Promise.all([one, two, three]);
}),
new Test("setState emits change on change", async () => {
const t = new Template();
await new Promise((resolve, reject) => {
t.on("change", resolve);
setTimeout(reject, 100);
t.setState({ foo: "bar" });
});
}),
new Test("setState does not emit change on same", async () => {
const t = new Template();
t.setState({ foo: "bar" });
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve, reject) => {
t.on("change", reject);
t.setState({ foo: "bar" });
setTimeout(resolve, 100);
});
}),
];
tests.forEach((test) =>
(async function runTest() {
try {
await test.run();
console.log(`PASS: ${test.name}`);
} catch (e) {
console.error(`FAIL: ${test.name}`, e);
}
})()
);
</script>
</body>
</html>