|
| 1 | +/** |
| 2 | + * CollisionPill — color-state pill per spec §4.6.1. |
| 3 | + * |
| 4 | + * Branches under test: |
| 5 | + * - 0 groups: gray, "no candidate duplicates" |
| 6 | + * - N groups, 0 verified: blue, "N candidate group(s)" |
| 7 | + * - 1 group, 0 verified: blue, "1 candidate group" (singular) |
| 8 | + * - N groups, 0 less than M less than N verified: blue, "N candidate (M ✓)" |
| 9 | + * - all verified: green, "all verified ✓" |
| 10 | + * |
| 11 | + * WHY mock `@tanstack/react-router`: CollisionPill renders a Link to="/dedup" |
| 12 | + * which requires a router context. Unit tests for a leaf pill component do not |
| 13 | + * need full router routing; mocking Link as a plain anchor keeps the test |
| 14 | + * focused on color-state rendering without router scaffolding. |
| 15 | + */ |
| 16 | +import { describe, it, expect, vi } from "vitest"; |
| 17 | +import { screen } from "@testing-library/react"; |
| 18 | +import CollisionPill from "../components/CollisionPill"; |
| 19 | +import type { CollisionGroup } from "../bindings"; |
| 20 | +import { renderWithProviders } from "./test-utils"; |
| 21 | + |
| 22 | +// WHY mock Link: avoids RouterProvider scaffolding for a pure leaf component. |
| 23 | +// The href is set to `to` so tests can assert navigation target if needed. |
| 24 | +vi.mock("@tanstack/react-router", () => ({ |
| 25 | + Link: ({ |
| 26 | + to, |
| 27 | + className, |
| 28 | + children, |
| 29 | + title, |
| 30 | + }: { |
| 31 | + to: string; |
| 32 | + className?: string; |
| 33 | + children: React.ReactNode; |
| 34 | + title?: string; |
| 35 | + }) => ( |
| 36 | + <a href={to} className={className} title={title}> |
| 37 | + {children} |
| 38 | + </a> |
| 39 | + ), |
| 40 | +})); |
| 41 | + |
| 42 | +// ── Helpers ────────────────────────────────────────────────────────────────── |
| 43 | + |
| 44 | +function makeGroup( |
| 45 | + quickHash: string, |
| 46 | + verifiedState: CollisionGroup["verified_state"], |
| 47 | +): CollisionGroup { |
| 48 | + return { |
| 49 | + quick_hash: quickHash, |
| 50 | + files: [], |
| 51 | + verified_state: verifiedState, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +// ── Tests ───────────────────────────────────────────────────────────────────── |
| 56 | + |
| 57 | +describe("CollisionPill", () => { |
| 58 | + it("renders gray 'no candidate duplicates' when groups is empty", () => { |
| 59 | + renderWithProviders(<CollisionPill groups={[]} />); |
| 60 | + const el = screen.getByText(/no candidate duplicates/i); |
| 61 | + expect(el).toBeInTheDocument(); |
| 62 | + // WHY check class presence: gray state = text-gray-500, not a link. |
| 63 | + expect(el.tagName).toBe("SPAN"); |
| 64 | + expect(el.className).toContain("text-gray-500"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("renders blue pill for N unverified groups (plural)", () => { |
| 68 | + const groups = [ |
| 69 | + makeGroup("aaa", "Unverified"), |
| 70 | + makeGroup("bbb", "Unverified"), |
| 71 | + makeGroup("ccc", "Unverified"), |
| 72 | + ]; |
| 73 | + renderWithProviders(<CollisionPill groups={groups} />); |
| 74 | + const link = screen.getByRole("link"); |
| 75 | + expect(link).toBeInTheDocument(); |
| 76 | + expect(link.textContent).toMatch(/3 candidate groups/i); |
| 77 | + expect(link.className).toContain("text-blue-400"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("renders blue pill with singular 'group' label for 1 unverified group", () => { |
| 81 | + renderWithProviders(<CollisionPill groups={[makeGroup("aaa", "Unverified")]} />); |
| 82 | + const link = screen.getByRole("link"); |
| 83 | + expect(link.textContent).toMatch(/1 candidate group$/i); |
| 84 | + // Must NOT be plural. |
| 85 | + expect(link.textContent).not.toMatch(/1 candidate groups/i); |
| 86 | + expect(link.className).toContain("text-blue-400"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("renders blue pill with verified count when 0 < M < N verified", () => { |
| 90 | + const groups = [ |
| 91 | + makeGroup("aaa", "VerifiedDuplicate"), |
| 92 | + makeGroup("bbb", "Unverified"), |
| 93 | + makeGroup("ccc", "VerifiedDistinct"), |
| 94 | + ]; |
| 95 | + renderWithProviders(<CollisionPill groups={groups} />); |
| 96 | + const link = screen.getByRole("link"); |
| 97 | + // 3 total, 2 verified (VerifiedDuplicate + VerifiedDistinct both count). |
| 98 | + expect(link.textContent).toMatch(/3 candidates \(2 ✓\)/i); |
| 99 | + expect(link.className).toContain("text-blue-400"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("renders green pill when all groups are verified", () => { |
| 103 | + const groups = [ |
| 104 | + makeGroup("aaa", "VerifiedDuplicate"), |
| 105 | + makeGroup("bbb", "VerifiedDistinct"), |
| 106 | + ]; |
| 107 | + renderWithProviders(<CollisionPill groups={groups} />); |
| 108 | + const link = screen.getByRole("link"); |
| 109 | + expect(link.textContent).toMatch(/all verified ✓/i); |
| 110 | + expect(link.className).toContain("text-green-400"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("link points to /dedup", () => { |
| 114 | + renderWithProviders(<CollisionPill groups={[makeGroup("aaa", "Unverified")]} />); |
| 115 | + const link = screen.getByRole("link"); |
| 116 | + expect(link).toHaveAttribute("href", "/dedup"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("Mixed state counts as unverified (not fully verified)", () => { |
| 120 | + const groups = [makeGroup("aaa", "Mixed")]; |
| 121 | + renderWithProviders(<CollisionPill groups={groups} />); |
| 122 | + const link = screen.getByRole("link"); |
| 123 | + // Mixed is not VerifiedDuplicate or VerifiedDistinct → not in verified count. |
| 124 | + // With 0 verified, label = "1 candidate group". |
| 125 | + expect(link.textContent).toMatch(/1 candidate group$/i); |
| 126 | + expect(link.className).toContain("text-blue-400"); |
| 127 | + }); |
| 128 | +}); |
0 commit comments