Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 2 additions & 50 deletions src/react/components/ui/adapter/drawer.conformance.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,14 @@ import { createRoot } from "react-dom/client";
import { JSDOM } from "npm:jsdom@28.0.0";
import { assert, assertEquals } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { installComponentDom } from "#veryfront/testing/dom-globals.ts";
import { Drawer, DrawerClose, DrawerContent, DrawerTitle, DrawerTrigger } from "../drawer.tsx";
import { UIAdapterProvider } from "./context.tsx";
import { Slot } from "../slot.tsx";
import type { DrawerParts } from "./contract.ts";

class ResizeObserverStub {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}

function installDom(dom: JSDOM): () => void {
const w = dom.window as unknown as Record<string, unknown>;
const g = globalThis as unknown as Record<string, unknown>;
const keys = [
"document",
"window",
"navigator",
"HTMLElement",
"Node",
"Element",
"MouseEvent",
"getComputedStyle",
"ResizeObserver",
"requestAnimationFrame",
"cancelAnimationFrame",
];
const prev: Record<string, unknown> = {};
for (const k of keys) prev[k] = g[k];
for (const k of keys) g[k] = w[k];
g.document = w.document;
g.window = w;
g.getComputedStyle = (w.getComputedStyle as (e: Element) => CSSStyleDeclaration).bind(w);
g.ResizeObserver = ResizeObserverStub;
// The stub is a real timer, so a frame still queued when the test ends is a
// pending timer and trips the leak sanitizer. React can schedule a frame right
// up to unmount, so track outstanding frames and drain them on teardown.
const pendingFrames = new Set<ReturnType<typeof setTimeout>>();
g.requestAnimationFrame = (cb: (t: number) => void) => {
const id = setTimeout(() => {
pendingFrames.delete(id);
cb(0);
}, 0);
pendingFrames.add(id);
return id as unknown as number;
};
g.cancelAnimationFrame = (id: number) => {
pendingFrames.delete(id as unknown as ReturnType<typeof setTimeout>);
clearTimeout(id);
};
return () => {
for (const frame of pendingFrames) clearTimeout(frame);
pendingFrames.clear();
for (const k of keys) g[k] = prev[k];
dom.window.close();
};
return installComponentDom(dom);
}

function render(el: React.ReactElement): { doc: Document; unmount: () => void } {
Expand Down
76 changes: 4 additions & 72 deletions src/react/components/ui/alert-dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { JSDOM } from "npm:jsdom@28.0.0";
import { unmountReactRoot } from "#veryfront/react/react-root.test-helpers.ts";
import { assert, assertEquals } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { installComponentDom } from "#veryfront/testing/dom-globals.ts";

import {
AlertDialog,
Expand All @@ -35,80 +36,11 @@ import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "./dialog.tsx"
// jsdom harness - installs a fresh DOM per render and stubs the browser APIs
// jsdom lacks (ResizeObserver, rAF, matchMedia) so effect-driven components mount.
// ---------------------------------------------------------------------------
class ResizeObserverStub {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}

function installDom(dom: JSDOM): () => void {
const w = dom.window as unknown as Record<string, unknown>;
const g = globalThis as unknown as Record<string, unknown>;
const keys = [
"document",
"window",
"navigator",
"HTMLElement",
"HTMLButtonElement",
"Node",
"Element",
"FocusEvent",
"KeyboardEvent",
"MouseEvent",
"getComputedStyle",
"ResizeObserver",
"matchMedia",
"requestAnimationFrame",
"cancelAnimationFrame",
];
const prev: Record<string, unknown> = {};
for (const k of keys) prev[k] = g[k];

g.document = w.document;
g.window = w;
g.navigator = w.navigator;
g.HTMLElement = w.HTMLElement;
g.HTMLButtonElement = w.HTMLButtonElement;
g.Node = w.Node;
g.Element = w.Element;
g.FocusEvent = w.FocusEvent;
g.KeyboardEvent = w.KeyboardEvent;
g.MouseEvent = w.MouseEvent;
g.getComputedStyle = (w.getComputedStyle as (e: Element) => CSSStyleDeclaration).bind(w);
g.ResizeObserver = ResizeObserverStub;
g.matchMedia = () => ({
matches: false,
media: "",
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
onchange: null,
dispatchEvent: () => false,
return installComponentDom(dom, {
matchMedia: true,
windowGlobals: ["KeyboardEvent", "FocusEvent", "HTMLButtonElement"],
});
// The stub is a real timer, so a frame still queued when the test ends is a
// pending timer and trips the leak sanitizer. React can schedule a frame right
// up to unmount, so track outstanding frames and drain them on teardown.
const pendingFrames = new Set<ReturnType<typeof setTimeout>>();
g.requestAnimationFrame = (cb: (t: number) => void) => {
const id = setTimeout(() => {
pendingFrames.delete(id);
cb(0);
}, 0);
pendingFrames.add(id);
return id as unknown as number;
};
g.cancelAnimationFrame = (id: number) => {
pendingFrames.delete(id as unknown as ReturnType<typeof setTimeout>);
clearTimeout(id);
};

return () => {
for (const frame of pendingFrames) clearTimeout(frame);
pendingFrames.clear();
for (const k of keys) g[k] = prev[k];
dom.window.close();
};
}

async function waitFor(
Expand Down
69 changes: 2 additions & 67 deletions src/react/components/ui/breadcrumb.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { JSDOM } from "npm:jsdom@28.0.0";
import { assert, assertEquals } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";

import { installComponentDom } from "#veryfront/testing/dom-globals.ts";
import {
Breadcrumb,
BreadcrumbEllipsis,
Expand All @@ -27,74 +28,8 @@ import {
// jsdom harness - installs a fresh DOM per render and stubs the browser APIs
// jsdom lacks (ResizeObserver, rAF, matchMedia) so effect-driven components mount.
// ---------------------------------------------------------------------------
class ResizeObserverStub {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}

function installDom(dom: JSDOM): () => void {
const w = dom.window as unknown as Record<string, unknown>;
const g = globalThis as unknown as Record<string, unknown>;
const keys = [
"document",
"window",
"navigator",
"HTMLElement",
"Node",
"Element",
"MouseEvent",
"getComputedStyle",
"ResizeObserver",
"matchMedia",
"requestAnimationFrame",
"cancelAnimationFrame",
];
const prev: Record<string, unknown> = {};
for (const k of keys) prev[k] = g[k];

g.document = w.document;
g.window = w;
g.navigator = w.navigator;
g.HTMLElement = w.HTMLElement;
g.Node = w.Node;
g.Element = w.Element;
g.MouseEvent = w.MouseEvent;
g.getComputedStyle = (w.getComputedStyle as (e: Element) => CSSStyleDeclaration).bind(w);
g.ResizeObserver = ResizeObserverStub;
g.matchMedia = () => ({
matches: false,
media: "",
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
onchange: null,
dispatchEvent: () => false,
});
// The stub is a real timer, so a frame still queued when the test ends is a
// pending timer and trips the leak sanitizer. React can schedule a frame right
// up to unmount, so track outstanding frames and drain them on teardown.
const pendingFrames = new Set<ReturnType<typeof setTimeout>>();
g.requestAnimationFrame = (cb: (t: number) => void) => {
const id = setTimeout(() => {
pendingFrames.delete(id);
cb(0);
}, 0);
pendingFrames.add(id);
return id as unknown as number;
};
g.cancelAnimationFrame = (id: number) => {
pendingFrames.delete(id as unknown as ReturnType<typeof setTimeout>);
clearTimeout(id);
};

return () => {
for (const frame of pendingFrames) clearTimeout(frame);
pendingFrames.clear();
for (const k of keys) g[k] = prev[k];
dom.window.close();
};
return installComponentDom(dom, { matchMedia: true });
}

/** Render `element` into a fresh DOM; returns the host node and a teardown. */
Expand Down
71 changes: 2 additions & 69 deletions src/react/components/ui/calendar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,15 @@ import { createRoot } from "react-dom/client";
import { JSDOM } from "npm:jsdom@28.0.0";
import { assert, assertEquals } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { installComponentDom } from "#veryfront/testing/dom-globals.ts";
import { Calendar } from "./calendar.tsx";

// ---------------------------------------------------------------------------
// jsdom harness - installs a fresh DOM per render and stubs the browser APIs
// jsdom lacks (ResizeObserver, rAF, matchMedia) so effect-driven components mount.
// ---------------------------------------------------------------------------
class ResizeObserverStub {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}

function installDom(dom: JSDOM): () => void {
const w = dom.window as unknown as Record<string, unknown>;
const g = globalThis as unknown as Record<string, unknown>;
const keys = [
"document",
"window",
"navigator",
"HTMLElement",
"Node",
"Element",
"KeyboardEvent",
"MouseEvent",
"getComputedStyle",
"ResizeObserver",
"matchMedia",
"requestAnimationFrame",
"cancelAnimationFrame",
];
const prev: Record<string, unknown> = {};
for (const k of keys) prev[k] = g[k];

g.document = w.document;
g.window = w;
g.navigator = w.navigator;
g.HTMLElement = w.HTMLElement;
g.Node = w.Node;
g.Element = w.Element;
g.KeyboardEvent = w.KeyboardEvent;
g.MouseEvent = w.MouseEvent;
g.getComputedStyle = (w.getComputedStyle as (e: Element) => CSSStyleDeclaration).bind(w);
g.ResizeObserver = ResizeObserverStub;
g.matchMedia = () => ({
matches: false,
media: "",
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
onchange: null,
dispatchEvent: () => false,
});
// The stub is a real timer, so a frame still queued when the test ends is a
// pending timer and trips the leak sanitizer. React can schedule a frame right
// up to unmount, so track outstanding frames and drain them on teardown.
const pendingFrames = new Set<ReturnType<typeof setTimeout>>();
g.requestAnimationFrame = (cb: (t: number) => void) => {
const id = setTimeout(() => {
pendingFrames.delete(id);
cb(0);
}, 0);
pendingFrames.add(id);
return id as unknown as number;
};
g.cancelAnimationFrame = (id: number) => {
pendingFrames.delete(id as unknown as ReturnType<typeof setTimeout>);
clearTimeout(id);
};

return () => {
for (const frame of pendingFrames) clearTimeout(frame);
pendingFrames.clear();
for (const k of keys) g[k] = prev[k];
dom.window.close();
};
return installComponentDom(dom, { matchMedia: true, windowGlobals: ["KeyboardEvent"] });
}

/** Render `element` into a fresh DOM; returns the host node, a click helper and a teardown. */
Expand Down
Loading