pi-mono/packages/chord/test/node/react/hooks.test.ts
Mario Zechner e9c0ff4d84 feat(chord): Chord v2 is the package's root
src/next is src (the main export), src/next-node is src/node with short subpaths (/http, /workers, /plugins,
/plugins/runtime, /app, /surfaces, /web, /react, /tui, /tui/launcher); the foundations get /json and /watch;
Chord v1 moves to src/old (/old, /old/node) until p2 runs on v2; tests and docs move likewise (test/, docs/,
test/old, docs/old). Durable and env import the foundations from their subpaths.
2026-11-02 22:36:55 +00:00

45 lines
2.1 KiB
TypeScript

// Tests of the React bindings (slice 11 §3) in a real browser (Chromium; skipped without one), on the fixture's page:
// `useHold` holds while mounted and releases on unmount, `useWatch` re-renders on change, `useRefState` follows the
// reference's state, `useRegistry` the registry (the panels).
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { BACKGROUND_CONTEXT as context } from "../../../src/context/index.ts";
import { Browser, CHROMIUM } from "../web/browser.ts";
import { freePort, panelPlugin, signIn, versionedEntry, webApp } from "../web/support.ts";
describe.skipIf(CHROMIUM === undefined)("React bindings", () => {
let browser: Browser;
beforeAll(async () => {
browser = await Browser.start();
}, 120_000);
afterAll(async () => {
await browser?.close();
});
it("hold while mounted, re-render on change, release on unmount", async () => {
const { app } = await webApp(await versionedEntry("1"), await freePort(), {
plugins: { notes: panelPlugin("Notes", "#123456") },
});
try {
await signIn(browser, app.ownerLink());
// useRegistry: the plugin's panel.
await browser.wait('document.querySelector(".panel")?.textContent === "Notes"');
// useHold + useRefState: the counter held and live.
await browser.wait('document.getElementById("count")?.dataset.state === "live"');
expect(await browser.evaluate<string>('document.getElementById("count").textContent')).toBe("0");
// useWatch: a change re-renders.
await browser.evaluate('document.getElementById("count").click()');
await browser.wait('document.getElementById("count").textContent === "1"');
// Unmounted: the reference is released.
await browser.evaluate('document.getElementById("toggle").click()');
await browser.wait('document.getElementById("count") === null && globalThis.counterState() === "ended"');
// Mounted again: held again.
await browser.evaluate('document.getElementById("toggle").click()');
await browser.wait('document.getElementById("count")?.dataset.state === "live"');
} finally {
await app.close(context);
}
}, 120_000);
});