Files
archie/ui/test/hooks.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

54 lines
2.0 KiB
JavaScript

import babel from "@babel/core";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
export async function resolve(specifier, context, nextResolve) {
try {
return await nextResolve(specifier, context);
} catch (err) {
if (err.code === "ERR_MODULE_NOT_FOUND" && specifier.endsWith(".js")) {
const jsxSpecifier = specifier.slice(0, -3) + ".jsx";
return await nextResolve(jsxSpecifier, context);
}
throw err;
}
}
export async function load(url, context, nextLoad) {
if (url.endsWith(".css")) {
return { format: "module", source: "export default {};", shortCircuit: true };
}
if (url.includes("?raw") || url.endsWith(".html")) {
try {
const filePath = fileURLToPath(url.replace(/\?raw$/, ""));
const content = fs.readFileSync(filePath, "utf8");
return { format: "module", source: `export default ${JSON.stringify(content)};`, shortCircuit: true };
} catch {
return { format: "module", source: 'export default "";', shortCircuit: true };
}
}
if (url.endsWith(".jsx") || url.endsWith(".test.js")) {
const result = await nextLoad(url, { ...context, format: "module" });
let source = result.source;
if (typeof source !== "string") {
source = new TextDecoder().decode(source);
}
const transformed = babel.transformSync(source, {
plugins: [[
"@babel/plugin-transform-react-jsx",
// The automatic runtime matches @preact/preset-vite, so JSX compiles
// to `import { jsx } from "preact/jsx-runtime"` rather than a bare `h`.
// This lets source files shadow `h` (e.g. a .map((h, i) => ...) in
// mapping-editor.jsx) without breaking, exactly as the Vite build does.
{ runtime: "automatic", importSource: "preact" },
]],
filename: fileURLToPath(url),
configFile: false,
babelrc: false,
});
source = transformed.code;
return { format: "module", source, shortCircuit: true };
}
return nextLoad(url, context);
}