-
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
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
/// <reference lib="dom" />
import { GlobalRegistrator } from "@happy-dom/global-registrator";
import { afterAll, beforeAll, describe, expect, mock, test } from "bun:test";
import { CustomEvent } from "happy-dom";
import { el, attr, fragment, mathml, on, prop, svg } from "../src/dom.js";
import { derived, signal } from "../src/signals.js";
beforeAll(() => {
GlobalRegistrator.register();
});
afterAll(() => {
GlobalRegistrator.unregister();
});
describe("el", () => {
test("should create an HTMLElement element", () => {
const p = el("p");
expect(p.tagName).toBe("P");
});
test("should run ElementSetups against the to-be created element", () => {
const fn = mock((_n: number) => {});
el("div", [
() => {
fn(0);
},
() => {
fn(1);
},
]);
expect(fn.mock.calls).toEqual([[0], [1]]);
});
test("should append child nodes", () => {
const div = el("div", [], [el("p")]);
const p = div.querySelector("p");
expect(p).toBeTruthy();
});
test("should convert strings to Text", () => {
const p = el("p", [], ["Foo"]);
expect(p.textContent).toBe("Foo");
});
test("should skip null or undefined", () => {
const p = el("p", [], [el("span"), null, undefined, el("span")]);
expect(p.childNodes.length).toBe(2);
});
test("should react to Signal value changes", () => {
const span = el("span");
const $child = signal<HTMLElement | null>(span);
const p = el("p", [], [$child]);
expect(p.querySelector("span")).toBe(span);
$child.set(null);
expect(p.querySelector("span")).toBe(null);
$child.set(span);
expect(p.querySelector("span")).toBe(span);
});
});
describe("attr", () => {
test("should set attribute", () => {
const div = el("div", [attr("aria-hidden", "true")]);
expect(div.getAttribute("aria-hidden")).toBe("true");
});
test("should react to change of Signal values", () => {
const $s = signal("foo");
const button = el("button", [attr("data-name", $s)]);
expect(button.dataset.name).toBe("foo");
$s.set("bar");
expect(button.dataset.name).toBe("bar");
});
test("should handle boolean value", () => {
const $s = signal(true);
const input = el("input", [attr("disabled", $s)]);
expect(input.getAttribute("disabled")).toBe("");
$s.set(false);
expect(input.getAttribute("disabled")).toBe(null);
});
});
describe("prop", () => {
test("should set property", () => {
const input = el("input", [prop("readOnly", true)]);
expect(input.readOnly).toBe(true);
});
test("should react to Signal value changes", () => {
const $disabled = signal(false);
const button = el("button", [prop("disabled", $disabled)]);
expect(button.disabled).toBe(false);
$disabled.set(true);
expect(button.disabled).toBe(true);
});
});
describe("on", () => {
test("should set event listener", () => {
const fn = mock(() => {});
const div = el("div", [
on("click", () => {
fn();
}),
]);
// @ts-expect-error: Using native event cause runtime error (https://github.com/capricorn86/happy-dom/issues/1049)
div.dispatchEvent(new CustomEvent("click"));
expect(fn.mock.calls.length).toBe(1);
});
test("should dispose stale listener", () => {
const fn = mock(() => {});
const $s = signal(0);
const div = el("div", [
on(
"click",
derived(() => {
// Associate this Derived to $s, so every time $s gets updated,
// listener function will be renewed too.
$s.get();
return () => {
fn();
};
}),
),
]);
$s.set(1);
// @ts-expect-error: Using native event cause runtime error (https://github.com/capricorn86/happy-dom/issues/1049)
div.dispatchEvent(new CustomEvent("click"));
expect(fn.mock.calls.length).toBe(1);
});
});
describe("fragment", () => {
test("children update mechanism correctly handles DocumentFragment quirk", () => {
const $visible = signal(true);
const div = el(
"div",
[],
[
derived(() =>
$visible.get() ? fragment([el("span"), el("span")]) : null,
),
],
);
expect(div.querySelectorAll("> span").length).toBe(2);
$visible.set(false);
expect(div.querySelectorAll("> span").length).toBe(0);
$visible.set(true);
expect(div.querySelectorAll("> span").length).toBe(2);
});
test("should react to value changes", () => {
const $name = signal("Alice");
const div = el("p", [], [fragment(["Hello, ", $name, "!"])]);
expect(div.textContent).toBe("Hello, Alice!");
$name.set("Bob");
expect(div.textContent).toBe("Hello, Bob!");
});
});
describe("svg", () => {
test("should create a SVGElement", () => {
const path = svg("path", [attr("d", "M0,0 L1,0 L0,1 Z")]);
// NOTE: happy-dom does not implement SVGPathElement
expect(path).toBeInstanceOf(SVGElement);
});
});
describe("mathml", () => {
test("should create a MathMLElement", () => {
const fomula = mathml(
"math",
[attr("display", "inline")],
[
mathml(
"mfrac",
[],
[
mathml(
"msup",
[],
[mathml("mi", [], ["π"]), mathml("mn", [], ["2"])],
),
mathml("mn", [], ["6"]),
],
),
],
);
// NOTE: happy-dom does not implement MathML at all.
expect(fomula).toBeInstanceOf(Element);
expect(
(fomula as Element).querySelector("> mfrac > msup > mn")?.textContent,
).toBe("2");
});
});