Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 SourceControlProviderKind,
5 type PullRequestDetail,
6} from "@t3tools/contracts";
7import { expect, it, vi } from "vite-plus/test";
8import { page, userEvent } from "vite-plus/test/browser";
9
10import { act, render } from "../../../test/render";
11import { RightPanelSheet } from "../RightPanelSheet";
12import { PullRequestTitleEditor } from "./PullRequestTitleEditor";
13
14const update = vi.hoisted(() => vi.fn());
15vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => update }));
16
17const detail: PullRequestDetail = {
18 provider: SourceControlProviderKind.make("github"),
19 projectId: ProjectId.make("project"),
20 projectTitle: "Project",
21 workspaceRoot: "/workspace",
22 repository: "owner/repo",
23 number: 1,
24 title: "Test pull request",
25 body: "",
26 url: "https://github.com/owner/repo/pull/1",
27 author: { login: "author", name: null, avatarUrl: null },
28 viewer: "author",
29 state: "open",
30 isDraft: false,
31 mergeability: "mergeable",
32 additions: 1,
33 deletions: 0,
34 changedFiles: 1,
35 headBranch: "feature",
36 baseBranch: "main",
37 createdAt: "2026-09-01T00:00:00Z",
38 updatedAt: "2026-09-01T00:00:00Z",
39 mergedAt: null,
40 closedAt: null,
41 reviewers: [],
42 labels: [],
43 checks: [],
44 mergeCapabilities: { merge: false, squash: false, rebase: false },
45 capabilities: {
46 diff: true,
47 comment: false,
48 search: true,
49 actions: [],
50 mergeMethods: [],
51 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
52 reviewers: { request: false, listCandidates: false },
53 edit: { changeRequest: true, comment: false },
54 },
55 viewerPermissions: {
56 actions: [],
57 comment: false,
58 resolve: false,
59 verdicts: [],
60 requestReviewers: false,
61 },
62};
63
64it("Escape cancels the title edit without closing the right panel sheet", async () => {
65 const onCloseSheet = vi.fn();
66 await using _screen = await render(
67 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
68 <PullRequestTitleEditor
69 environmentId={EnvironmentId.make("environment")}
70 reference={{ projectId: detail.projectId, repository: "owner/repo", number: 1 }}
71 pullRequestKey="owner/repo#1"
72 detail={detail}
73 canWriteSourceControl
74 />
75 </RightPanelSheet>,
76 );
77 const editTitleButton = page.getByRole("button", { name: "Edit title" });
78 await act(() => (editTitleButton.element() as HTMLElement).click());
79 const input = page.getByRole("textbox", { name: "Pull request title" });
80 await expect.element(input).toHaveFocus();
81
82 await userEvent.keyboard("{Escape}");
83 await expect.element(input).not.toBeInTheDocument();
84 await expect.element(editTitleButton).toHaveFocus();
85 expect(onCloseSheet).not.toHaveBeenCalled();
86
87 await userEvent.keyboard("{Enter}");
88 await expect.element(input).toHaveFocus();
89 await userEvent.keyboard("{Tab}");
90 await expect.element(page.getByRole("button", { name: "Cancel" })).toHaveFocus();
91 await userEvent.keyboard("{Enter}");
92 await expect.element(input).not.toBeInTheDocument();
93 await expect.element(editTitleButton).toHaveFocus();
94 expect(onCloseSheet).not.toHaveBeenCalled();
95
96 await userEvent.keyboard("{Enter}");
97 await expect.element(input).toHaveFocus();
98 await userEvent.keyboard("{Tab}");
99 await expect.element(page.getByRole("button", { name: "Cancel" })).toHaveFocus();
100 await userEvent.keyboard("{Escape}");
101 expect(onCloseSheet).not.toHaveBeenCalled();
102 await expect.element(input).not.toBeInTheDocument();
103 await expect.element(editTitleButton).toHaveFocus();
104
105 await userEvent.keyboard("{Escape}");
106 await vi.waitFor(() => expect(onCloseSheet).toHaveBeenCalledTimes(1));
107});
108
109it("Submitting the title returns focus to Edit title", async () => {
110 update.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
111 await using _screen = await render(
112 <PullRequestTitleEditor
113 environmentId={EnvironmentId.make("environment")}
114 reference={{ projectId: detail.projectId, repository: "owner/repo", number: 1 }}
115 pullRequestKey="owner/repo#1"
116 detail={detail}
117 canWriteSourceControl
118 />,
119 );
120 const editTitleButton = page.getByRole("button", { name: "Edit title" });
121 const input = page.getByRole("textbox", { name: "Pull request title" });
122
123 for (const title of [null, "", "Renamed pull request"]) {
124 await act(() => (editTitleButton.element() as HTMLElement).click());
125 await expect.element(input).toHaveFocus();
126 if (title !== null) {
127 await userEvent.clear(input);
128 if (title) await userEvent.type(input, title);
129 }
130 await userEvent.keyboard("{Enter}");
131 await expect.element(input).not.toBeInTheDocument();
132 await expect.element(editTitleButton).toHaveFocus();
133 }
134 expect(update).toHaveBeenCalledOnce();
135});