Open History to see the last commit
1import { expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3
4import { registerCommands } from "~/shortcuts/dispatcher";
5import { act, render } from "../../../test/render";
6import { DiffCommentAnnotation } from "./DiffCommentAnnotation";
7
8it("leaves IME keys in a diff comment to the composition", async () => {
9 const onCancel = vi.fn();
10 const onComment = vi.fn();
11 await using screen = await render(
12 <DiffCommentAnnotation
13 kind="draft"
14 rangeLabel="3"
15 text=""
16 onCancel={onCancel}
17 onComment={onComment}
18 />,
19 );
20 const heard: Array<{ key: string; consumed: boolean }> = [];
21 using _observer = {
22 [Symbol.dispose]: registerCommands(
23 {},
24 { observe: (event, consumed) => heard.push({ key: event.key, consumed }) },
25 ),
26 };
27 const draft = screen.getByRole("textbox", { name: "Comment on lines 3" });
28 await draft.fill("Draft");
29
30 for (const composing of [{ isComposing: true }, { keyCode: 229 }]) {
31 for (const keys of [
32 { key: "Escape" },
33 { key: "Enter", ctrlKey: true },
34 { key: "Enter", metaKey: true },
35 ]) {
36 const label = JSON.stringify({ ...keys, ...composing });
37 const event = new KeyboardEvent("keydown", {
38 ...keys,
39 ...composing,
40 bubbles: true,
41 cancelable: true,
42 });
43 await act(() => draft.element().dispatchEvent(event));
44 expect(event.defaultPrevented, label).toBe(false);
45 // The right panel sheet and pull requests page close on an Escape that reaches them.
46 expect(heard, label).toEqual([]);
47 }
48 }
49 expect(onCancel).not.toHaveBeenCalled();
50 expect(onComment).not.toHaveBeenCalled();
51
52 await userEvent.keyboard("{Escape}");
53 expect(onCancel).toHaveBeenCalledOnce();
54 expect(heard).toEqual([{ key: "Escape", consumed: true }]);
55
56 await userEvent.keyboard("{Control>}{Enter}{/Control}");
57 expect(onComment).toHaveBeenCalledExactlyOnceWith("Draft");
58});