Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { EnvironmentId, ProjectId, type PullRequestDetailView } from "@t3tools/contracts";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6import { userEvent } from "vite-plus/test/browser";
7
8vi.mock(import("~/state/use-atom-command"), async (original) => ({
9 ...(await original()),
10 useAtomCommand: (() => vi.fn()) as never,
11}));
12
13import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
14
15const detail: PullRequestDetailView = {
16 provider: SourceControlProviderKind.make("github"),
17 projectId: ProjectId.make("project"),
18 projectTitle: "Project",
19 workspaceRoot: "/workspace",
20 repository: "owner/repo",
21 number: 1,
22 title: "Test pull request",
23 body: "Original description",
24 url: "https://github.com/owner/repo/pull/1",
25 author: { login: "author", name: null, avatarUrl: null },
26 viewer: "author",
27 state: "open",
28 isDraft: false,
29 mergeability: "mergeable",
30 additions: 1,
31 deletions: 0,
32 changedFiles: 1,
33 headBranch: "feature",
34 baseBranch: "main",
35 createdAt: "2026-09-01T00:00:00Z",
36 updatedAt: "2026-09-01T00:00:00Z",
37 mergedAt: null,
38 closedAt: null,
39 reviewers: [],
40 labels: [],
41 checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
42 comments: [],
43 commentCount: 0,
44 commentsTruncated: false,
45 reviewThreads: [],
46 commits: [],
47 mergeCapabilities: { merge: false, squash: false, rebase: false },
48 capabilities: {
49 diff: true,
50 comment: true,
51 search: true,
52 actions: [],
53 mergeMethods: [],
54 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
55 reviewers: { request: false, listCandidates: false },
56 edit: { changeRequest: true, comment: true },
57 },
58 viewerPermissions: {
59 actions: [],
60 comment: true,
61 resolve: false,
62 verdicts: [],
63 requestReviewers: false,
64 },
65};
66
67let root: Root;
68let container: HTMLDivElement;
69beforeEach(() => {
70 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
71 container = document.createElement("div");
72 document.body.append(container);
73 root = createRoot(container);
74});
75afterEach(async () => {
76 await act(async () => root.unmount());
77 container.remove();
78 localStorage.clear();
79 vi.unstubAllGlobals();
80});
81
82const render = (value = detail) =>
83 act(async () =>
84 root.render(
85 <PullRequestSummaryTab
86 scrollerRef={() => {}}
87 environmentId={EnvironmentId.make("environment")}
88 threadRef={null}
89 reference={value}
90 detail={value}
91 activityPending={false}
92 activityError={null}
93 onRefresh={() => {}}
94 />,
95 ),
96 );
97
98function heading(title: string) {
99 const section = container.querySelector(`section[aria-label="${title}"]`);
100 expect(section, `section ${title}`).not.toBeNull();
101 return [...section!.querySelectorAll("button")].find(
102 (button) => button.querySelector("span")?.textContent === title,
103 )!;
104}
105
106const click = (title: string) => act(async () => heading(title).click());
107const expanded = (title: string) => heading(title).getAttribute("aria-expanded");
108const visibleText = () => container.innerText;
109
110it("toggles checks from their heading and resets sections for another pull request", async () => {
111 await render();
112 expect(visibleText()).not.toContain("Unit tests");
113 await click("Checks");
114 expect(visibleText()).toContain("Unit tests");
115 await click("Checks");
116 expect(expanded("Checks")).toBe("false");
117 await click("Checks");
118 await click("Description");
119 await render({ ...detail, url: "https://github.com/owner/repo/pull/2", number: 2 });
120 expect(expanded("Checks")).toBe("false");
121 expect(expanded("Description")).toBe("true");
122});
123
124it("keeps an unsaved description when collapsed and reopened", async () => {
125 await render();
126 await act(async () =>
127 container.querySelector<HTMLButtonElement>('[aria-label="Edit description"]')!.click(),
128 );
129 const textarea = container.querySelector("textarea")!;
130 await userEvent.clear(textarea);
131 await userEvent.type(textarea, "Unsaved description");
132 await click("Description");
133 expect(expanded("Description")).toBe("false");
134 await click("Description");
135 expect(container.querySelector("textarea")!.value).toBe("Unsaved description");
136});
137
138function commentTexts() {
139 const section = [...container.querySelectorAll("section")].find((element) =>
140 element.getAttribute("aria-label")?.startsWith("Comments"),
141 )!;
142 return [...section.querySelectorAll("p")]
143 .filter((p) => p.checkVisibility())
144 .map((p) => p.textContent ?? "");
145}
146
147const filterButton = () =>
148 container.querySelector<HTMLButtonElement>('[aria-label="Filter comments"]')!;
149
150function filterItem(label: string) {
151 const item = [...document.querySelectorAll('[role="menuitemcheckbox"]')].find((element) =>
152 element.textContent?.includes(label),
153 ) as HTMLElement | undefined;
154 expect(item, `filter ${label}`).toBeDefined();
155 return item!;
156}
157
158async function toggleFilter(label: string) {
159 if (!document.querySelector('[role="menuitemcheckbox"]')) await userEvent.click(filterButton());
160 await userEvent.click(filterItem(label));
161}
162
163it("hides bot and resolved comments until the filter shows them, and remembers the choice", async () => {
164 const comment = (index: number, login: string, body: string, isBot = false) => ({
165 id: `comment-${index}`,
166 kind: "issue-comment" as const,
167 author: { login, name: null, avatarUrl: null, isBot },
168 body,
169 createdAt: `2026-09-01T00:00:${String(index).padStart(2, "0")}Z`,
170 url: null,
171 path: null,
172 reviewState: null,
173 });
174 const resolved = {
175 ...comment(13, "reviewer", "Resolved remark"),
176 kind: "review-comment" as const,
177 };
178 const value: PullRequestDetailView = {
179 ...detail,
180 commentCount: 14,
181 comments: [
182 ...Array.from({ length: 12 }, (_, index) =>
183 comment(index, "review-app", `Bot report ${index}`, true),
184 ),
185 comment(12, "human", "Human comment"),
186 resolved,
187 ],
188 reviewThreads: [
189 {
190 id: "thread",
191 path: "src/index.ts",
192 line: 1,
193 side: "right",
194 isResolved: true,
195 isOutdated: false,
196 comments: [resolved],
197 },
198 ],
199 };
200 await render(value);
201 expect(commentTexts()).toEqual(["Human comment"]);
202 expect(filterButton().textContent).toContain("13 hidden");
203
204 await toggleFilter("Bot comments");
205 expect(commentTexts().filter((text) => text.startsWith("Bot report"))).toHaveLength(9);
206 expect(commentTexts()).toContain("Human comment");
207 expect(commentTexts()).not.toContain("Bot report 0");
208
209 await toggleFilter("Resolved or dismissed");
210 await userEvent.keyboard("{Escape}");
211 expect(
212 [...container.querySelectorAll("button")].some((button) => button.textContent === "Resolved"),
213 ).toBe(true);
214
215 await act(async () => root.unmount());
216 root = createRoot(container);
217 await render({ ...value, url: `${value.url}0`, number: 10 });
218 expect(commentTexts()).toContain("Bot report 11");
219 await userEvent.click(filterButton());
220 expect(filterItem("Bot comments").getAttribute("aria-checked")).toBe("true");
221});