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.each(["textbox", "button"] as const)(
63 "Escape from a diff comment %s cancels the comment without closing the right panel sheet",
64 async (target) => {
65 const onCloseSheet = vi.fn();
66 function Draft() {
67 const [open, setOpen] = useState(true);
68 if (!open) return null;
69 return (
70 <DiffCommentAnnotation
71 kind="draft"
72 rangeLabel="3"
73 text=""
74 focusOnMount={false}
75 onCancel={() => setOpen(false)}
76 onComment={() => {}}
77 />
78 );
79 }
80 await using _screen = await render(
81 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
82 <Draft />
83 </RightPanelSheet>,
84 );
85 const draft = page.getByRole("textbox", { name: "Comment on lines 3" });
86 await act(() => (draft.element() as HTMLElement).focus());
87 if (target === "button") {
88 await userEvent.tab();
89 await expect.element(page.getByRole("button", { name: "Cancel" })).toHaveFocus();
90 }
91
92 await userEvent.keyboard("{Escape}");
93 expect(onCloseSheet).not.toHaveBeenCalled();
94 await expect.element(draft).not.toBeInTheDocument();
95
96 await userEvent.keyboard("{Escape}");
97 expect(onCloseSheet).toHaveBeenCalledOnce();
98 },
99);