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

PullRequestSummaryTab.test.tsx

141 lines4.3 KB

1import { EnvironmentId, ProjectId, type PullRequestDetailView } from "@t3tools/contracts";
2import { act } 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}));
12​
13import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
14​
15const detail: PullRequestDetailView = {
16 provider: "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 renderer: ReactTestRenderer;
68beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true));
69afterEach(() => {
70 act(() => renderer?.unmount());
71 vi.unstubAllGlobals();
72});
73​
74function render(value = detail) {
75 return (
76 <PullRequestSummaryTab
77 environmentId={EnvironmentId.make("environment")}
78 threadRef={null}
79 reference={value}
80 detail={value}
81 activityPending={false}
82 activityError={null}
83 onRefresh={() => {}}
84 />
85 );
87​
88function heading(title: string) {
89 const section = renderer.root
90 .findAllByType("section")
91 .find((section) => section.props["aria-label"] === title)!;
92 return section
93 .findAllByType("button")
94 .find((button) => button.findAllByType("span").some((span) => span.children.includes(title)))!;
96​
97function click(title: string) {
98 act(() =>
99 heading(title).props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }),
100 );
102​
103it("toggles checks from their heading and resets sections for another pull request", () => {
104 act(() => {
105 renderer = create(render());
106 });
107 expect(
108 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
109 ).toBe(false);
110 click("Checks");
111 expect(
112 renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
113 ).toBe(true);
114 click("Checks");
115 expect(heading("Checks").props["aria-expanded"]).toBe(false);
116 click("Checks");
117 click("Description");
118 act(() =>
119 renderer.update(render({ ...detail, url: "https://github.com/owner/repo/pull/2", number: 2 })),
120 );
121 expect(heading("Checks").props["aria-expanded"]).toBe(false);
122 expect(heading("Description").props["aria-expanded"]).toBe(true);
123});
124​
125it("keeps an unsaved description when collapsed and reopened", () => {
126 act(() => {
127 renderer = create(render());
128 });
129 act(() => renderer.root.findByProps({ "aria-label": "Edit description" }).props.onClick());
130 act(() =>
131 renderer.root.findByType("textarea").props.onChange({
132 target: { value: "Unsaved description" },
133 currentTarget: { value: "Unsaved description" },
134 nativeEvent: {},
135 }),
136 );
137 click("Description");
138 expect(heading("Description").props["aria-expanded"]).toBe(false);
139 click("Description");
140 expect(renderer.root.findByType("textarea").props.value).toBe("Unsaved description");
141});