|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import type { ReactNode } from "react"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 6 | +import type { TokenWithPrices } from "../../../../../bridge/index.js"; |
| 7 | +import { getToken } from "../../../../../pay/convert/get-token.js"; |
| 8 | +import { useTokenQuery } from "./token-query.js"; |
| 9 | + |
| 10 | +vi.mock("../../../../../pay/convert/get-token.js", () => ({ |
| 11 | + getToken: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +const TOKEN_ADDRESS = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359"; |
| 15 | +const CHAIN_ID = 137; |
| 16 | + |
| 17 | +const MOCK_TOKEN = { |
| 18 | + address: TOKEN_ADDRESS, |
| 19 | + chainId: CHAIN_ID, |
| 20 | + decimals: 6, |
| 21 | + name: "USD Coin", |
| 22 | + prices: { USD: 1 }, |
| 23 | + symbol: "USDC", |
| 24 | +} as TokenWithPrices; |
| 25 | + |
| 26 | +const createWrapper = () => { |
| 27 | + const queryClient = new QueryClient({ |
| 28 | + defaultOptions: { |
| 29 | + queries: { |
| 30 | + retry: false, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }); |
| 34 | + return ({ children }: { children: ReactNode }) => ( |
| 35 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +describe("useTokenQuery", () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.mocked(getToken).mockReset(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns success when getToken resolves", async () => { |
| 45 | + vi.mocked(getToken).mockResolvedValue(MOCK_TOKEN); |
| 46 | + |
| 47 | + const { result } = renderHook( |
| 48 | + () => |
| 49 | + useTokenQuery({ |
| 50 | + chainId: CHAIN_ID, |
| 51 | + client: TEST_CLIENT, |
| 52 | + tokenAddress: TOKEN_ADDRESS, |
| 53 | + }), |
| 54 | + { wrapper: createWrapper() }, |
| 55 | + ); |
| 56 | + |
| 57 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 58 | + expect(result.current.data).toEqual({ |
| 59 | + token: MOCK_TOKEN, |
| 60 | + type: "success", |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns unsupported_token when getToken throws a not-supported Error", async () => { |
| 65 | + vi.mocked(getToken).mockRejectedValue(new Error("Token not supported")); |
| 66 | + |
| 67 | + const { result } = renderHook( |
| 68 | + () => |
| 69 | + useTokenQuery({ |
| 70 | + chainId: CHAIN_ID, |
| 71 | + client: TEST_CLIENT, |
| 72 | + tokenAddress: TOKEN_ADDRESS, |
| 73 | + }), |
| 74 | + { wrapper: createWrapper() }, |
| 75 | + ); |
| 76 | + |
| 77 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 78 | + expect(result.current.data).toEqual({ type: "unsupported_token" }); |
| 79 | + expect(result.current.isError).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it("surfaces 401 failures as query errors instead of unsupported_token", async () => { |
| 83 | + vi.mocked(getToken).mockRejectedValue(new Error("401 Unauthorized")); |
| 84 | + |
| 85 | + const { result } = renderHook( |
| 86 | + () => |
| 87 | + useTokenQuery({ |
| 88 | + chainId: CHAIN_ID, |
| 89 | + client: TEST_CLIENT, |
| 90 | + tokenAddress: TOKEN_ADDRESS, |
| 91 | + }), |
| 92 | + { wrapper: createWrapper() }, |
| 93 | + ); |
| 94 | + |
| 95 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 96 | + expect(result.current.data).toBeUndefined(); |
| 97 | + expect(result.current.error).toBeInstanceOf(Error); |
| 98 | + expect((result.current.error as Error).message).toBe("401 Unauthorized"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("surfaces non-Error rejections as query errors instead of unsupported_token", async () => { |
| 102 | + vi.mocked(getToken).mockRejectedValue("timeout"); |
| 103 | + |
| 104 | + const { result } = renderHook( |
| 105 | + () => |
| 106 | + useTokenQuery({ |
| 107 | + chainId: CHAIN_ID, |
| 108 | + client: TEST_CLIENT, |
| 109 | + tokenAddress: TOKEN_ADDRESS, |
| 110 | + }), |
| 111 | + { wrapper: createWrapper() }, |
| 112 | + ); |
| 113 | + |
| 114 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 115 | + expect(result.current.data).toBeUndefined(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments