Open History to see the last commit
1import type { ReactNode } from "react";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4import "~/index.css";
5
6import { render } from "../../../test/render";
7
8import { getSyntaxHighlighterPromise } from "~/lib/syntaxHighlighting";
9
10// The pool starts its workers before showing any code, which can outlast a cold CI browser.
11vi.mock(import("~/components/DiffWorkerPoolProvider"), () => ({
12 DiffWorkerPoolProvider: ({ children }: { children?: ReactNode }) => <>{children}</>,
13}));
14
15import ReadOnlySourcePreview from "./ReadOnlySourcePreview";
16
17const LINE_COUNT = 301;
18const text = `${Array.from({ length: LINE_COUNT }, (_, index) => `line ${index + 1}`).join("\n")}\n`;
19
20// The browser ignores a copy event's data unless the event is cancelled, and copies the selection itself.
21function copiedText(target: Element) {
22 const clipboardData = new DataTransfer();
23 const handled = !target.dispatchEvent(
24 new ClipboardEvent("copy", { clipboardData, bubbles: true, cancelable: true, composed: true }),
25 );
26 return handled ? clipboardData.getData("text/plain") : null;
27}
28
29describe("ReadOnlySourcePreview", () => {
30 it("copies the whole file after Select All, wherever the preview is scrolled", async () => {
31 // File paints nothing until the shared highlighter has loaded; once it has, mount renders synchronously.
32 await getSyntaxHighlighterPromise("text");
33 await using screen = await render(
34 <div className="flex h-[400px] flex-col">
35 <ReadOnlySourcePreview name="notes.txt" text={text} wordWrap={false} />
36 </div>,
37 );
38 const root = screen.container.querySelector("diffs-container")!.shadowRoot!;
39 const rows = () => root.querySelectorAll("[data-line]");
40 await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
41 // Only rows near the viewport are mounted, so the browser's own copy would stop there.
42 expect(rows().length).toBeLessThan(LINE_COUNT);
43
44 await userEvent.click(rows()[0]!);
45 await userEvent.keyboard("{ControlOrMeta>}a{/ControlOrMeta}");
46 expect(copiedText(rows()[0]!)).toBe(text);
47
48 const scroller = screen.container.querySelector(".file-preview-virtualizer")!;
49 scroller.scrollTop = scroller.scrollHeight;
50 await vi.waitFor(() => expect(rows()[0]?.textContent).not.toBe("line 1"));
51 expect(copiedText(rows()[0]!)).toBe(text);
52
53 document.getSelection()!.removeAllRanges();
54 await userEvent.click(rows()[rows().length - 1]!);
55 await userEvent.keyboard("{ControlOrMeta>}a{/ControlOrMeta}");
56 expect(copiedText(rows()[0]!)).toBe(text);
57
58 // A selection that stops inside the mounted rows at either end stays the browser's to copy.
59 const pre = root.querySelector("pre")!;
60 const firstText = rows()[0]!.querySelector("span")!.firstChild!;
61 const lastText = rows()[rows().length - 1]!.querySelector("span")!.firstChild!;
62 document.getSelection()!.setBaseAndExtent(pre, 0, lastText, 4);
63 expect(copiedText(rows()[0]!)).toBeNull();
64 document.getSelection()!.setBaseAndExtent(firstText, 4, pre, pre.childNodes.length);
65 expect(copiedText(rows()[0]!)).toBeNull();
66 });
67});