Files
archie/ui/test/register.js
T
sam 5965f3158a test(webui): migrate UI tests to Preact testing library and fix tasks Fragment render
Use @testing-library/preact rendered under jsdom (registered via
test/register.js) so the Post-Preact .jsx components and pages are
exercised through the real automatic-runtime render path instead of the
legacy el() DOM-helper API the tests were written against.

- Add jsdom + @testing-library/preact dev deps and a jsdom globals setup
  in ui/test/register.js (window/document/navigator/getComputedStyle/...).
- Switch the .jsx loader hook to the automatic JSX runtime to match
  @preact/preset-vite, so source files that shadow h (mapping-editor) and
  Fragment/children arrays work exactly as in the Vite build.
- Migrate captures, config-row, mappings, settings-update-status,
  task-logs, settings-lifecycle, settings-schema, tasks-actions and the
  chat-app tests to render the Preact components and query the DOM via
  testing-library.
- Fix a real render bug in tasks.jsx: <h.Fragment> is not rendered by the
  automatic runtime (rows came out as [object Object]); use the imported
  <Fragment> instead, matching the other migrated modules.
2026-09-03 00:52:20 +10:00

46 lines
1.9 KiB
JavaScript

import { register } from "node:module";
import { JSDOM } from "jsdom";
register("./hooks.js", import.meta.url);
// A real DOM for @testing-library/preact. The custom shim in shim.js covers
// only the el() DOM-building primitives; the testing library needs full DOM
// globals (document, window, getComputedStyle, MutationObserver, ...), which
// jsdom provides. Applied here so every test file sees them before importing
// source modules.
const dom = new JSDOM("<!doctype html><html><body></body></html>", { url: "http://localhost/" });
const { window } = dom;
// Some globals (navigator, location) are read-only accessors on Node's
// globalThis, so assign via defineProperty rather than direct assignment.
const setGlobal = (key, value) => {
Object.defineProperty(globalThis, key, { value, configurable: true, writable: true });
};
setGlobal("window", window);
setGlobal("document", window.document);
setGlobal("navigator", window.navigator);
setGlobal("HTMLElement", window.HTMLElement);
setGlobal("Element", window.Element);
setGlobal("Node", window.Node);
setGlobal("Text", window.Text);
setGlobal("TextNode", window.TextNode);
setGlobal("DocumentFragment", window.DocumentFragment);
setGlobal("CustomEvent", window.CustomEvent);
setGlobal("Event", window.Event);
setGlobal("AbortController", window.AbortController);
setGlobal("getComputedStyle", window.getComputedStyle.bind(window));
setGlobal("MutationObserver", window.MutationObserver);
if (typeof window.requestAnimationFrame === "function") {
setGlobal("requestAnimationFrame", window.requestAnimationFrame.bind(window));
} else {
setGlobal("requestAnimationFrame", (cb) => setTimeout(() => cb(Date.now()), 0));
}
if (typeof window.cancelAnimationFrame === "function") {
setGlobal("cancelAnimationFrame", window.cancelAnimationFrame.bind(window));
} else {
setGlobal("cancelAnimationFrame", (id) => clearTimeout(id));
}
setGlobal("localStorage", window.localStorage);
setGlobal("location", window.location);