Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

test(views): polyfill jsdom Range geometry in global setup (MUL-9253) (#10592)

Jiayuan Zhang906f366a02ce
diff --git a/packages/views/editor/content-editor-quick-actions.test.tsx b/packages/views/editor/content-editor-quick-actions.test.tsx
index f57bbfe3d3..8f11d30030 100644
--- a/packages/views/editor/content-editor-quick-actions.test.tsx
+++ b/packages/views/editor/content-editor-quick-actions.test.tsx
@@ -1,7 +1,7 @@
import type { Editor } from "@tiptap/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, cleanup, fireEvent, screen, waitFor } from "@testing-library/react";
-import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ListQuickActionsResponse, QuickAction } from "@multica/core/types";
import { quickActionKeys } from "@multica/core/quick-actions";
import { renderWithI18n } from "../test/i18n";
@@ -26,19 +26,6 @@ vi.mock("@multica/core/paths", async (importOriginal) => ({
useWorkspacePaths: () => ({ issueDetail: (id: string) => `/test/issues/${id}` }),
}));
-beforeAll(() => {
- // jsdom has no layout; ProseMirror measures the caret while opening a picker.
- const rects = () => ({
- length: 0,
- item: () => null,
- [Symbol.iterator]: function* () {},
- }) as DOMRectList;
- Range.prototype.getBoundingClientRect = () => new DOMRect();
- Range.prototype.getClientRects = rects;
- HTMLElement.prototype.getClientRects = rects;
- Object.defineProperty(Text.prototype, "getClientRects", { configurable: true, value: rects });
-});
-
beforeEach(() => {
listQuickActions.mockReset();
renderQuickAction.mockReset().mockResolvedValue("Rendered feedback");
diff --git a/packages/views/editor/extensions/file-upload.test.ts b/packages/views/editor/extensions/file-upload.test.ts
index c7cf8dbade..92cc69347c 100644
--- a/packages/views/editor/extensions/file-upload.test.ts
+++ b/packages/views/editor/extensions/file-upload.test.ts
@@ -1,4 +1,4 @@
-import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import { Markdown } from "@tiptap/markdown";
@@ -78,31 +78,6 @@ function makeUpload(
};
}
-// jsdom lays nothing out, so ProseMirror's scrollToSelection (reached via the
-// `focus()` in the fileCard insert) throws on the missing rect APIs. Same stub
-// set as suggestion-popup.test.tsx.
-beforeAll(() => {
- const rect = () => new DOMRect(0, 0, 0, 0);
- const rectList = () =>
- ({ length: 0, item: () => null, [Symbol.iterator]: function* () {} }) as DOMRectList;
- Object.defineProperty(Range.prototype, "getBoundingClientRect", {
- configurable: true,
- value: rect,
- });
- Object.defineProperty(Range.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
- Object.defineProperty(HTMLElement.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
- Object.defineProperty(Text.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
-});
-
beforeEach(() => {
originalCreateObjectURL = URL.createObjectURL;
originalRevokeObjectURL = URL.revokeObjectURL;
diff --git a/packages/views/editor/extensions/issue-identifier-autolink.test.ts b/packages/views/editor/extensions/issue-identifier-autolink.test.ts
index e8f9aa916a..0a86400cc3 100644
--- a/packages/views/editor/extensions/issue-identifier-autolink.test.ts
+++ b/packages/views/editor/extensions/issue-identifier-autolink.test.ts
@@ -3,7 +3,6 @@ import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import Link from "@tiptap/extension-link";
import { Markdown } from "@tiptap/markdown";
-import { EditorView } from "@tiptap/pm/view";
import type { RefObject } from "react";
import { BaseMentionExtension } from "./mention-extension";
import { createMarkdownPasteExtension } from "./markdown-paste";
@@ -86,10 +85,6 @@ function typeAt1(ed: Editor, text: string): void {
beforeEach(() => {
resolveMock.mockReset();
- // jsdom has no layout; ProseMirror's post-dispatch scroll needs client rects.
- (EditorView.prototype as unknown as { scrollToSelection: () => void }).scrollToSelection =
- () => {};
- Element.prototype.scrollIntoView = () => {};
});
afterEach(() => {
diff --git a/packages/views/editor/extensions/mention-boundary.test.ts b/packages/views/editor/extensions/mention-boundary.test.ts
index ec8572c771..3f759feb3f 100644
--- a/packages/views/editor/extensions/mention-boundary.test.ts
+++ b/packages/views/editor/extensions/mention-boundary.test.ts
@@ -1,9 +1,8 @@
-import { describe, it, expect, beforeAll, vi } from "vitest";
+import { describe, it, expect, vi } from "vitest";
import { Editor, Extension } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import { Markdown } from "@tiptap/markdown";
import { Suggestion } from "@tiptap/suggestion";
-import { EditorView } from "@tiptap/pm/view";
import type { QueryClient } from "@tanstack/react-query";
import { workspaceKeys } from "@multica/core/workspace/queries";
import { createMarkdownPasteExtension } from "./markdown-paste";
@@ -127,14 +126,6 @@ function picker(editor: Editor, config: ReturnType<typeof createMentionSuggestio
return { active: state?.active ?? false, query: state?.query ?? null };
}
-beforeAll(() => {
- // jsdom has no layout, so ProseMirror's post-dispatch scroll walks
- // coordsAtPos into getClientRects() on nodes jsdom does not implement.
- (EditorView.prototype as unknown as { scrollToSelection: () => void }).scrollToSelection =
- () => {};
- Element.prototype.scrollIntoView = () => {};
-});
-
describe("mention picker boundary", () => {
describe("opens where an @ starts a token", () => {
const cases: Array<[string, string]> = [
diff --git a/packages/views/editor/extensions/suggestion-popup.test.tsx b/packages/views/editor/extensions/suggestion-popup.test.tsx
index 17215ea578..82e6f16e32 100644
--- a/packages/views/editor/extensions/suggestion-popup.test.tsx
+++ b/packages/views/editor/extensions/suggestion-popup.test.tsx
@@ -4,7 +4,7 @@ import StarterKit from "@tiptap/starter-kit";
import { Suggestion, type SuggestionProps } from "@tiptap/suggestion";
import { PluginKey } from "@tiptap/pm/state";
import { forwardRef, useImperativeHandle, useState } from "react";
-import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { ContextMenu, ContextMenuContent, ContextMenuItem } from "@multica/ui/components/ui/context-menu";
import { contextMenuOriginRef } from "@multica/ui/lib/context-menu-origin";
@@ -46,27 +46,6 @@ const TestSuggestionList = forwardRef<TestListRef, TestListProps>(
let editor: Editor | null = null;
-beforeAll(() => {
- const rect = () => new DOMRect(0, 0, 0, 0);
- const rectList = () => ({ length: 0, item: () => null, [Symbol.iterator]: function* () {} }) as DOMRectList;
- Object.defineProperty(Range.prototype, "getBoundingClientRect", {
- configurable: true,
- value: rect,
- });
- Object.defineProperty(Range.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
- Object.defineProperty(HTMLElement.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
- Object.defineProperty(Text.prototype, "getClientRects", {
- configurable: true,
- value: rectList,
- });
-});
-
afterEach(() => {
act(() => {
editor?.destroy();
diff --git a/packages/views/editor/extensions/suggestion-trigger-arming.test.ts b/packages/views/editor/extensions/suggestion-trigger-arming.test.ts
index 16f44100f8..c1ac4f79fc 100644
--- a/packages/views/editor/extensions/suggestion-trigger-arming.test.ts
+++ b/packages/views/editor/extensions/suggestion-trigger-arming.test.ts
@@ -1,10 +1,9 @@
-import { describe, it, expect, beforeAll } from "vitest";
+import { describe, it, expect } from "vitest";
import { Editor, Extension } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import { Markdown } from "@tiptap/markdown";
import { Suggestion } from "@tiptap/suggestion";
import { PluginKey, TextSelection } from "@tiptap/pm/state";
-import { EditorView } from "@tiptap/pm/view";
import { createMarkdownPasteExtension } from "./markdown-paste";
import {
SuggestionTriggerArmingExtension,
@@ -97,15 +96,6 @@ function picker(editor: Editor, pluginKey: PluginKey = key) {
return { active: state?.active, query: state?.query };
}
-beforeAll(() => {
- // jsdom has no layout, so ProseMirror's post-dispatch scroll walks
- // coordsAtPos into getClientRects() on nodes jsdom does not implement.
- // Scrolling is irrelevant here; these tests read plugin state.
- (EditorView.prototype as unknown as { scrollToSelection: () => void }).scrollToSelection =
- () => {};
- Element.prototype.scrollIntoView = () => {};
-});
-
describe("suggestion trigger arming", () => {
describe("text the user did not type never opens a picker", () => {
it("pasted text containing @", () => {
diff --git a/packages/views/issues/components/annotation-composer.test.tsx b/packages/views/issues/components/annotation-composer.test.tsx
index 3713bace57..a7b656d7a4 100644
--- a/packages/views/issues/components/annotation-composer.test.tsx
+++ b/packages/views/issues/components/annotation-composer.test.tsx
@@ -1,7 +1,6 @@
import { createRef } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, cleanup, fireEvent, screen, waitFor } from "@testing-library/react";
-import { EditorView } from "@tiptap/pm/view";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import {
cancelVoice,
@@ -29,9 +28,6 @@ const annotation: ReplyAnnotation = {
};
beforeAll(() => {
- // jsdom has no layout, so ProseMirror's post-dispatch scroll walks
- // coordsAtPos into getClientRects() on nodes jsdom does not implement.
- (EditorView.prototype as unknown as { scrollToSelection: () => void }).scrollToSelection = () => {};
connectVoiceRuntime(() => ({
record: () => ({
start: () => Promise.resolve(),
diff --git a/packages/views/issues/hooks/use-in-page-find.test.ts b/packages/views/issues/hooks/use-in-page-find.test.ts
index dd6052e587..171d4ef60d 100644
--- a/packages/views/issues/hooks/use-in-page-find.test.ts
+++ b/packages/views/issues/hooks/use-in-page-find.test.ts
@@ -71,21 +71,12 @@ describe("useInPageFind without the CSS Custom Highlight API", () => {
let container: HTMLElement;
beforeEach(() => {
- // jsdom lays nothing out and doesn't implement Range.getClientRects, which
- // the scroll-to-match path now calls in every browser. Return a single
- // zero-size rect so scrollRangeIntoView no-ops instead of throwing.
- (Range.prototype as { getClientRects: () => DOMRectList }).getClientRects =
- () =>
- [
- { top: 0, bottom: 0, left: 0, right: 0, width: 0, height: 0 },
- ] as unknown as DOMRectList;
container = document.createElement("div");
container.innerHTML = "<h1>Find me</h1><p>find me twice: find</p>";
document.body.appendChild(container);
});
afterEach(() => {
- delete (Range.prototype as { getClientRects?: unknown }).getClientRects;
container.remove();
});
diff --git a/packages/views/test/setup.ts b/packages/views/test/setup.ts
index 7561b75b3a..e7e9c6ebc7 100644
--- a/packages/views/test/setup.ts
+++ b/packages/views/test/setup.ts
@@ -40,4 +40,16 @@ if (typeof window !== "undefined") {
if (typeof Element.prototype.scrollIntoView !== "function") {
Element.prototype.scrollIntoView = () => {};
}
+
+ // jsdom doesn't implement Range geometry. ProseMirror's post-dispatch
+ // scrollToSelection measures the caret through coordsAtPos, which calls these
+ // on a text Range, so any test that types into an editor would throw.
+ // Return an empty layout; tests that need real geometry override them.
+ if (typeof Range.prototype.getClientRects !== "function") {
+ Range.prototype.getClientRects = () =>
+ ({ length: 0, item: () => null, [Symbol.iterator]: function* () {} }) as unknown as DOMRectList;
+ }
+ if (typeof Range.prototype.getBoundingClientRect !== "function") {
+ Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0);
+ }
}