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

PullRequestCodeTab.browser.test.tsx

201 lines6.0 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 SourceControlProviderKind,
5 type PullRequestDetailView,
6 type PullRequestDiffResult,
7} from "@t3tools/contracts";
8import { expect, it, vi } from "vite-plus/test";
9​
10import "~/index.css";
11​
12const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
13 const { AsyncResult, Atom } = await import("effect/reactivity");
14 const filePatch = (path: string) =>
15 [
16 `diff --git a/${path} b/${path}`,
17 "index 1111111..2222222 100644",
18 `--- a/${path}`,
19 `+++ b/${path}`,
20 "@@ -1,6 +1,6 @@",
21 "-before()",
22 "+after()",
23 " one()",
24 " two()",
25 " three()",
26 " four()",
27 " five()",
28 "",
29 ].join("\n");
30 const firstPage: PullRequestDiffResult = {
31 patch: filePatch("a.ts"),
32 truncated: false,
33 nextCursor: null,
34 };
35 const pages: Record<string, PullRequestDiffResult> = {
36 first: {
37 patch: Array.from({ length: 40 }, (_, index) => filePatch(`early-${index}.ts`)).join(""),
38 truncated: true,
39 nextCursor: "page-2",
40 },
41 "page-2": { patch: filePatch("late.ts"), truncated: false, nextCursor: null },
42 };
43 return {
44 // An environment query whose refetch returns equal content hands back the previous value
45 // itself, so a refresh publishes a new result around the very same page.
46 diffAtom: Atom.make(() => AsyncResult.success(firstPage)),
47 pagedDiffAtom: Atom.family((cursor: string) =>
48 Atom.make(() => AsyncResult.success(pages[cursor]!)),
49 ),
50 };
51});
52​
53const mocks = vi.hoisted(() => ({ paged: false }));
54​
55vi.mock(import("~/state/use-atom-command"), async (original) => ({
56 ...(await original()),
57 useAtomCommand: (() => vi.fn()) as never,
58}));
59vi.mock(import("~/state/pullRequests"), async (original) => {
60 const actual = await original();
61 return {
62 ...actual,
63 pullRequestEnvironment: {
64 ...actual.pullRequestEnvironment,
65 diff: (({ input }: { input: { cursor?: string } }) =>
66 mocks.paged ? pagedDiffAtom(input.cursor ?? "first") : diffAtom) as never,
67 },
68 };
69});
70​
71import { render } from "../../../test/render";
72import PullRequestCodeTab from "./PullRequestCodeTab";
73​
74const detail: PullRequestDetailView = {
75 provider: SourceControlProviderKind.make("github"),
76 projectId: ProjectId.make("project"),
77 projectTitle: "Project",
78 workspaceRoot: "/workspace",
79 repository: "owner/repo",
80 number: 1,
81 title: "Test pull request",
82 body: "",
83 url: "https://github.com/owner/repo/pull/1",
84 author: { login: "author", name: null, avatarUrl: null },
85 viewer: "author",
86 state: "open",
87 isDraft: false,
88 mergeability: "mergeable",
89 additions: 1,
90 deletions: 1,
91 changedFiles: 1,
92 headBranch: "feature",
93 baseBranch: "main",
94 createdAt: "2026-09-01T00:00:00Z",
95 updatedAt: "2026-09-01T00:00:00Z",
96 mergedAt: null,
97 closedAt: null,
98 reviewers: [],
99 labels: [],
100 checks: [],
101 comments: [],
102 commentCount: 0,
103 commentsTruncated: false,
104 reviewThreads: [],
105 commits: [],
106 mergeCapabilities: { merge: false, squash: false, rebase: false },
107 capabilities: {
108 diff: true,
109 comment: false,
110 search: true,
111 actions: [],
112 mergeMethods: [],
113 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
114 reviewers: { request: false, listCandidates: false },
115 edit: { changeRequest: false, comment: false },
116 },
117 viewerPermissions: {
118 actions: [],
119 comment: false,
120 resolve: false,
121 verdicts: [],
122 requestReviewers: false,
123 },
124};
125​
126function setup({ paged }: { paged: boolean }) {
127 mocks.paged = paged;
129​
130const tab = (
131 refreshToken: number,
132 revealRequest: { readonly path: string; readonly nonce: number } | null = null,
133) => (
134 <PullRequestCodeTab
135 scrollerRef={() => {}}
136 environmentId={EnvironmentId.make("environment")}
137 reference={detail}
138 detail={detail}
139 selectedCommitOid={null}
140 onSelectedCommitChange={() => {}}
141 onRefresh={() => {}}
142 refreshToken={refreshToken}
143 revealRequest={revealRequest}
144 />
145);
146​
147it("keeps the diff after a refresh that brings back the same page", async () => {
148 setup({ paged: false });
149 const container = document.createElement("div");
150 container.className = "flex h-[700px] w-[900px]";
151 await using screen = await render(tab(0), { container });
152 await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
153​
154 await screen.rerender(tab(1));
155​
156 await expect
157 .element(screen.getByText("This pull request has no file changes."))
158 .not.toBeInTheDocument();
159 await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
160});
161​
162function pagedContainer() {
163 const container = document.createElement("div");
164 container.className = "flex h-[500px] w-[900px]";
165 return container;
167​
168// Rows are virtualized, so a header far down is not rendered until the view scrolls to it.
169async function expectFileHeaderShown(
170 screen: Awaited<ReturnType<typeof render>>,
171 container: HTMLElement,
172 path: string,
173) {
174 const header = screen.getByText(path, { exact: true }).first();
175 await expect.element(header).toBeInTheDocument();
176 await vi.waitFor(() => {
177 const top = header.element().getBoundingClientRect().top;
178 const bounds = container.getBoundingClientRect();
179 expect(top).toBeGreaterThanOrEqual(bounds.top);
180 expect(top).toBeLessThan(bounds.top + bounds.height / 2);
181 });
183​
184it("scrolls to a file asked for as the tab opens", async () => {
185 setup({ paged: true });
186 const container = pagedContainer();
187 await using screen = await render(tab(0, { path: "early-39.ts", nonce: 1 }), { container });
188​
189 await expectFileHeaderShown(screen, container, "early-39.ts");
190});
191​
192it("pages forward to a file asked for that is not loaded yet, then scrolls to it", async () => {
193 setup({ paged: true });
194 const container = pagedContainer();
195 await using screen = await render(tab(0), { container });
196 await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible();
197​
198 await screen.rerender(tab(0, { path: "late.ts", nonce: 1 }));
199​
200 await expectFileHeaderShown(screen, container, "late.ts");
201});