|
| 1 | +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 3 | +import { TransactionPanel } from "./TransactionPanel"; |
| 4 | +import { getClient } from "@/lib/client"; |
| 5 | +import { useSorokit } from "@/context/useSorokit"; |
| 6 | + |
| 7 | +vi.mock("@/context/useSorokit", () => ({ |
| 8 | + useSorokit: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("@/lib/client", () => ({ |
| 12 | + getClient: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +describe("TransactionPanel", () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + (useSorokit as any).mockReturnValue({ |
| 19 | + address: "GABC", |
| 20 | + isConnected: true, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("handles loading, success, and error states", async () => { |
| 25 | + const mockSubmit = vi.fn().mockImplementation(() => { |
| 26 | + return new Promise(resolve => { |
| 27 | + setTimeout(() => resolve({ data: { hash: "txhash123", ledger: 100 }, error: null }), 50); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + (getClient as any).mockReturnValue({ |
| 32 | + transaction: { |
| 33 | + submit: mockSubmit, |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + render(<TransactionPanel />); |
| 38 | + |
| 39 | + const destInput = screen.getByLabelText("Destination Address"); |
| 40 | + const amountInput = screen.getByLabelText("Amount (XLM)"); |
| 41 | + const submitBtn = screen.getByRole("button", { name: "Send Payment" }); |
| 42 | + |
| 43 | + fireEvent.change(destInput, { target: { value: "GDEF" } }); |
| 44 | + fireEvent.change(amountInput, { target: { value: "10" } }); |
| 45 | + |
| 46 | + // Submit and check loading state |
| 47 | + fireEvent.click(submitBtn); |
| 48 | + expect(submitBtn).toBeDisabled(); |
| 49 | + expect(screen.getByRole("button", { name: "Submitting…" })).toBeInTheDocument(); |
| 50 | + |
| 51 | + // Check success state |
| 52 | + expect(await screen.findByText("Transaction submitted")).toBeInTheDocument(); |
| 53 | + expect(screen.getByText("Ledger #100")).toBeInTheDocument(); |
| 54 | + expect(screen.getByText("txhash123")).toBeInTheDocument(); |
| 55 | + |
| 56 | + // Test "New Transaction" button resets state |
| 57 | + const newTxBtn = screen.getByRole("button", { name: "New Transaction" }); |
| 58 | + fireEvent.click(newTxBtn); |
| 59 | + |
| 60 | + expect(screen.getByLabelText("Destination Address")).toHaveValue(""); |
| 61 | + expect(screen.getByLabelText("Amount (XLM)")).toHaveValue(null); |
| 62 | + }); |
| 63 | + |
| 64 | + it("handles error state", async () => { |
| 65 | + const mockSubmit = vi.fn().mockResolvedValue({ data: null, error: "Insufficient balance" }); |
| 66 | + |
| 67 | + (getClient as any).mockReturnValue({ |
| 68 | + transaction: { |
| 69 | + submit: mockSubmit, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + render(<TransactionPanel />); |
| 74 | + |
| 75 | + fireEvent.change(screen.getByLabelText("Destination Address"), { target: { value: "GDEF" } }); |
| 76 | + fireEvent.change(screen.getByLabelText("Amount (XLM)"), { target: { value: "10" } }); |
| 77 | + |
| 78 | + fireEvent.click(screen.getByRole("button", { name: "Send Payment" })); |
| 79 | + |
| 80 | + expect(await screen.findByText("Transaction failed")).toBeInTheDocument(); |
| 81 | + expect(screen.getByText("Insufficient balance")).toBeInTheDocument(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments