|
| 1 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import NewPairPage from "./page"; |
| 3 | + |
| 4 | +const mockPush = jest.fn(); |
| 5 | + |
| 6 | +jest.mock("next/navigation", () => ({ |
| 7 | + useRouter: () => ({ push: mockPush }), |
| 8 | +})); |
| 9 | + |
| 10 | +describe("NewPairPage", () => { |
| 11 | + let originalFetch: typeof globalThis.fetch; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + originalFetch = globalThis.fetch; |
| 15 | + mockPush.mockReset(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + globalThis.fetch = originalFetch; |
| 20 | + }); |
| 21 | + |
| 22 | + function submitPair(source: string, destination: string) { |
| 23 | + fireEvent.change(screen.getByLabelText("Source"), { |
| 24 | + target: { value: source }, |
| 25 | + }); |
| 26 | + fireEvent.change(screen.getByLabelText("Destination"), { |
| 27 | + target: { value: destination }, |
| 28 | + }); |
| 29 | + fireEvent.submit(screen.getByRole("button", { name: /Register pair/i }).closest("form")!); |
| 30 | + } |
| 31 | + |
| 32 | + it("normalizes lowercase and surrounding whitespace before submit", async () => { |
| 33 | + const mockFetch = jest.fn().mockResolvedValueOnce({ |
| 34 | + ok: true, |
| 35 | + text: async () => "{}", |
| 36 | + } as unknown as Response); |
| 37 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 38 | + |
| 39 | + render(<NewPairPage />); |
| 40 | + submitPair(" usdc ", " eurc "); |
| 41 | + |
| 42 | + await waitFor(() => { |
| 43 | + expect(mockPush).toHaveBeenCalledWith("/pairs"); |
| 44 | + }); |
| 45 | + const requestInit = mockFetch.mock.calls[0][1] as RequestInit; |
| 46 | + expect(requestInit.method).toBe("POST"); |
| 47 | + expect(JSON.parse(requestInit.body as string)).toEqual({ |
| 48 | + source: "USDC", |
| 49 | + destination: "EURC", |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it.each(["USD-C", "USD C", "ABCDEFGHIJKLM"])( |
| 54 | + "rejects invalid source asset code %s with accessible field errors", |
| 55 | + async (code) => { |
| 56 | + const mockFetch = jest.fn(); |
| 57 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 58 | + |
| 59 | + render(<NewPairPage />); |
| 60 | + submitPair(code, "EURC"); |
| 61 | + |
| 62 | + const sourceInput = document.getElementById("source"); |
| 63 | + await waitFor(() => { |
| 64 | + expect(screen.getByRole("alert")).toHaveTextContent(/ASCII letters or numbers/i); |
| 65 | + }); |
| 66 | + expect(sourceInput).toHaveAttribute("aria-invalid", "true"); |
| 67 | + expect(sourceInput).toHaveAttribute("aria-describedby", "source-err"); |
| 68 | + expect(mockFetch).not.toHaveBeenCalled(); |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + it("rejects pairs that are identical after trimming and uppercasing", async () => { |
| 73 | + const mockFetch = jest.fn(); |
| 74 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 75 | + |
| 76 | + render(<NewPairPage />); |
| 77 | + submitPair(" usdc ", "USDC"); |
| 78 | + |
| 79 | + const destinationInput = document.getElementById("destination"); |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.getByRole("alert")).toHaveTextContent(/must differ/i); |
| 82 | + }); |
| 83 | + expect(destinationInput).toHaveAttribute("aria-invalid", "true"); |
| 84 | + expect(destinationInput).toHaveAttribute("aria-describedby", "destination-err"); |
| 85 | + expect(mockFetch).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("validates empty fields with inline errors instead of native browser validation", async () => { |
| 89 | + const mockFetch = jest.fn(); |
| 90 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 91 | + |
| 92 | + render(<NewPairPage />); |
| 93 | + fireEvent.submit(screen.getByRole("button", { name: /Register pair/i }).closest("form")!); |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(screen.getAllByRole("alert")).toHaveLength(2); |
| 97 | + }); |
| 98 | + expect(document.getElementById("source")).toHaveAttribute("aria-invalid", "true"); |
| 99 | + expect(document.getElementById("destination")).toHaveAttribute("aria-invalid", "true"); |
| 100 | + expect(mockFetch).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("clears an identical-pair error when source changes", async () => { |
| 104 | + const mockFetch = jest.fn(); |
| 105 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 106 | + |
| 107 | + render(<NewPairPage />); |
| 108 | + submitPair("USDC", "USDC"); |
| 109 | + |
| 110 | + await waitFor(() => { |
| 111 | + expect(screen.getByRole("alert")).toHaveTextContent(/must differ/i); |
| 112 | + }); |
| 113 | + fireEvent.change(screen.getByLabelText("Source"), { |
| 114 | + target: { value: "XLM" }, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(screen.queryByRole("alert")).not.toBeInTheDocument(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("surfaces backend errors without losing the normalized request body", async () => { |
| 121 | + const mockFetch = jest.fn().mockResolvedValueOnce({ |
| 122 | + ok: false, |
| 123 | + text: async () => |
| 124 | + JSON.stringify({ |
| 125 | + error: "invalid_request", |
| 126 | + message: "Pair already exists", |
| 127 | + }), |
| 128 | + } as unknown as Response); |
| 129 | + globalThis.fetch = mockFetch as unknown as typeof globalThis.fetch; |
| 130 | + |
| 131 | + render(<NewPairPage />); |
| 132 | + submitPair("xlm", "usdc"); |
| 133 | + |
| 134 | + await waitFor(() => { |
| 135 | + expect(screen.getByRole("alert")).toHaveTextContent(/Pair already exists/i); |
| 136 | + }); |
| 137 | + const requestInit = mockFetch.mock.calls[0][1] as RequestInit; |
| 138 | + expect(JSON.parse(requestInit.body as string)).toEqual({ |
| 139 | + source: "XLM", |
| 140 | + destination: "USDC", |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments