| 1 | import { JSDOM } from "jsdom"; |
| 2 | import { act } from "react"; |
| 3 | |
| 4 | export function installTranscriptRecoveryRaceDom() { |
| 5 | const dom = new JSDOM('<!doctype html><html><body><div id="root"></div><div id="scroll"><div class="transcript__row" data-row-key="row-a"></div></div></body></html>', { |
| 6 | pretendToBeVisual: true, |
| 7 | url: "http://localhost/", |
| 8 | }); |
| 9 | (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 10 | globalThis.window = dom.window as unknown as Window & typeof globalThis; |
| 11 | globalThis.document = dom.window.document; |
| 12 | globalThis.HTMLElement = dom.window.HTMLElement; |
| 13 | globalThis.Element = dom.window.Element; |
| 14 | globalThis.Node = dom.window.Node; |
| 15 | |
| 16 | let nextFrame = 1; |
| 17 | const frames = new Map<number, FrameRequestCallback>(); |
| 18 | const requestFrame = (callback: FrameRequestCallback) => { |
| 19 | const id = nextFrame; |
| 20 | nextFrame += 1; |
| 21 | frames.set(id, callback); |
| 22 | return id; |
| 23 | }; |
| 24 | const cancelFrame = (id: number) => void frames.delete(id); |
| 25 | globalThis.requestAnimationFrame = requestFrame; |
| 26 | globalThis.cancelAnimationFrame = cancelFrame; |
| 27 | dom.window.requestAnimationFrame = requestFrame; |
| 28 | dom.window.cancelAnimationFrame = cancelFrame; |
| 29 | |
| 30 | const flushFrames = async () => { |
| 31 | const pending = [...frames.entries()]; |
| 32 | frames.clear(); |
| 33 | await act(async () => pending.forEach(([, callback]) => callback(performance.now()))); |
| 34 | }; |
| 35 | return { dom, flushFrames }; |
| 36 | } |
| 37 |