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

pullRequestChecks.test.tsx

85 lines3.2 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 { pullRequestChecksState } from "./pullRequestPresentation";
9​
10function check(status: PullRequestCheck["status"]): PullRequestCheck {
11 return { name: `check-${status}`, status, description: null, url: null };
13​
14describe("pullRequestChecksState", () => {
15 it("lets a failure outrank a run still going, and reports nothing without checks", () => {
16 expect(pullRequestChecksState([check("success"), check("pending"), check("failure")])).toBe(
17 "failing",
18 );
19 expect(pullRequestChecksState([check("success"), check("cancelled")])).toBe("failing");
20 expect(pullRequestChecksState([check("success"), check("pending")])).toBe("pending");
21 expect(pullRequestChecksState([check("success")])).toBe("passing");
22 // Skipped and neutral are neither a pass nor a failure, so they are no verdict at all.
23 expect(pullRequestChecksState([check("skipped"), check("neutral")])).toBe(null);
24 expect(pullRequestChecksState([])).toBe(null);
25 });
26});
27​
28/** Every element of the tree the row returned, so a nested indicator can be looked for. */
29function flatten(node: ReactNode): ReadonlyArray<ReturnType<typeof Object>> {
30 const found: unknown[] = [];
31 for (const child of Children.toArray(node)) {
32 if (!isValidElement(child)) continue;
33 found.push(child);
34 found.push(...flatten((child.props as { readonly children?: ReactNode }).children));
35 }
36 return found as ReadonlyArray<ReturnType<typeof Object>>;
38​
39function entry(overrides: Partial<EnvironmentPullRequestEntry>): EnvironmentPullRequestEntry {
40 return {
41 environmentId: "env-1" as EnvironmentId,
42 projectId: "project-1" as ProjectId,
43 provider: "github",
44 repository: "pingdotgg/t3code",
45 number: 1,
46 title: "Add the pull requests page",
47 url: "https://github.com/pingdotgg/t3code/pull/1",
48 author: null,
49 headBranch: "feat/page",
50 baseBranch: "main",
51 state: "open",
52 isDraft: false,
53 mergeability: "mergeable",
54 additions: 1,
55 deletions: 0,
56 createdAt: "2026-07-01T00:00:00Z",
57 updatedAt: "2026-07-02T00:00:00Z",
58 viewerReviewRequested: false,
59 labels: [],
60 ...overrides,
61 } as EnvironmentPullRequestEntry;
63​
64function row(overrides: Partial<EnvironmentPullRequestEntry>): ReactNode {
65 return PullRequestRow.type({
66 entry: entry(overrides),
67 selected: false,
68 showProjectTitle: false,
69 showProvider: false,
70 onSelect: () => {},
71 });
73​
74describe("PullRequestRow checks indicator", () => {
75 function indicators(node: ReactNode): number {
76 return flatten(node).filter(
77 (element) => (element as { type?: unknown }).type === PullRequestChecksPopover,
78 ).length;
79 }
80​
81 it("shows the indicator only for a row the host reported a rollup for", () => {
82 expect(indicators(row({ checksState: "failing" }))).toBe(1);
83 expect(indicators(row({}))).toBe(0);
84 });
85});