|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import type React from "react"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 6 | +import { prepare as prepareOnramp } from "../../../../bridge/Onramp.js"; |
| 7 | +import { getToken } from "../../../../pay/convert/get-token.js"; |
| 8 | +import { useBuyWithFiatQuotesForProviders } from "./useBuyWithFiatQuotesForProviders.js"; |
| 9 | + |
| 10 | +vi.mock("../../../../bridge/Onramp.js"); |
| 11 | +vi.mock("../../../../pay/convert/get-token.js"); |
| 12 | + |
| 13 | +const RECEIVER = "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709" as const; |
| 14 | +const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" as const; |
| 15 | + |
| 16 | +describe("useBuyWithFiatQuotesForProviders", () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + vi.mocked(getToken).mockResolvedValue({ |
| 20 | + address: USDC, |
| 21 | + chainId: 1, |
| 22 | + decimals: 6, |
| 23 | + name: "USD Coin", |
| 24 | + priceUsd: 1, |
| 25 | + prices: { USD: 1 }, |
| 26 | + symbol: "USDC", |
| 27 | + }); |
| 28 | + // Return a minimal valid prepared-onramp response per call. |
| 29 | + vi.mocked(prepareOnramp).mockImplementation(async (opts) => ({ |
| 30 | + currency: opts.currency ?? "USD", |
| 31 | + currencyAmount: 1, |
| 32 | + destinationAmount: opts.amount ?? 1n, |
| 33 | + destinationToken: { |
| 34 | + address: opts.tokenAddress, |
| 35 | + chainId: opts.chainId, |
| 36 | + decimals: 6, |
| 37 | + name: "USD Coin", |
| 38 | + priceUsd: 1, |
| 39 | + prices: { USD: 1 }, |
| 40 | + symbol: "USDC", |
| 41 | + }, |
| 42 | + id: `mock-${opts.onramp}`, |
| 43 | + intent: { |
| 44 | + amount: (opts.amount ?? 1n).toString(), |
| 45 | + chainId: opts.chainId, |
| 46 | + onramp: opts.onramp, |
| 47 | + receiver: opts.receiver, |
| 48 | + tokenAddress: opts.tokenAddress, |
| 49 | + }, |
| 50 | + link: `https://example.com/${opts.onramp}`, |
| 51 | + steps: [], |
| 52 | + timestamp: 0, |
| 53 | + })); |
| 54 | + }); |
| 55 | + |
| 56 | + const wrapper = ({ children }: { children: React.ReactNode }) => { |
| 57 | + const queryClient = new QueryClient({ |
| 58 | + defaultOptions: { queries: { retry: false } }, |
| 59 | + }); |
| 60 | + return ( |
| 61 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + it("fans out to all four onramp providers (including rampnow)", async () => { |
| 66 | + const { result } = renderHook( |
| 67 | + () => |
| 68 | + useBuyWithFiatQuotesForProviders({ |
| 69 | + amount: "10", |
| 70 | + chainId: 1, |
| 71 | + client: TEST_CLIENT, |
| 72 | + country: "US", |
| 73 | + currency: "USD", |
| 74 | + receiver: RECEIVER, |
| 75 | + tokenAddress: USDC, |
| 76 | + }), |
| 77 | + { wrapper }, |
| 78 | + ); |
| 79 | + |
| 80 | + await waitFor(() => { |
| 81 | + expect(result.current.every((q) => q.isSuccess)).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.current).toHaveLength(4); |
| 85 | + |
| 86 | + // Each provider should be called exactly once with the expected `onramp` value. |
| 87 | + const calls = vi.mocked(prepareOnramp).mock.calls.map((c) => c[0].onramp); |
| 88 | + expect(calls.sort()).toEqual( |
| 89 | + ["coinbase", "rampnow", "stripe", "transak"].sort(), |
| 90 | + ); |
| 91 | + |
| 92 | + // The hook should surface the prepared link per provider. |
| 93 | + const links = result.current.map((q) => q.data?.link); |
| 94 | + expect(links).toContain("https://example.com/rampnow"); |
| 95 | + expect(links).toContain("https://example.com/stripe"); |
| 96 | + expect(links).toContain("https://example.com/coinbase"); |
| 97 | + expect(links).toContain("https://example.com/transak"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("is disabled when no params are provided", () => { |
| 101 | + const { result } = renderHook(() => useBuyWithFiatQuotesForProviders(), { |
| 102 | + wrapper, |
| 103 | + }); |
| 104 | + |
| 105 | + expect(result.current).toHaveLength(4); |
| 106 | + expect(result.current.every((q) => !q.isSuccess && !q.isError)).toBe(true); |
| 107 | + expect(vi.mocked(prepareOnramp)).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments