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

PullRequestSummaryTab.test.tsx

245 lines7.5 KB

1import { EnvironmentId, ProjectId, type PullRequestDetailView } from "@t3tools/contracts";
2import { act, type ReactNode } from "react";
3import { create, type ReactTestRenderer } from "react-test-renderer";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5​
6vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
7vi.mock("~/state/pullRequests", () => ({ pullRequestEnvironment: {} }));
8vi.mock("~/browser/useOpenLink", () => ({ useOpenLink: () => vi.fn() }));
9vi.mock("./PullRequestMarkdown", () => ({
10 PullRequestMarkdown: ({ text }: { text: string }) => <p>{text}</p>,
11}));
12vi.mock("../ui/menu", () => ({
13 Menu: "div",
14 MenuCheckboxItem: "div",
15 MenuGroup: "div",
16 MenuGroupLabel: "div",
17 MenuPopup: "div",
18 MenuTrigger: "div",
19}));
20vi.mock("../ui/tooltip", () => ({
21 Tooltip: ({ children }: { children: ReactNode }) => children,
22 TooltipTrigger: ({ children }: { children: ReactNode }) => children,
23 TooltipPopup: () => null,
24}));
25​
26import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
27​
28const detail: PullRequestDetailView = {
29 provider: "github",
30 projectId: ProjectId.make("project"),
31 projectTitle: "Project",
32 workspaceRoot: "/workspace",
33 repository: "owner/repo",
34 number: 1,
35 title: "Test pull request",
36 body: "Original description",
37 url: "https://github.com/owner/repo/pull/1",
38 author: { login: "author", name: null, avatarUrl: null },
39 viewer: "author",
40 state: "open",
41 isDraft: false,
42 mergeability: "mergeable",
43 additions: 1,
44 deletions: 0,
45 changedFiles: 1,
46 headBranch: "feature",
47 baseBranch: "main",
48 createdAt: "2026-09-01T00:00:00Z",
49 updatedAt: "2026-09-01T00:00:00Z",
50 mergedAt: null,
51 closedAt: null,
52 reviewers: [],
53 labels: [],
54 checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
55 comments: [],
56 commentCount: 0,
57 commentsTruncated: false,
58 reviewThreads: [],
59 commits: [],
60 mergeCapabilities: { merge: false, squash: false, rebase: false },
61 capabilities: {
62 diff: true,
63 comment: true,
64 search: true,
65 actions: [],
66 mergeMethods: [],
67 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
68 reviewers: { request: false, listCandidates: false },
69 edit: { changeRequest: true, comment: true },
70 },
71 viewerPermissions: {
72 actions: [],
73 comment: true,
74 resolve: false,
75 verdicts: [],
76 requestReviewers: false,
77 },
78};
79​
80let renderer: ReactTestRenderer;
81beforeEach(() => {
82 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
83 const store = new Map<string, string>();
84 vi.stubGlobal(
85 "window",
86 Object.assign(new EventTarget(), {
87 localStorage: {
88 getItem: (key: string) => store.get(key) ?? null,
89 setItem: (key: string, value: string) => store.set(key, value),
90 removeItem: (key: string) => store.delete(key),
91 },
92 }),
93 );
94});
95afterEach(() => {
96 act(() => renderer?.unmount());
97 vi.unstubAllGlobals();
98});
99​
100function render(value = detail) {
101 return (
102 <PullRequestSummaryTab
103 environmentId={EnvironmentId.make("environment")}
104 threadRef={null}
105 reference={value}
106 detail={value}
107 activityPending={false}
108 activityError={null}
109 onRefresh={() => {}}
110 />
111 );
113​
114function heading(title: string) {
115 const section = renderer.root
116 .findAllByType("section")
117 .find((section) => section.props["aria-label"] === title)!;
118 return section
119 .findAllByType("button")
120 .find((button) => button.findAllByType("span").some((span) => span.children.includes(title)))!;
122​
123function click(title: string) {
124 act(() =>
125 heading(title).props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }),
126 );
128​
129it("toggles checks from their heading and resets sections for another pull request", () => {
130 act(() => {
131 renderer = create(render());
132 });
133 expect(
134 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
135 ).toBe(false);
136 click("Checks");
137 expect(
138 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
139 ).toBe(true);
140 click("Checks");
141 expect(heading("Checks").props["aria-expanded"]).toBe(false);
142 click("Checks");
143 click("Description");
144 act(() =>
145 renderer.update(render({ ...detail, url: "https://github.com/owner/repo/pull/2", number: 2 })),
146 );
147 expect(heading("Checks").props["aria-expanded"]).toBe(false);
148 expect(heading("Description").props["aria-expanded"]).toBe(true);
149});
150​
151it("keeps an unsaved description when collapsed and reopened", () => {
152 act(() => {
153 renderer = create(render());
154 });
155 act(() => renderer.root.findByProps({ "aria-label": "Edit description" }).props.onClick());
156 act(() =>
157 renderer.root.findByType("textarea").props.onChange({
158 target: { value: "Unsaved description" },
159 currentTarget: { value: "Unsaved description" },
160 nativeEvent: {},
161 }),
162 );
163 click("Description");
164 expect(heading("Description").props["aria-expanded"]).toBe(false);
165 click("Description");
166 expect(renderer.root.findByType("textarea").props.value).toBe("Unsaved description");
167});
168​
169function texts() {
170 return renderer.root
171 .findAllByType("p")
172 .map((p) => p.children.join(""))
173 .filter((text) => text !== detail.body);
175​
176function filterItem(label: string) {
177 return renderer.root.find(
178 (node) => typeof node.props.onCheckedChange === "function" && node.children.includes(label),
179 );
181​
182it("hides bot and resolved comments until the filter shows them, and remembers the choice", () => {
183 const comment = (index: number, login: string, body: string, isBot = false) => ({
184 id: `comment-${index}`,
185 kind: "issue-comment" as const,
186 author: { login, name: null, avatarUrl: null, isBot },
187 body,
188 createdAt: `2026-09-01T00:00:${String(index).padStart(2, "0")}Z`,
189 url: null,
190 path: null,
191 reviewState: null,
192 });
193 const resolved = {
194 ...comment(13, "reviewer", "Resolved remark"),
195 kind: "review-comment" as const,
196 };
197 const value: PullRequestDetailView = {
198 ...detail,
199 commentCount: 14,
200 comments: [
201 ...Array.from({ length: 12 }, (_, index) =>
202 comment(index, "review-app", `Bot report ${index}`, true),
203 ),
204 comment(12, "human", "Human comment"),
205 resolved,
206 ],
207 reviewThreads: [
208 {
209 id: "thread",
210 path: "src/index.ts",
211 line: 1,
212 side: "right",
213 isResolved: true,
214 isOutdated: false,
215 comments: [resolved],
216 },
217 ],
218 };
219 act(() => {
220 renderer = create(render(value));
221 });
222 expect(texts()).toEqual(["Human comment"]);
223 expect(
224 renderer.root.findByProps({ "aria-label": "Filter comments" }).children.join(""),
225 ).toContain("13 hidden");
226​
227 act(() => filterItem("Bot comments").props.onCheckedChange(true));
228 // Bots now share the human comment's pages: the ten most recent, the oldest left behind.
229 expect(texts().filter((text) => text.startsWith("Bot report"))).toHaveLength(9);
230 expect(texts()).toContain("Human comment");
231 expect(texts()).not.toContain("Bot report 0");
232​
233 act(() => filterItem("Resolved or dismissed").props.onCheckedChange(true));
234 expect(
235 renderer.root.findAllByType("button").some((button) => button.children.includes("Resolved")),
236 ).toBe(true);
237​
238 // Another pull request, or the same one opened again, keeps the reader's choice.
239 act(() => renderer.unmount());
240 act(() => {
241 renderer = create(render({ ...value, url: `${value.url}0`, number: 10 }));
242 });
243 expect(filterItem("Bot comments").props.checked).toBe(true);
244 expect(texts()).toContain("Bot report 11");
245});