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

PullRequestSummaryTab.test.tsx

214 lines7.0 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/tooltip", () => ({
13 Tooltip: ({ children }: { children: ReactNode }) => children,
14 TooltipTrigger: ({ children }: { children: ReactNode }) => children,
15 TooltipPopup: () => null,
16}));
17​
18import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
19​
20const detail: PullRequestDetailView = {
21 provider: "github",
22 projectId: ProjectId.make("project"),
23 projectTitle: "Project",
24 workspaceRoot: "/workspace",
25 repository: "owner/repo",
26 number: 1,
27 title: "Test pull request",
28 body: "Original description",
29 url: "https://github.com/owner/repo/pull/1",
30 author: { login: "author", name: null, avatarUrl: null },
31 viewer: "author",
32 state: "open",
33 isDraft: false,
34 mergeability: "mergeable",
35 additions: 1,
36 deletions: 0,
37 changedFiles: 1,
38 headBranch: "feature",
39 baseBranch: "main",
40 createdAt: "2026-09-01T00:00:00Z",
41 updatedAt: "2026-09-01T00:00:00Z",
42 mergedAt: null,
43 closedAt: null,
44 reviewers: [],
45 labels: [],
46 checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
47 comments: [],
48 commentCount: 0,
49 commentsTruncated: false,
50 reviewThreads: [],
51 commits: [],
52 mergeCapabilities: { merge: false, squash: false, rebase: false },
53 capabilities: {
54 diff: true,
55 comment: true,
56 search: true,
57 actions: [],
58 mergeMethods: [],
59 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
60 reviewers: { request: false, listCandidates: false },
61 edit: { changeRequest: true, comment: true },
62 },
63 viewerPermissions: {
64 actions: [],
65 comment: true,
66 resolve: false,
67 verdicts: [],
68 requestReviewers: false,
69 },
70};
71​
72let renderer: ReactTestRenderer;
73beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true));
74afterEach(() => {
75 act(() => renderer?.unmount());
76 vi.unstubAllGlobals();
77});
78​
79function render(value = detail) {
80 return (
81 <PullRequestSummaryTab
82 environmentId={EnvironmentId.make("environment")}
83 threadRef={null}
84 reference={value}
85 detail={value}
86 activityPending={false}
87 activityError={null}
88 onRefresh={() => {}}
89 />
90 );
92​
93function heading(title: string) {
94 const section = renderer.root
95 .findAllByType("section")
96 .find((section) => section.props["aria-label"] === title)!;
97 return section
98 .findAllByType("button")
99 .find((button) => button.findAllByType("span").some((span) => span.children.includes(title)))!;
101​
102function click(title: string) {
103 act(() =>
104 heading(title).props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }),
105 );
107​
108it("toggles checks from their heading and resets sections for another pull request", () => {
109 act(() => {
110 renderer = create(render());
111 });
112 expect(
113 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
114 ).toBe(false);
115 click("Checks");
116 expect(
117 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
118 ).toBe(true);
119 click("Checks");
120 expect(heading("Checks").props["aria-expanded"]).toBe(false);
121 click("Checks");
122 click("Description");
123 act(() =>
124 renderer.update(render({ ...detail, url: "https://github.com/owner/repo/pull/2", number: 2 })),
125 );
126 expect(heading("Checks").props["aria-expanded"]).toBe(false);
127 expect(heading("Description").props["aria-expanded"]).toBe(true);
128});
129​
130it("keeps an unsaved description when collapsed and reopened", () => {
131 act(() => {
132 renderer = create(render());
133 });
134 act(() => renderer.root.findByProps({ "aria-label": "Edit description" }).props.onClick());
135 act(() =>
136 renderer.root.findByType("textarea").props.onChange({
137 target: { value: "Unsaved description" },
138 currentTarget: { value: "Unsaved description" },
139 nativeEvent: {},
140 }),
141 );
142 click("Description");
143 expect(heading("Description").props["aria-expanded"]).toBe(false);
144 click("Description");
145 expect(renderer.root.findByType("textarea").props.value).toBe("Unsaved description");
146});
147​
148it("opens bot reports in pages without hiding human comments", () => {
149 const value: PullRequestDetailView = {
150 ...detail,
151 commentCount: 13,
152 comments: Array.from({ length: 13 }, (_, index) => ({
153 id: `comment-${index}`,
154 kind: "issue-comment",
155 author: {
156 login: index === 12 ? "human" : "review-app",
157 name: null,
158 avatarUrl: null,
159 isBot: index !== 12,
160 },
161 body: index === 12 ? "Human comment" : `Bot report ${index}`,
162 createdAt: `2026-09-01T00:00:${String(index).padStart(2, "0")}Z`,
163 url: null,
164 path: null,
165 reviewState: null,
166 })),
167 };
168 act(() => {
169 renderer = create(render(value));
170 });
171 expect(renderer.root.findAllByType("p").map((p) => p.children.join(""))).toContain(
172 "Human comment",
173 );
174 expect(
175 renderer.root.findAllByType("p").some((p) => p.children.join("").startsWith("Bot report")),
176 ).toBe(false);
177 const group = renderer.root
178 .findAllByType("button")
179 .find((button) => button.props["aria-label"] === "12 bot comments")!;
180 act(() => group.props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }));
181 expect(
182 renderer.root.findAllByType("p").filter((p) => p.children.join("").startsWith("Bot report")),
183 ).toHaveLength(10);
184 expect(renderer.root.findAllByType("p").map((p) => p.children.join(""))).not.toContain(
185 "Bot report 0",
186 );
187 act(() =>
188 renderer.root
189 .findAllByType("button")
190 .find((button) => button.children.includes(" older bot comment"))!
191 .props.onClick(),
192 );
193 expect(
194 renderer.root.findAllByType("p").filter((p) => p.children.join("").startsWith("Bot report")),
195 ).toHaveLength(12);
196 act(() => group.props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }));
197 act(() => group.props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }));
198 expect(renderer.root.findAllByType("p").map((p) => p.children.join(""))).toContain(
199 "Bot report 0",
200 );
201 act(() =>
202 renderer.root
203 .findAllByType("button")
204 .find((button) => button.children.includes(" recent bot comments"))!
205 .props.onClick(),
206 );
207 expect(
208 renderer.root.findAllByType("p").filter((p) => p.children.join("").startsWith("Bot report")),
209 ).toHaveLength(10);
210 act(() => renderer.update(render({ ...value, url: `${value.url}0`, number: 10 })));
211 expect(
212 renderer.root.findAllByType("p").some((p) => p.children.join("").startsWith("Bot report")),
213 ).toBe(false);
214});