Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestReviewAnnotation.browser.test.tsx

128 lines4.3 KB

1import { EnvironmentId, ProjectId, type PullRequestReviewThread } from "@t3tools/contracts";
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 { ReviewThreadCard } from "./PullRequestReviewAnnotation";
9​
10const thread: PullRequestReviewThread = {
11 id: "thread",
12 path: "a.ts",
13 line: 3,
14 side: "right",
15 isResolved: false,
16 isOutdated: false,
17 comments: [
18 {
19 id: "comment",
20 author: { login: "reviewer", name: null, avatarUrl: null },
21 body: "Rename this.",
22 createdAt: "2026-09-01T00:00:00Z",
23 url: null,
24 },
25 ],
26};
27​
28it("leaves IME keys in a review reply to the composition", async () => {
29 const onReply = vi.fn(async () => true);
30 await using screen = await render(
31 <ReviewThreadCard
32 thread={thread}
33 workspaceRoot="/workspace"
34 canReply
35 canResolve={false}
36 canReact={false}
37 environmentId={EnvironmentId.make("environment")}
38 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
39 completionScope={null}
40 pending={false}
41 onReply={onReply}
42 onLoadMore={async () => null}
43 canEditComment={() => false}
44 onEditComment={async () => false}
45 onToggleResolved={() => {}}
46 onReacted={() => {}}
47 />,
48 );
49 const heard: Array<{ key: string; consumed: boolean }> = [];
50 using _observer = {
51 [Symbol.dispose]: registerCommands(
52 {},
53 { observe: (event, consumed) => heard.push({ key: event.key, consumed }) },
54 ),
55 };
56 const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" });
57 await screen.getByRole("button", { name: "Reply", exact: true }).click();
58 await reply().fill("Draft");
59​
60 for (const composing of [{ isComposing: true }, { keyCode: 229 }]) {
61 for (const keys of [
62 { key: "Escape" },
63 { key: "Enter", ctrlKey: true },
64 { key: "Enter", metaKey: true },
65 ]) {
66 const label = JSON.stringify({ ...keys, ...composing });
67 const event = new KeyboardEvent("keydown", {
68 ...keys,
69 ...composing,
70 bubbles: true,
71 cancelable: true,
72 });
73 await act(() => reply().element().dispatchEvent(event));
74 expect(event.defaultPrevented, label).toBe(false);
75 // The pull requests page leaves on an Escape nothing consumed.
76 expect(heard, label).toEqual([]);
77 await expect.element(reply()).toHaveValue("Draft");
78 }
79 }
80 expect(onReply).not.toHaveBeenCalled();
81​
82 await userEvent.keyboard("{Escape}");
83 await expect.element(reply()).not.toBeInTheDocument();
84 expect(heard).toEqual([]);
85​
86 await screen.getByRole("button", { name: "Reply", exact: true }).click();
87 await reply().fill("Draft");
88 await userEvent.keyboard("{Control>}{Enter}{/Control}");
89 expect(onReply).toHaveBeenCalledExactlyOnceWith("Draft");
90});
91​
92it("Escape cancels a review reply without closing the right panel sheet", async () => {
93 const onCloseSheet = vi.fn();
94 await using _screen = await render(
95 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
96 <ReviewThreadCard
97 thread={thread}
98 workspaceRoot="/workspace"
99 canReply
100 canResolve={false}
101 canReact={false}
102 environmentId={EnvironmentId.make("environment")}
103 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
104 completionScope={null}
105 pending={false}
106 onReply={async () => true}
107 onLoadMore={async () => null}
108 canEditComment={() => false}
109 onEditComment={async () => false}
110 onToggleResolved={() => {}}
111 onReacted={() => {}}
112 />
113 </RightPanelSheet>,
114 );
115 await act(() =>
116 (page.getByRole("button", { name: "Reply", exact: true }).element() as HTMLElement).focus(),
117 );
118 await userEvent.keyboard("{Enter}");
119 const reply = page.getByRole("textbox", { name: "Reply to this conversation" });
120 await reply.fill("Draft");
121​
122 await userEvent.keyboard("{Escape}");
123 await expect.element(reply).not.toBeInTheDocument();
124 expect(onCloseSheet).not.toHaveBeenCalled();
125​
126 await userEvent.keyboard("{Escape}");
127 expect(onCloseSheet).toHaveBeenCalledOnce();
128});