|
| 1 | +import { createElement } from "react"; |
| 2 | +import { act, create } from "react-test-renderer"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import type { PrStatus } from "../hooks/usePrStatus"; |
| 5 | +import { PrStatusBadge } from "./PrStatusBadge"; |
| 6 | + |
| 7 | +vi.mock("phosphor-react-native", () => ({ |
| 8 | + GitMerge: (props: Record<string, unknown>) => |
| 9 | + createElement("GitMerge", props), |
| 10 | + GitPullRequest: (props: Record<string, unknown>) => |
| 11 | + createElement("GitPullRequest", props), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("@/lib/theme", () => ({ |
| 15 | + useThemeColors: () => ({ |
| 16 | + gray: { 11: "#444444" }, |
| 17 | + status: { success: "#00aa00", error: "#cc0000" }, |
| 18 | + }), |
| 19 | + toRgba: (hex: string, alpha: number) => `${hex}/${alpha}`, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("@/lib/openExternalUrl", () => ({ openExternalUrl: vi.fn() })); |
| 23 | + |
| 24 | +vi.mock("../hooks/usePrStatus", () => ({ usePrStatus: vi.fn() })); |
| 25 | + |
| 26 | +import { usePrStatus } from "../hooks/usePrStatus"; |
| 27 | + |
| 28 | +const mockUsePrStatus = vi.mocked(usePrStatus); |
| 29 | + |
| 30 | +function setStatus(data: PrStatus | null | undefined) { |
| 31 | + mockUsePrStatus.mockReturnValue({ data } as ReturnType<typeof usePrStatus>); |
| 32 | +} |
| 33 | + |
| 34 | +function render(props: Parameters<typeof PrStatusBadge>[0]) { |
| 35 | + let renderer: ReturnType<typeof create> | null = null; |
| 36 | + act(() => { |
| 37 | + renderer = create(createElement(PrStatusBadge, props)); |
| 38 | + }); |
| 39 | + if (!renderer) throw new Error("Renderer not created"); |
| 40 | + return renderer as ReturnType<typeof create>; |
| 41 | +} |
| 42 | + |
| 43 | +function label(renderer: ReturnType<typeof create>): string | undefined { |
| 44 | + const node = renderer.root.findAll( |
| 45 | + (n) => typeof n.props?.accessibilityLabel === "string", |
| 46 | + )[0]; |
| 47 | + return node?.props.accessibilityLabel as string | undefined; |
| 48 | +} |
| 49 | + |
| 50 | +function iconCount(renderer: ReturnType<typeof create>, type: string): number { |
| 51 | + return renderer.root.findAll((n) => n.type === type).length; |
| 52 | +} |
| 53 | + |
| 54 | +const base: PrStatus = { |
| 55 | + state: "open", |
| 56 | + merged: false, |
| 57 | + draft: false, |
| 58 | + additions: 0, |
| 59 | + deletions: 0, |
| 60 | +}; |
| 61 | + |
| 62 | +describe("PrStatusBadge", () => { |
| 63 | + it("renders an open PR badge", () => { |
| 64 | + setStatus({ ...base, state: "open" }); |
| 65 | + const r = render({ prUrl: "https://github.com/a/b/pull/1" }); |
| 66 | + expect(label(r)).toBe("Open PR"); |
| 67 | + expect(iconCount(r, "GitPullRequest")).toBe(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it("renders a merged PR badge with the merge icon", () => { |
| 71 | + setStatus({ ...base, state: "closed", merged: true }); |
| 72 | + const r = render({ prUrl: "https://github.com/a/b/pull/1" }); |
| 73 | + expect(label(r)).toBe("Open merged PR"); |
| 74 | + expect(iconCount(r, "GitMerge")).toBe(1); |
| 75 | + expect(iconCount(r, "GitPullRequest")).toBe(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it("renders a closed PR badge", () => { |
| 79 | + setStatus({ ...base, state: "closed" }); |
| 80 | + const r = render({ prUrl: "https://github.com/a/b/pull/1" }); |
| 81 | + expect(label(r)).toBe("Open closed PR"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("renders a draft PR badge", () => { |
| 85 | + setStatus({ ...base, state: "open", draft: true }); |
| 86 | + const r = render({ prUrl: "https://github.com/a/b/pull/1" }); |
| 87 | + expect(label(r)).toBe("Open draft PR"); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each([ |
| 91 | + { data: undefined, label: "loading" }, |
| 92 | + { data: null, label: "unresolved (private/404/non-GitHub)" }, |
| 93 | + ])( |
| 94 | + "renders nothing when hideWhenUnresolved is set and status is $label", |
| 95 | + ({ data }) => { |
| 96 | + setStatus(data); |
| 97 | + const r = render({ |
| 98 | + prUrl: "https://github.com/a/b/pull/1", |
| 99 | + hideWhenUnresolved: true, |
| 100 | + }); |
| 101 | + expect(r.toJSON()).toBeNull(); |
| 102 | + }, |
| 103 | + ); |
| 104 | + |
| 105 | + it("still renders a neutral badge for an unresolved PR by default", () => { |
| 106 | + setStatus(null); |
| 107 | + const r = render({ prUrl: "https://github.com/a/b/pull/1" }); |
| 108 | + expect(r.toJSON()).not.toBeNull(); |
| 109 | + expect(label(r)).toBe("Open PR"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments