Open History to see the last commit
1import { EnvironmentId, type PullRequestCheck } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6
7const state = vi.hoisted(() => ({
8 status: "success" as PullRequestCheck["status"],
9 extraStatus: null as PullRequestCheck["status"] | null,
10 perform: vi.fn(),
11}));
12
13vi.mock(import("~/state/entities"), async (importOriginal) => ({
14 ...(await importOriginal()),
15 useServerConfigs: (() => new Map()) as never,
16}));
17const queryData = () => ({
18 number: 1,
19 title: "Test PR",
20 state: "open",
21 isDraft: false,
22 mergeability: "mergeable",
23 headBranch: "feature",
24 baseBranch: "main",
25 changedFiles: 1,
26 additions: 1,
27 deletions: 0,
28 checks: [state.status, ...(state.extraStatus ? [state.extraStatus] : [])].map((status) => ({
29 name: `CI-${status}`,
30 status,
31 description: null,
32 url: null,
33 })),
34 capabilities: { actions: ["merge"], mergeMethods: ["merge"] },
35 viewerPermissions: { actions: ["merge"] },
36 mergeCapabilities: { merge: true, squash: false, rebase: false },
37});
38vi.mock(import("~/state/query"), async (importOriginal) => ({
39 ...(await importOriginal()),
40 useEnvironmentQuerySelector: ((_atom: unknown, select: (result: unknown) => unknown) =>
41 select(AsyncResult.success(queryData()))) as never,
42 useEnvironmentQueryIsNewer: () => true,
43 useEnvironmentQueryRefresh: () => ({ refresh: vi.fn(), isPending: () => false }),
44}));
45vi.mock(import("../pullRequest/usePullRequestActions"), async (importOriginal) => ({
46 ...(await importOriginal()),
47 usePullRequestActionRunner: () => ({ actionPending: false, perform: state.perform }),
48 usePullRequestHandoffs: () => ({ handoff: null, startAsk: vi.fn(), startHandoff: vi.fn() }),
49}));
50
51import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
52
53let root: Root;
54let container: HTMLDivElement;
55beforeEach(() => {
56 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
57 container = document.createElement("div");
58 document.body.append(container);
59 root = createRoot(container);
60});
61afterEach(() => {
62 act(() => root.unmount());
63 document.body.replaceChildren();
64 vi.unstubAllGlobals();
65 state.status = "success";
66 state.extraStatus = null;
67});
68
69it("requires a new merge click after passing checks become pending and pass again", () => {
70 const render = () => (
71 <ThreadDetailsPrRow
72 environmentId={EnvironmentId.make("environment")}
73 pr={null}
74 number={1}
75 status={null}
76 project={null}
77 label="Test PR"
78 openAriaLabel="Open PR"
79 onOpen={vi.fn()}
80 />
81 );
82 const clickMerge = () =>
83 act(() => {
84 [...container.querySelectorAll("button")]
85 .find((button) => button.textContent?.trim() === "Merge")!
86 .click();
87 });
88 const dialogs = () => [...document.querySelectorAll('[role="alertdialog"]')];
89
90 act(() => root.render(render()));
91 clickMerge();
92 expect(dialogs()).toHaveLength(1);
93 act(() => {
94 state.status = "pending";
95 root.render(render());
96 });
97 expect(dialogs()).toHaveLength(0);
98 act(() => {
99 state.status = "success";
100 root.render(render());
101 });
102 expect(dialogs()).toHaveLength(0);
103 clickMerge();
104 expect(dialogs()).toHaveLength(1);
105 expect(state.perform).not.toHaveBeenCalled();
106});
107
108it.each<[PullRequestCheck["status"], PullRequestCheck["status"], string]>([
109 ["success", "skipped", ""],
110 ["failure", "cancelled", ""],
111 ["success", "action-required", ""],
112 ["success", "pending", "1/2"],
113 ["failure", "pending", "1/2"],
114])("shows a count only while checks run (%s, %s)", (status, extraStatus, count) => {
115 state.status = status;
116 state.extraStatus = extraStatus;
117 act(() => {
118 root.render(
119 <ThreadDetailsPrRow
120 environmentId={EnvironmentId.make("environment")}
121 pr={null}
122 number={1}
123 status={null}
124 project={null}
125 label="Test PR"
126 openAriaLabel="Open PR"
127 onOpen={vi.fn()}
128 />,
129 );
130 });
131 const counts = [...container.querySelectorAll("*")]
132 .map((element) => element.textContent?.trim() ?? "")
133 .filter((value) => /^\d+\/\d+$/.test(value));
134 expect(new Set(counts)).toEqual(new Set(count ? [count] : []));
135});