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

pullRequestChecks.test.tsx

116 lines4.3 KB

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 };
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 expect(pullRequestChecksState([check("skipped"), check("neutral")])).toBe(null);
31 expect(pullRequestChecksState([])).toBe(null);
32 });
33​
34 it("names workflow approval instead of claiming every check passed", () => {
35 const workflow = check("action-required", {
36 url: "https://github.com/acme/web/actions/runs/42/job/7",
37 });
38 const manualGate = check("action-required", { url: "https://example.com/manual-gate" });
39 expect(pullRequestCheckStatusLabel(workflow)).toBe("Awaiting approval");
40 expect(pullRequestCheckStatusLabel(manualGate)).toBe("Awaiting action");
41 expect(summarizePullRequestChecks([check("success"), workflow])).toBe(
42 "1 workflow awaiting approval",
43 );
44 expect(summarizePullRequestChecks([check("failure"), workflow])).toBe("1 of 2 failing");
45 expect(summarizePullRequestChecks([check("success"), manualGate])).toBe(
46 "1 check awaiting action",
47 );
48 expect(summarizePullRequestChecks([workflow, manualGate])).toBe(
49 "1 workflow and 1 check awaiting action",
50 );
51 });
52});
53​
54function flatten(node: ReactNode): ReadonlyArray<ReturnType<typeof Object>> {
55 const found: unknown[] = [];
56 for (const child of Children.toArray(node)) {
57 if (!isValidElement(child)) continue;
58 found.push(child);
59 for (const value of Object.values(child.props as Record<string, unknown>)) {
60 if (isValidElement(value)) found.push(...flatten(value));
61 else if (Array.isArray(value)) found.push(...flatten(value.filter(isValidElement)));
62 }
63 }
64 return found as ReadonlyArray<ReturnType<typeof Object>>;
66​
67function entry(overrides: Partial<EnvironmentPullRequestEntry>): EnvironmentPullRequestEntry {
68 return {
69 environmentId: "env-1" as EnvironmentId,
70 projectId: "project-1" as ProjectId,
71 provider: "github",
72 repository: "pingdotgg/t3code",
73 number: 1,
74 title: "Add the pull requests page",
75 url: "https://github.com/pingdotgg/t3code/pull/1",
76 author: null,
77 headBranch: "feat/page",
78 baseBranch: "main",
79 state: "open",
80 isDraft: false,
81 mergeability: "mergeable",
82 additions: 1,
83 deletions: 0,
84 createdAt: "2026-07-01T00:00:00Z",
85 updatedAt: "2026-07-02T00:00:00Z",
86 viewerReviewRequested: false,
87 labels: [],
88 ...overrides,
89 } as EnvironmentPullRequestEntry;
91​
92function row(overrides: Partial<EnvironmentPullRequestEntry>): ReactNode {
93 return PullRequestRow.type({
94 entry: entry(overrides),
95 selected: false,
96 showProjectTitle: false,
97 showProvider: false,
98 onSelect: () => {},
99 speedMode: false,
100 hostActions: new Set(),
101 onActed: () => {},
102 });
104​
105describe("PullRequestRow checks indicator", () => {
106 function indicators(node: ReactNode): number {
107 return flatten(node).filter(
108 (element) => (element as { type?: unknown }).type === PullRequestChecksPopover,
109 ).length;
110 }
111​
112 it("shows the indicator only for a row the host reported a rollup for", () => {
113 expect(indicators(row({ checksState: "failing" }))).toBe(1);
114 expect(indicators(row({}))).toBe(0);
115 });
116});