|
| 1 | +import React from "react"; |
| 2 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import PayModal, { getPaymentErrorMessage } from "@/components/PayModal"; |
| 4 | +import type { Invoice } from "@stellar-split/sdk"; |
| 5 | + |
| 6 | +const SCALE = 10_000_000n; |
| 7 | + |
| 8 | +jest.mock("@stellar-split/sdk", () => ({ |
| 9 | + formatAmount: (value: bigint) => (Number(value) / 10_000_000).toFixed(2), |
| 10 | + parseAmount: (value: string) => BigInt(Math.round(Number(value) * 10_000_000)), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock("@/lib/stellar", () => ({ |
| 14 | + USDC_CONTRACT_ID: "CUSDC", |
| 15 | + fetchUsdcBalance: jest.fn().mockResolvedValue(1_000_000_000n), |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock("@/components/FocusTrap", () => ({ |
| 19 | + __esModule: true, |
| 20 | + default: function FocusTrap({ children }: { children: React.ReactNode }) { |
| 21 | + return <>{children}</>; |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +const invoice: Invoice = { |
| 26 | + id: "inv-286", |
| 27 | + creator: "GCREATOR", |
| 28 | + recipients: [{ address: "GPAYER", amount: 50n * SCALE }], |
| 29 | + token: "CUSDC", |
| 30 | + deadline: 0, |
| 31 | + funded: 25n * SCALE, |
| 32 | + status: "Pending", |
| 33 | + payments: [{ payer: "GPAYER", amount: 10n * SCALE }], |
| 34 | +}; |
| 35 | + |
| 36 | +describe("PayModal", () => { |
| 37 | + it("defaults to remaining share, shows balance, pays with tip, and renders explorer link", async () => { |
| 38 | + const onPay = jest.fn().mockResolvedValue({ txHash: "abc123hash" }); |
| 39 | + |
| 40 | + render( |
| 41 | + <PayModal |
| 42 | + invoice={invoice} |
| 43 | + total={100n * SCALE} |
| 44 | + publicKey="GPAYER" |
| 45 | + onPay={onPay} |
| 46 | + onClose={jest.fn()} |
| 47 | + /> |
| 48 | + ); |
| 49 | + |
| 50 | + const amountInput = await screen.findByLabelText(/amount \(usdc\)/i); |
| 51 | + expect(amountInput).toHaveValue(40); |
| 52 | + expect(await screen.findByText(/100.00 USDC available/i)).toBeInTheDocument(); |
| 53 | + |
| 54 | + fireEvent.change(screen.getByLabelText(/tip \(optional\)/i), { |
| 55 | + target: { value: "2" }, |
| 56 | + }); |
| 57 | + fireEvent.click(screen.getByLabelText(/donate on failure/i)); |
| 58 | + fireEvent.click(screen.getByRole("button", { name: /review & pay/i })); |
| 59 | + fireEvent.click(screen.getByRole("button", { name: /confirm & pay/i })); |
| 60 | + |
| 61 | + await waitFor(() => |
| 62 | + expect(onPay).toHaveBeenCalledWith( |
| 63 | + 42n * SCALE, |
| 64 | + undefined, |
| 65 | + expect.objectContaining({ tip: 2n * SCALE, donateOnFailure: true }) |
| 66 | + ) |
| 67 | + ); |
| 68 | + expect(await screen.findByText(/payment confirmed/i)).toBeInTheDocument(); |
| 69 | + expect(screen.getByRole("link", { name: /view on stellar expert/i })).toHaveAttribute( |
| 70 | + "href", |
| 71 | + expect.stringContaining("/abc123hash") |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("maps known payment errors to human-readable messages", () => { |
| 76 | + expect(getPaymentErrorMessage("insufficient balance")).toMatch(/balance is too low/i); |
| 77 | + expect(getPaymentErrorMessage("invoice closed")).toMatch(/no longer accepting payments/i); |
| 78 | + expect(getPaymentErrorMessage("user rejected request")).toMatch(/signing request was cancelled/i); |
| 79 | + }); |
| 80 | +}); |
0 commit comments