-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathSelectableDiffRenderer.dragSelect.test.tsx
More file actions
185 lines (150 loc) · 6.55 KB
/
SelectableDiffRenderer.dragSelect.test.tsx
File metadata and controls
185 lines (150 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { afterEach, beforeEach, describe, expect, mock, test, type Mock } from "bun:test";
import { copyFile, rm } from "node:fs/promises";
import { randomUUID } from "node:crypto";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalWindow } from "happy-dom";
import { cleanup, fireEvent, render, waitFor } from "@testing-library/react";
import { requireTestModule } from "@/browser/testUtils";
import { ThemeProvider } from "@/browser/contexts/ThemeContext";
import { TooltipProvider } from "@/browser/components/Tooltip/Tooltip";
import type * as DiffRendererModule from "./DiffRenderer";
let SelectableDiffRenderer!: typeof DiffRendererModule.SelectableDiffRenderer;
let isolatedDiffRendererPath: string | null = null;
const sharedDir = dirname(fileURLToPath(import.meta.url));
async function importIsolatedSelectableDiffRenderer() {
const isolatedPath = join(sharedDir, `DiffRenderer.dragSelect.real.${randomUUID()}.tsx`);
// Load a unique temp copy of the real module so earlier Bun mock.module registrations for
// @/browser/features/Shared/DiffRenderer cannot swap in the stubbed renderer for this suite.
await copyFile(join(sharedDir, "DiffRenderer.tsx"), isolatedPath);
({ SelectableDiffRenderer } = requireTestModule<{
SelectableDiffRenderer: typeof DiffRendererModule.SelectableDiffRenderer;
}>(isolatedPath));
return isolatedPath;
}
describe("SelectableDiffRenderer drag selection", () => {
let onReviewNote: Mock<(data: unknown) => void>;
let originalRequestAnimationFrame: typeof globalThis.requestAnimationFrame;
let originalCancelAnimationFrame: typeof globalThis.cancelAnimationFrame;
let rafHandleCounter = 0;
const rafTimeouts = new Map<number, ReturnType<typeof setTimeout>>();
beforeEach(async () => {
isolatedDiffRendererPath = await importIsolatedSelectableDiffRenderer();
globalThis.window = new GlobalWindow() as unknown as Window & typeof globalThis;
globalThis.document = globalThis.window.document;
originalRequestAnimationFrame = globalThis.requestAnimationFrame;
originalCancelAnimationFrame = globalThis.cancelAnimationFrame;
rafHandleCounter = 0;
rafTimeouts.clear();
// Happy DOM in this isolated test does not provide RAF globals, but the review composer
// schedules textarea resize/drag updates through animation frames during drag selection.
const requestAnimationFrameMock: typeof requestAnimationFrame = (callback) => {
rafHandleCounter += 1;
const handle = rafHandleCounter;
const timeout = setTimeout(() => {
rafTimeouts.delete(handle);
callback(Date.now());
}, 0);
rafTimeouts.set(handle, timeout);
return handle;
};
const cancelAnimationFrameMock: typeof cancelAnimationFrame = (handle) => {
const timeout = rafTimeouts.get(handle);
if (!timeout) {
return;
}
clearTimeout(timeout);
rafTimeouts.delete(handle);
};
globalThis.requestAnimationFrame = requestAnimationFrameMock;
globalThis.cancelAnimationFrame = cancelAnimationFrameMock;
globalThis.window.requestAnimationFrame = requestAnimationFrameMock;
globalThis.window.cancelAnimationFrame = cancelAnimationFrameMock;
onReviewNote = mock(() => undefined);
});
afterEach(async () => {
cleanup();
for (const timeout of rafTimeouts.values()) {
clearTimeout(timeout);
}
rafTimeouts.clear();
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
globalThis.cancelAnimationFrame = originalCancelAnimationFrame;
if (globalThis.window) {
globalThis.window.requestAnimationFrame = originalRequestAnimationFrame;
globalThis.window.cancelAnimationFrame = originalCancelAnimationFrame;
}
globalThis.window = undefined as unknown as Window & typeof globalThis;
globalThis.document = undefined as unknown as Document;
if (isolatedDiffRendererPath) {
await rm(isolatedDiffRendererPath, { force: true });
isolatedDiffRendererPath = null;
}
});
test("hovering the review button does not show a tooltip", async () => {
const content = "+const a = 1;\n+const b = 2;";
const { container } = render(
<ThemeProvider forcedTheme="dark">
<TooltipProvider>
<SelectableDiffRenderer
content={content}
filePath="src/test.ts"
onReviewNote={onReviewNote}
maxHeight="none"
enableHighlighting={false}
/>
</TooltipProvider>
</ThemeProvider>
);
const commentButton = await waitFor(() =>
container.querySelector<HTMLButtonElement>('button[aria-label="Add review comment"]')
);
expect(commentButton).toBeTruthy();
expect(commentButton?.getAttribute("title")).toBeNull();
fireEvent.mouseEnter(commentButton!);
expect(document.body.textContent).not.toContain(
"Add review comment (Shift-click or drag to select range)"
);
});
test("dragging on the indicator column selects a line range", async () => {
const content = "+const a = 1;\n+const b = 2;\n+const c = 3;";
const { container, getByPlaceholderText } = render(
<ThemeProvider forcedTheme="dark">
<TooltipProvider>
<SelectableDiffRenderer
content={content}
filePath="src/test.ts"
onReviewNote={onReviewNote}
maxHeight="none"
enableHighlighting={false}
/>
</TooltipProvider>
</ThemeProvider>
);
await waitFor(() => {
const indicators = container.querySelectorAll('[data-diff-indicator="true"]');
expect(indicators.length).toBe(3);
});
const indicators = Array.from(
container.querySelectorAll<HTMLSpanElement>('[data-diff-indicator="true"]')
);
fireEvent.mouseDown(indicators[0], { button: 0 });
fireEvent.mouseEnter(indicators[2]);
fireEvent.mouseUp(window);
const textarea = (await waitFor(() =>
getByPlaceholderText(/Add a review note/i)
)) as HTMLTextAreaElement;
await waitFor(() => {
const selectedLines = Array.from(
container.querySelectorAll<HTMLElement>('.selectable-diff-line[data-selected="true"]')
);
expect(selectedLines.length).toBe(3);
const allLines = Array.from(container.querySelectorAll<HTMLElement>(".selectable-diff-line"));
expect(allLines.length).toBe(3);
// Input should render *after* the last selected line (line 2).
const inputWrapper = allLines[2]?.nextElementSibling;
expect(inputWrapper).toBeTruthy();
expect(inputWrapper?.querySelector("textarea")).toBe(textarea);
});
});
});