aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/viewer/tests/fakedom.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/tests/fakedom.mjs')
-rw-r--r--viewer/tests/fakedom.mjs29
1 files changed, 29 insertions, 0 deletions
diff --git a/viewer/tests/fakedom.mjs b/viewer/tests/fakedom.mjs
new file mode 100644
index 0000000..5b2fadd
--- /dev/null
+++ b/viewer/tests/fakedom.mjs
@@ -0,0 +1,29 @@
+// Minimal DOM stand-in for node tests of the panels: just what ui.el() and the panels touch.
+export class FakeEl {
+ constructor(tag) { Object.assign(this, { tag, kids: [], attrs: {}, on: {}, style: {}, value: '', hidden: false }); }
+ setAttribute(k, v) { this.attrs[k] = v; }
+ get className() { return this.attrs.class ?? ''; }
+ addEventListener(t, f) { (this.on[t] ??= []).push(f); }
+ fire(t, e = {}) { for (const f of this.on[t] ?? []) f({ preventDefault() {}, stopPropagation() {}, target: this, ...e }); }
+ append(...k) { this.kids.push(...k); }
+ replaceChildren(...k) { this.kids = k; }
+ focus() {}
+ blur() {}
+ getBoundingClientRect() { return { left: 0, top: 0, width: 320, height: 130 }; }
+ get textContent() { return this.kids.map(k => (typeof k === 'string' ? k : k.textContent)).join(''); }
+ set textContent(v) { this.kids = [String(v)]; }
+ find(pred) {
+ for (const k of this.kids) {
+ if (typeof k === 'string') continue;
+ if (pred(k)) return k;
+ const r = k.find(pred);
+ if (r) return r;
+ }
+ return null;
+ }
+}
+
+export function installFakeDom() {
+ globalThis.document = { createElement: t => new FakeEl(t), createElementNS: (_, t) => new FakeEl(t),
+ querySelector: () => null, querySelectorAll: () => [] };
+}