Open History to see the last commit
1import { EnvironmentId, ThreadId, type ThreadPullRequestLink } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5
6const watchCommand = vi.hoisted(() => vi.fn());
7
8// The row queries its own pull request; this test covers only which rows the list shows.
9vi.mock(import("./ThreadDetailsPrRow"), async (importOriginal) => ({
10 ...(await importOriginal()),
11 ThreadDetailsPrRow: ({
12 number,
13 onStopWatching,
14 }: {
15 number: number;
16 onStopWatching?: (() => void) | undefined;
17 }) => (
18 <div data-row={String(number)}>
19 {onStopWatching ? <button onClick={onStopWatching}>Stop watching #{number}</button> : null}
20 </div>
21 ),
22}));
23vi.mock(import("~/state/entities"), async (importOriginal) => ({
24 ...(await importOriginal()),
25 useProjects: () => [],
26}));
27vi.mock(import("~/state/use-atom-command"), async (importOriginal) => ({
28 ...(await importOriginal()),
29 useAtomCommand: (() => watchCommand) as never,
30}));
31
32import { ThreadDetailsPrRows } from "./ThreadDetailsPrRows";
33
34function link(
35 number: number,
36 headBranch: string,
37 baseBranch: string,
38 updatedAt: string,
39): ThreadPullRequestLink {
40 return {
41 host: "github.com",
42 repository: "pingdotgg/t3code",
43 number,
44 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
45 source: "manual",
46 linkedAt: updatedAt,
47 snapshot: {
48 state: "open",
49 title: `Change ${number}`,
50 headBranch,
51 baseBranch,
52 isDraft: false,
53 updatedAt,
54 syncedAt: updatedAt,
55 },
56 stack: null,
57 };
58}
59
60const bottom = link(1, "layer-one", "main", "2026-01-01T00:00:10.000Z");
61const top = link(2, "layer-two", "layer-one", "2026-01-01T00:00:20.000Z");
62const other = link(3, "unrelated", "main", "2026-01-01T00:00:05.000Z");
63
64let root: Root;
65let container: HTMLDivElement;
66beforeEach(() => {
67 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
68 container = document.createElement("div");
69 document.body.append(container);
70 root = createRoot(container);
71});
72afterEach(() => {
73 act(() => root.unmount());
74 container.remove();
75 vi.unstubAllGlobals();
76});
77
78function render(links: ReadonlyArray<ThreadPullRequestLink>, current: ThreadPullRequestLink) {
79 act(() => {
80 root.render(
81 <ThreadDetailsPrRows
82 threadRef={{
83 environmentId: EnvironmentId.make("environment"),
84 threadId: ThreadId.make("thread"),
85 }}
86 links={links}
87 currentLink={current}
88 onOpenLink={vi.fn()}
89 environmentId={EnvironmentId.make("environment")}
90 pr={null}
91 number={current.number}
92 reference={current}
93 status={null}
94 project={null}
95 label={`#${current.number}`}
96 openAriaLabel="Open pull request"
97 onOpen={vi.fn()}
98 />,
99 );
100 });
101}
102
103const rows = () =>
104 [...container.querySelectorAll("[data-row]")].map((node) => node.getAttribute("data-row"));
105const toggleButton = () =>
106 [...container.querySelectorAll("button")].find((button) =>
107 /^(Show \d+ more|Show less)$/.test(button.textContent?.trim() ?? ""),
108 );
109const toggleLabel = () => toggleButton()?.textContent?.trim();
110
111function toggle() {
112 act(() => toggleButton()!.click());
113}
114
115it("shows only the current pull request until the rest are asked for", () => {
116 render([other, bottom, top], top);
117 expect(rows()).toEqual(["2"]);
118 expect(toggleLabel()).toBe("Show 2 more");
119
120 toggle();
121 expect(rows()).toEqual(["2", "1", "3"]);
122 expect(toggleLabel()).toBe("Show less");
123
124 toggle();
125 expect(rows()).toEqual(["2"]);
126 expect(toggleLabel()).toBe("Show 2 more");
127});
128
129it("keeps the single row untouched when the thread links one pull request", () => {
130 render([bottom], bottom);
131 expect(rows()).toEqual(["1"]);
132 expect(toggleLabel()).toBeUndefined();
133});
134
135it("lets only watched pull requests stop their watch", () => {
136 const watched: ThreadPullRequestLink = {
137 ...bottom,
138 watch: {
139 startedAt: "2026-01-01T00:00:30.000Z",
140 headSha: null,
141 failedChecks: [],
142 passed: false,
143 passedChecks: [],
144 remarksThrough: "2026-01-01T00:00:30.000Z",
145 remarkIds: [],
146 conflicting: false,
147 wakes: 0,
148 },
149 };
150 render([other, watched, top], top);
151 toggle();
152 const stopButtons = [...container.querySelectorAll("button")].filter((button) =>
153 button.textContent?.startsWith("Stop watching"),
154 );
155 expect(stopButtons.map((button) => button.textContent)).toEqual(["Stop watching #1"]);
156
157 act(() => stopButtons[0]!.click());
158 expect(watchCommand).toHaveBeenCalledWith({
159 environmentId: EnvironmentId.make("environment"),
160 input: {
161 threadId: ThreadId.make("thread"),
162 host: "github.com",
163 repository: "pingdotgg/t3code",
164 number: 1,
165 watching: false,
166 },
167 });
168});