|
1 | 1 | import { describe, it, expect, afterEach } from "vitest"; |
2 | | -import { render, screen, cleanup } from "@testing-library/react"; |
| 2 | +import { render, screen, cleanup, waitFor, act, fireEvent } from "@testing-library/react"; |
3 | 3 | import { InsightsReportCard } from "./index.js"; |
4 | 4 | import type { InsightsReportView } from "./insightsStream.js"; |
5 | 5 |
|
@@ -50,4 +50,53 @@ describe("InsightsReportCard", () => { |
50 | 50 | render(<InsightsReportCard report={report} onContribute={() => {}} />); |
51 | 51 | expect(screen.getByText("Contribute to explore →")).toBeTruthy(); |
52 | 52 | }); |
| 53 | + |
| 54 | + const contributeReport: InsightsReportView = { |
| 55 | + totals: { sessions: 1, mostly: 1, partially: 0, not: 0 }, |
| 56 | + outcomes_summary: "1 session(s): 1 mostly achieved.", |
| 57 | + narrative: "You ship.", |
| 58 | + by_model: [], |
| 59 | + friction: [], |
| 60 | + publish_candidates: [{ sessionId: "c", goal: "deploy feature", why: "Succeeded" }], |
| 61 | + }; |
| 62 | + |
| 63 | + it("disables the Contribute button and shows Preparing… while the prepare call is in flight", async () => { |
| 64 | + let resolveContribute!: () => void; |
| 65 | + const onContribute = () => new Promise<void>((r) => { resolveContribute = r; }); |
| 66 | + |
| 67 | + render(<InsightsReportCard report={contributeReport} onContribute={onContribute} />); |
| 68 | + const btn = screen.getByText("Contribute to explore →") as HTMLButtonElement; |
| 69 | + expect(btn.disabled).toBe(false); |
| 70 | + |
| 71 | + fireEvent.click(btn); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + const preparing = screen.getByText("Preparing…") as HTMLButtonElement; |
| 75 | + expect(preparing).toBeTruthy(); |
| 76 | + expect(preparing.disabled).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + act(() => { resolveContribute(); }); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + const restored = screen.getByText("Contribute to explore →") as HTMLButtonElement; |
| 83 | + expect(restored.disabled).toBe(false); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("shows an error and does not navigate when prepare rejects", async () => { |
| 88 | + const originalHash = window.location.hash; |
| 89 | + const onContribute = () => Promise.reject(new Error("server blew up")); |
| 90 | + |
| 91 | + render(<InsightsReportCard report={contributeReport} onContribute={onContribute} />); |
| 92 | + const btn = screen.getByText("Contribute to explore →"); |
| 93 | + fireEvent.click(btn); |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(screen.getByText(/server blew up/)).toBeTruthy(); |
| 97 | + }); |
| 98 | + |
| 99 | + expect(window.location.hash).toBe(originalHash); |
| 100 | + expect((screen.getByText("Contribute to explore →") as HTMLButtonElement).disabled).toBe(false); |
| 101 | + }); |
53 | 102 | }); |
0 commit comments