Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
154 lines
4.2 KiB
TypeScript
154 lines
4.2 KiB
TypeScript
import React from "react";
|
|
import { act } from "react";
|
|
import { JSDOM } from "jsdom";
|
|
|
|
type DomEnvironment = {
|
|
window: Window & typeof globalThis;
|
|
document: Document;
|
|
cleanup: () => void;
|
|
};
|
|
|
|
export function installDomEnvironment(): DomEnvironment {
|
|
const dom = new JSDOM("<!doctype html><html><body><div id=\"root\"></div><div id=\"splash\"></div></body></html>", {
|
|
url: "http://localhost/",
|
|
});
|
|
|
|
const previousWindow = globalThis.window;
|
|
const previousDocument = globalThis.document;
|
|
const previousNavigator = globalThis.navigator;
|
|
const previousHTMLElement = globalThis.HTMLElement;
|
|
const previousNode = globalThis.Node;
|
|
const previousEvent = globalThis.Event;
|
|
const previousFocusEvent = globalThis.FocusEvent;
|
|
const previousKeyboardEvent = globalThis.KeyboardEvent;
|
|
const previousMouseEvent = globalThis.MouseEvent;
|
|
const previousCustomEvent = globalThis.CustomEvent;
|
|
const previousDOMParser = globalThis.DOMParser;
|
|
const previousGetComputedStyle = globalThis.getComputedStyle;
|
|
|
|
const overrides = {
|
|
window: dom.window,
|
|
document: dom.window.document,
|
|
navigator: dom.window.navigator,
|
|
HTMLElement: dom.window.HTMLElement,
|
|
Node: dom.window.Node,
|
|
Event: dom.window.Event,
|
|
FocusEvent: dom.window.FocusEvent,
|
|
KeyboardEvent: dom.window.KeyboardEvent,
|
|
MouseEvent: dom.window.MouseEvent,
|
|
CustomEvent: dom.window.CustomEvent,
|
|
DOMParser: dom.window.DOMParser,
|
|
getComputedStyle: dom.window.getComputedStyle.bind(dom.window),
|
|
} as const;
|
|
|
|
Object.defineProperty(dom.window.document, "hasFocus", {
|
|
configurable: true,
|
|
value: () => true,
|
|
});
|
|
|
|
for (const [key, value] of Object.entries(overrides)) {
|
|
Object.defineProperty(globalThis, key, {
|
|
configurable: true,
|
|
writable: true,
|
|
value,
|
|
});
|
|
}
|
|
|
|
if (!dom.window.HTMLElement.prototype.attachEvent) {
|
|
Object.defineProperty(dom.window.HTMLElement.prototype, "attachEvent", {
|
|
configurable: true,
|
|
writable: true,
|
|
value: () => {},
|
|
});
|
|
}
|
|
if (!dom.window.HTMLElement.prototype.detachEvent) {
|
|
Object.defineProperty(dom.window.HTMLElement.prototype, "detachEvent", {
|
|
configurable: true,
|
|
writable: true,
|
|
value: () => {},
|
|
});
|
|
}
|
|
|
|
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
|
|
configurable: true,
|
|
writable: true,
|
|
value: true,
|
|
});
|
|
|
|
return {
|
|
window: dom.window as Window & typeof globalThis,
|
|
document: dom.window.document,
|
|
cleanup() {
|
|
dom.window.close();
|
|
const previousValues = {
|
|
window: previousWindow,
|
|
document: previousDocument,
|
|
navigator: previousNavigator,
|
|
HTMLElement: previousHTMLElement,
|
|
Node: previousNode,
|
|
Event: previousEvent,
|
|
FocusEvent: previousFocusEvent,
|
|
KeyboardEvent: previousKeyboardEvent,
|
|
MouseEvent: previousMouseEvent,
|
|
CustomEvent: previousCustomEvent,
|
|
DOMParser: previousDOMParser,
|
|
getComputedStyle: previousGetComputedStyle,
|
|
} as const;
|
|
for (const [key, value] of Object.entries(previousValues)) {
|
|
Object.defineProperty(globalThis, key, {
|
|
configurable: true,
|
|
writable: true,
|
|
value,
|
|
});
|
|
}
|
|
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
|
|
configurable: true,
|
|
writable: true,
|
|
value: undefined,
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function createDomRenderer(document: Document) {
|
|
const container = document.getElementById("root");
|
|
if (!container) {
|
|
throw new Error("DOM root container missing");
|
|
}
|
|
|
|
const { createRoot } = await import("react-dom/client");
|
|
const root = createRoot(container);
|
|
|
|
return {
|
|
container,
|
|
async render(node: React.ReactNode) {
|
|
await act(async () => {
|
|
root.render(node);
|
|
});
|
|
},
|
|
async unmount() {
|
|
await act(async () => {
|
|
root.unmount();
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function flushEffects() {
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
});
|
|
}
|
|
|
|
export async function dispatchDomEvent(target: EventTarget, event: Event) {
|
|
await act(async () => {
|
|
target.dispatchEvent(event);
|
|
});
|
|
}
|
|
|
|
export async function runWithAct(run: () => void | Promise<void>) {
|
|
await act(async () => {
|
|
await run();
|
|
});
|
|
}
|