Skip to content

Commit 923d347

Browse files
committed
test(web): read-only preview copy test checks the copy event was cancelled
The browser uses a copy handler's text only when the event is cancelled, so the helper now returns the text for a cancelled event and null otherwise. The test also makes a second Select All after scrolling to the bottom, and sizes its wrapper with classes.
1 parent 3e4beb9 commit 923d347

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎apps/web/src/components/files/ReadOnlySourcePreview.browser.test.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ import ReadOnlySourcePreview from "./ReadOnlySourcePreview";
1717
const LINE_COUNT = 301;
1818
const text = `${Array.from({ length: LINE_COUNT }, (_, index) => `line ${index + 1}`).join("\n")}\n`;
1919

20+
// The browser ignores a copy event's data unless the event is cancelled, and copies the selection itself.
2021
function copiedText(target: Element) {
2122
const clipboardData = new DataTransfer();
22-
target.dispatchEvent(
23+
const handled = !target.dispatchEvent(
2324
new ClipboardEvent("copy", { clipboardData, bubbles: true, cancelable: true, composed: true }),
2425
);
25-
return clipboardData.getData("text/plain");
26+
return handled ? clipboardData.getData("text/plain") : null;
2627
}
2728

2829
describe("ReadOnlySourcePreview", () => {
2930
it("copies the whole file after Select All, wherever the preview is scrolled", async () => {
3031
// File paints nothing until the shared highlighter has loaded; once it has, mount renders synchronously.
3132
await getSyntaxHighlighterPromise("text");
3233
await using screen = await render(
33-
<div style={{ display: "flex", flexDirection: "column", height: 400 }}>
34+
<div className="flex h-[400px] flex-col">
3435
<ReadOnlySourcePreview name="notes.txt" text={text} wordWrap={false} />
3536
</div>,
3637
);
@@ -49,10 +50,15 @@ describe("ReadOnlySourcePreview", () => {
4950
await vi.waitFor(() => expect(rows()[0]?.textContent).not.toBe("line 1"));
5051
expect(copiedText(rows()[0]!)).toBe(text);
5152

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+
5258
// A selection that ends inside the mounted rows stays the browser's to copy.
5359
const firstText = rows()[0]!.querySelector("span")!.firstChild!;
5460
const lastText = rows()[rows().length - 1]!.querySelector("span")!.firstChild!;
5561
document.getSelection()!.setBaseAndExtent(firstText, 0, lastText, 4);
56-
expect(copiedText(rows()[0]!)).toBe("");
62+
expect(copiedText(rows()[0]!)).toBeNull();
5763
});
5864
});

0 commit comments

Comments
 (0)