|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { vi } from "vitest"; |
| 3 | +import SubscriptionDetailClient from "@/components/SubscriptionDetailClient"; |
| 4 | +import type { Subscription } from "@/types/subscription"; |
| 5 | + |
| 6 | +vi.mock("@stellar-split/sdk", async (importOriginal) => { |
| 7 | + const actual = await importOriginal<typeof import("@stellar-split/sdk")>(); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + formatAmount: (n: bigint) => `${n / 1000000n}`, |
| 11 | + truncateAddress: (s: string) => `${s.slice(0, 4)}...${s.slice(-4)}`, |
| 12 | + }; |
| 13 | +}); |
| 14 | + |
| 15 | +vi.mock("next/link", () => ({ |
| 16 | + default: ({ children, href, ...props }: React.ComponentProps<"a"> & { href: string }) => ( |
| 17 | + <a href={href} {...props}> |
| 18 | + {children} |
| 19 | + </a> |
| 20 | + ), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("next/navigation", () => ({ |
| 24 | + useParams: () => ({ id: "sub-1" }), |
| 25 | + useRouter: () => ({ replace: vi.fn() }), |
| 26 | +})); |
| 27 | + |
| 28 | +const mockSubscription: Subscription = { |
| 29 | + id: "sub-1", |
| 30 | + templateName: "Weekly Design Retainer", |
| 31 | + creator: "GCREADER1234567890ABCDEF", |
| 32 | + recipients: [ |
| 33 | + { address: "GDEST1234567890ABCDEF", amount: 100000000n }, |
| 34 | + ], |
| 35 | + frequency: "weekly", |
| 36 | + intervalDays: 7, |
| 37 | + status: "active", |
| 38 | + createdAt: Math.floor(Date.now() / 1000) - 86400 * 14, |
| 39 | + nextRunDate: Math.floor(Date.now() / 1000) + 86400 * 3, |
| 40 | + lastRunDate: Math.floor(Date.now() / 1000) - 86400 * 4, |
| 41 | + token: "USDC", |
| 42 | + totalInvoicesGenerated: 2, |
| 43 | + totalUsdcCollected: 200000000n, |
| 44 | + invoiceHistory: [ |
| 45 | + { |
| 46 | + invoiceId: "201", |
| 47 | + generatedAt: Math.floor(Date.now() / 1000) - 86400 * 14, |
| 48 | + deadline: Math.floor(Date.now() / 1000) - 86400 * 10, |
| 49 | + amount: 100000000n, |
| 50 | + status: "Released", |
| 51 | + }, |
| 52 | + { |
| 53 | + invoiceId: "202", |
| 54 | + generatedAt: Math.floor(Date.now() / 1000) - 86400 * 7, |
| 55 | + deadline: Math.floor(Date.now() / 1000) - 86400 * 3, |
| 56 | + amount: 100000000n, |
| 57 | + status: "Pending", |
| 58 | + }, |
| 59 | + ], |
| 60 | +}; |
| 61 | + |
| 62 | +describe("SubscriptionDetailClient", () => { |
| 63 | + beforeEach(() => { |
| 64 | + localStorage.clear(); |
| 65 | + localStorage.setItem( |
| 66 | + "stellar_split_subscriptions", |
| 67 | + JSON.stringify([ |
| 68 | + { |
| 69 | + ...mockSubscription, |
| 70 | + recipients: mockSubscription.recipients.map((r) => ({ |
| 71 | + ...r, |
| 72 | + amount: r.amount.toString(), |
| 73 | + })), |
| 74 | + totalUsdcCollected: mockSubscription.totalUsdcCollected.toString(), |
| 75 | + invoiceHistory: mockSubscription.invoiceHistory.map((inv) => ({ |
| 76 | + ...inv, |
| 77 | + amount: inv.amount.toString(), |
| 78 | + })), |
| 79 | + }, |
| 80 | + ]) |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("renders subscription template name", async () => { |
| 85 | + render(<SubscriptionDetailClient />); |
| 86 | + const els = await screen.findAllByText("Weekly Design Retainer"); |
| 87 | + expect(els.length).toBeGreaterThanOrEqual(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it("renders subscription status", async () => { |
| 91 | + render(<SubscriptionDetailClient />); |
| 92 | + expect(await screen.findByText("Active")).toBeInTheDocument(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("renders frequency", async () => { |
| 96 | + render(<SubscriptionDetailClient />); |
| 97 | + expect(await screen.findByText("Weekly")).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("renders pause button for active subscription", async () => { |
| 101 | + render(<SubscriptionDetailClient />); |
| 102 | + expect(await screen.findByText("Pause Subscription")).toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("renders cancel button", async () => { |
| 106 | + render(<SubscriptionDetailClient />); |
| 107 | + expect(await screen.findByText("Cancel Subscription")).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("renders invoice history table", async () => { |
| 111 | + render(<SubscriptionDetailClient />); |
| 112 | + expect(await screen.findByText(/Invoice History/)).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("renders recipients section", async () => { |
| 116 | + render(<SubscriptionDetailClient />); |
| 117 | + expect(await screen.findByText("Recipients")).toBeInTheDocument(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("renders calendar preview for active subscription", async () => { |
| 121 | + render(<SubscriptionDetailClient />); |
| 122 | + expect(await screen.findByText("Upcoming Invoice Dates")).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("renders total invoices count", async () => { |
| 126 | + render(<SubscriptionDetailClient />); |
| 127 | + expect(await screen.findByText("Total Invoices")).toBeInTheDocument(); |
| 128 | + }); |
| 129 | + |
| 130 | + it("renders back link", async () => { |
| 131 | + render(<SubscriptionDetailClient />); |
| 132 | + const backLink = await screen.findByText("← Subscriptions"); |
| 133 | + expect(backLink).toHaveAttribute("href", "/subscriptions"); |
| 134 | + }); |
| 135 | +}); |
0 commit comments