Файли
openidea/src/test/setup.ts
T
Deng Liuтаdengliu cbae278355 feat(markdown): highlight search and selected word occurrences in preview (#2413)
⌘F in a Markdown file only decorated matches in the editor, so the rendered preview (Split and Preview Only) showed none, and selecting a word in the preview didn't highlight its other occurrences. The Markdown viewer now publishes the active Find query and current match, and the preview draws matches, the current match and selected-word occurrences with the CSS Custom Highlight API in the editor scheme's colours, scrolling the current match into view; a selection in the preview seeds Find, and closing Find in Preview Only returns focus to the preview. Matches are indexed once per render, not rescanned per keystroke.

Co-authored-by: dengliu <[email protected]>
2026-11-10 04:52:13 +08:00

56 рядки
2.4 KiB
TypeScript

import "@testing-library/jest-dom/vitest";
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
afterEach(() => cleanup());
// jsdom has no layout: give elements a size so virtualized lists render rows.
if (typeof window !== "undefined") {
// jsdom's user agent names the host OS; tests are written for macOS (⌘ keymaps) and opt into Windows explicitly.
Object.defineProperty(window.navigator, "userAgent", {
configurable: true,
get: () => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) jsdom",
});
Object.defineProperty(HTMLElement.prototype, "clientHeight", { configurable: true, get: () => 600 });
Object.defineProperty(HTMLElement.prototype, "clientWidth", { configurable: true, get: () => 400 });
Object.defineProperty(HTMLElement.prototype, "offsetHeight", { configurable: true, get: () => 600 });
Object.defineProperty(HTMLElement.prototype, "offsetWidth", { configurable: true, get: () => 400 });
if (!("ResizeObserver" in window)) {
class ResizeObserverStub {
observe() {}
unobserve() {}
disconnect() {}
}
(window as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverStub;
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverStub;
}
if (!window.matchMedia) {
window.matchMedia = (query: string) =>
({
matches: false,
media: query,
onchange: null,
addEventListener() {},
removeEventListener() {},
addListener() {},
removeListener() {},
dispatchEvent: () => false,
}) as MediaQueryList;
}
Element.prototype.scrollIntoView ??= function () {};
// No layout either: CodeMirror measures text ranges when it positions the caret,
// tooltips and line heights, sometimes in an animation frame after a test ends.
Range.prototype.getClientRects ??= () => Object.assign([], { item: () => null }) as unknown as DOMRectList;
Range.prototype.getBoundingClientRect ??= () => new DOMRect();
Element.prototype.scrollTo ??= function () {} as Element["scrollTo"];
if (!("Highlight" in globalThis)) {
class HighlightStub extends Set<AbstractRange> {
priority = 0;
}
(globalThis as unknown as { Highlight: unknown }).Highlight = HighlightStub;
}
const gCss = ((globalThis as unknown as { CSS?: Record<string, unknown> }).CSS ??= {});
gCss.highlights ??= new Map<string, Set<AbstractRange>>();
}