Fork Horse repository, listed by its owner. Publisher snapshot.

packages/forkhorse-dashboard/test/dom.ts

Snapshot at 34e3175

Containing directory

1,590 bytes · typescriptView raw
1import { createRequire } from "node:module";
2/** DOM-only fixtures. ResizeObserver is a no-layout shim; browser acceptance checks real geometry. */
3export function installDom(window: Window & typeof globalThis) {
4  for (const name of [
5    "window",
6    "document",
7    "HTMLElement",
8    "Element",
9    "Node",
10    "MutationObserver",
11    "getComputedStyle",
12    "HTMLInputElement",
13    "HTMLSelectElement",
14    "DocumentFragment",
15    "ShadowRoot",
16    "HTMLButtonElement",
17  ] as const)
18    Object.defineProperty(globalThis, name, {
19      configurable: true,
20      writable: true,
21      value: name === "window" ? window : window[name],
22    });
23  Object.defineProperty(window, "innerWidth", { configurable: true, value: 1440, writable: true });
24  // jsdom 26 has MouseEvent but no PointerEvent; Base UI synthesizes a click from keyboard input.
25  Object.defineProperty(window, "PointerEvent", {
26    configurable: true,
27    value: class extends window.MouseEvent {
28      pointerId = 1;
29      pointerType = "mouse";
30      isPrimary = true;
31    },
32  });
33  window.HTMLElement.prototype.scrollIntoView = () => {};
34  globalThis.ResizeObserver = class {
35    observe() {}
36    unobserve() {}
37    disconnect() {}
38  };
39  globalThis.requestAnimationFrame = (callback) =>
40    setTimeout(() => callback(Date.now()), 16) as unknown as number;
41  globalThis.cancelAnimationFrame = (id) => clearTimeout(id);
42}
43
44// Base UI detects DOM availability when imported, before individual test windows exist.
45const { JSDOM } = createRequire(import.meta.url)("jsdom");
46installDom(new JSDOM("<body></body>").window);
47