Open History to see the last commit
1import { useState } from "react";
2import { expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4
5import { registerCommands } from "~/shortcuts/dispatcher";
6import { act, render } from "../../../test/render";
7import { RightPanelSheet } from "../RightPanelSheet";
8import { DiffCommentAnnotation } from "./DiffCommentAnnotation";
9
10it("leaves IME keys in a diff comment to the composition", async () => {
11 const onCancel = vi.fn();
12 const onComment = vi.fn();
13 await using screen = await render(
14 <DiffCommentAnnotation
15 kind="draft"
16 rangeLabel="3"
17 text=""
18 onCancel={onCancel}
19 onComment={onComment}
20 />,
21 );
22 const heard: Array<{ key: string; consumed: boolean }> = [];
23 using _observer = {
24 [Symbol.dispose]: registerCommands(
25 {},
26 { observe: (event, consumed) => heard.push({ key: event.key, consumed }) },
27 ),
28 };
29 const draft = screen.getByRole("textbox", { name: "Comment on lines 3" });
30 await draft.fill("Draft");
31
32 for (const composing of [{ isComposing: true }, { keyCode: 229 }]) {
33 for (const keys of [
34 { key: "Escape" },
35 { key: "Enter", ctrlKey: true },
36 { key: "Enter", metaKey: true },
37 ]) {
38 const label = JSON.stringify({ ...keys, ...composing });
39 const event = new KeyboardEvent("keydown", {
40 ...keys,
41 ...composing,
42 bubbles: true,
43 cancelable: true,
44 });
45 await act(() => draft.element().dispatchEvent(event));
46 expect(event.defaultPrevented, label).toBe(false);
47 // The right panel sheet and pull requests page close on an Escape that reaches them.
48 expect(heard, label).toEqual([]);
49 }
50 }
51 expect(onCancel).not.toHaveBeenCalled();
52 expect(onComment).not.toHaveBeenCalled();
53
54 await userEvent.keyboard("{Escape}");
55 expect(onCancel).toHaveBeenCalledOnce();
56 expect(heard).toEqual([]);
57
58 await userEvent.keyboard("{Control>}{Enter}{/Control}");
59 expect(onComment).toHaveBeenCalledExactlyOnceWith("Draft");
60});
61
62it("Escape cancels a diff comment without closing the right panel sheet", async () => {
63 const onCloseSheet = vi.fn();
64 function Draft() {
65 const [open, setOpen] = useState(true);
66 if (!open) return null;
67 return (
68 <DiffCommentAnnotation
69 kind="draft"
70 rangeLabel="3"
71 text=""
72 onCancel={() => setOpen(false)}
73 onComment={() => {}}
74 />
75 );
76 }
77 await using _screen = await render(
78 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
79 <Draft />
80 </RightPanelSheet>,
81 );
82 const draft = page.getByRole("textbox", { name: "Comment on lines 3" });
83 await act(() => (draft.element() as HTMLElement).focus());
84
85 await userEvent.keyboard("{Escape}");
86 await expect.element(draft).not.toBeInTheDocument();
87 expect(onCloseSheet).not.toHaveBeenCalled();
88
89 await userEvent.keyboard("{Escape}");
90 expect(onCloseSheet).toHaveBeenCalledOnce();
91});