Open History to see the last commit
1import type { EnvironmentId, ProjectId, PullRequestCheck } from "@t3tools/contracts";
2import { Children, isValidElement, type ReactNode } from "react";
3import { describe, expect, it } from "vite-plus/test";
4
5import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
6import type { EnvironmentPullRequestEntry } from "./pullRequestList.logic";
7import { PullRequestRow } from "./PullRequestRow";
8import {
9 pullRequestChecksState,
10 pullRequestCheckStatusLabel,
11 summarizePullRequestChecks,
12} from "./pullRequestPresentation";
13
14function check(
15 status: PullRequestCheck["status"],
16 overrides: Partial<PullRequestCheck> = {},
17): PullRequestCheck {
18 return { name: `check-${status}`, status, description: null, url: null, ...overrides };
19}
20
21describe("pullRequestChecksState", () => {
22 it("lets a failure outrank a run still going, and reports nothing without checks", () => {
23 expect(pullRequestChecksState([check("success"), check("pending"), check("failure")])).toBe(
24 "failing",
25 );
26 expect(pullRequestChecksState([check("success"), check("cancelled")])).toBe("failing");
27 expect(pullRequestChecksState([check("success"), check("pending")])).toBe("pending");
28 expect(pullRequestChecksState([check("success"), check("action-required")])).toBe("pending");
29 expect(pullRequestChecksState([check("success")])).toBe("passing");
30 // Skipped and neutral are neither a pass nor a failure, so they are no verdict at all.
31 expect(pullRequestChecksState([check("skipped"), check("neutral")])).toBe(null);
32 expect(pullRequestChecksState([])).toBe(null);
33 });
34
35 it("names workflow approval instead of claiming every check passed", () => {
36 const workflow = check("action-required", {
37 url: "https://github.com/acme/web/actions/runs/42/job/7",
38 });
39 const manualGate = check("action-required", { url: "https://example.com/manual-gate" });
40 expect(pullRequestCheckStatusLabel(workflow)).toBe("Awaiting approval");
41 expect(pullRequestCheckStatusLabel(manualGate)).toBe("Awaiting action");
42 expect(summarizePullRequestChecks([check("success"), workflow])).toBe(
43 "1 workflow awaiting approval",
44 );
45 expect(summarizePullRequestChecks([check("failure"), workflow])).toBe("1 of 2 failing");
46 expect(summarizePullRequestChecks([check("success"), manualGate])).toBe(
47 "1 check awaiting action",
48 );
49 expect(summarizePullRequestChecks([workflow, manualGate])).toBe(
50 "1 workflow and 1 check awaiting action",
51 );
52 });
53});
54
55/** Every element of the tree the row returned, so a nested indicator can be looked for. */
56function flatten(node: ReactNode): ReadonlyArray<ReturnType<typeof Object>> {
57 const found: unknown[] = [];
58 for (const child of Children.toArray(node)) {
59 if (!isValidElement(child)) continue;
60 found.push(child);
61 found.push(...flatten((child.props as { readonly children?: ReactNode }).children));
62 }
63 return found as ReadonlyArray<ReturnType<typeof Object>>;
64}
65
66function entry(overrides: Partial<EnvironmentPullRequestEntry>): EnvironmentPullRequestEntry {
67 return {
68 environmentId: "env-1" as EnvironmentId,
69 projectId: "project-1" as ProjectId,
70 provider: "github",
71 repository: "pingdotgg/t3code",
72 number: 1,
73 title: "Add the pull requests page",
74 url: "https://github.com/pingdotgg/t3code/pull/1",
75 author: null,
76 headBranch: "feat/page",
77 baseBranch: "main",
78 state: "open",
79 isDraft: false,
80 mergeability: "mergeable",
81 additions: 1,
82 deletions: 0,
83 createdAt: "2026-07-01T00:00:00Z",
84 updatedAt: "2026-07-02T00:00:00Z",
85 viewerReviewRequested: false,
86 labels: [],
87 ...overrides,
88 } as EnvironmentPullRequestEntry;
89}
90
91function row(overrides: Partial<EnvironmentPullRequestEntry>): ReactNode {
92 return PullRequestRow.type({
93 entry: entry(overrides),
94 selected: false,
95 showProjectTitle: false,
96 showProvider: false,
97 onSelect: () => {},
98 });
99}
100
101describe("PullRequestRow checks indicator", () => {
102 function indicators(node: ReactNode): number {
103 return flatten(node).filter(
104 (element) => (element as { type?: unknown }).type === PullRequestChecksPopover,
105 ).length;
106 }
107
108 it("shows the indicator only for a row the host reported a rollup for", () => {
109 expect(indicators(row({ checksState: "failing" }))).toBe(1);
110 expect(indicators(row({}))).toBe(0);
111 });
112});