|
| 1 | +import { renderHook, waitFor } from "@testing-library/react-native" |
| 2 | + |
| 3 | +import { ConvertDirection } from "@app/types/payment.types" |
| 4 | + |
| 5 | +import { useNonCustodialConversionLimits } from "@app/self-custodial/hooks/use-non-custodial-conversion-limits" |
| 6 | + |
| 7 | +const mockFetchConversionLimits = jest.fn() |
| 8 | +const mockUseSelfCustodialWallet = jest.fn() |
| 9 | + |
| 10 | +jest.mock("@app/self-custodial/bridge", () => ({ |
| 11 | + fetchConversionLimits: (...args: unknown[]) => mockFetchConversionLimits(...args), |
| 12 | +})) |
| 13 | + |
| 14 | +jest.mock("@app/self-custodial/providers/wallet-provider", () => ({ |
| 15 | + useSelfCustodialWallet: () => mockUseSelfCustodialWallet(), |
| 16 | +})) |
| 17 | + |
| 18 | +const fakeSdk = { id: "fake-sdk" } |
| 19 | + |
| 20 | +describe("useNonCustodialConversionLimits", () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks() |
| 23 | + mockUseSelfCustodialWallet.mockReturnValue({ sdk: fakeSdk }) |
| 24 | + }) |
| 25 | + |
| 26 | + it("returns limits after successful fetch and forwards direction to the bridge", async () => { |
| 27 | + mockFetchConversionLimits.mockResolvedValue({ |
| 28 | + minFromAmount: 1000, |
| 29 | + minToAmount: 500, |
| 30 | + }) |
| 31 | + |
| 32 | + const { result } = renderHook(() => |
| 33 | + useNonCustodialConversionLimits(ConvertDirection.BtcToUsd), |
| 34 | + ) |
| 35 | + |
| 36 | + await waitFor(() => { |
| 37 | + expect(result.current.loading).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + expect(result.current.limits).toEqual({ minFromAmount: 1000, minToAmount: 500 }) |
| 41 | + expect(result.current.error).toBeNull() |
| 42 | + expect(mockFetchConversionLimits).toHaveBeenCalledWith( |
| 43 | + fakeSdk, |
| 44 | + ConvertDirection.BtcToUsd, |
| 45 | + ) |
| 46 | + }) |
| 47 | + |
| 48 | + it("surfaces the error when fetchConversionLimits throws and clears limits", async () => { |
| 49 | + mockFetchConversionLimits.mockRejectedValue(new Error("network unreachable")) |
| 50 | + |
| 51 | + const { result } = renderHook(() => |
| 52 | + useNonCustodialConversionLimits(ConvertDirection.UsdToBtc), |
| 53 | + ) |
| 54 | + |
| 55 | + await waitFor(() => { |
| 56 | + expect(result.current.loading).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + expect(result.current.limits).toBeNull() |
| 60 | + expect(result.current.error?.message).toBe("network unreachable") |
| 61 | + }) |
| 62 | + |
| 63 | + it("does not call the bridge when sdk is null", () => { |
| 64 | + mockUseSelfCustodialWallet.mockReturnValue({ sdk: null }) |
| 65 | + |
| 66 | + const { result } = renderHook(() => |
| 67 | + useNonCustodialConversionLimits(ConvertDirection.BtcToUsd), |
| 68 | + ) |
| 69 | + |
| 70 | + expect(mockFetchConversionLimits).not.toHaveBeenCalled() |
| 71 | + expect(result.current.limits).toBeNull() |
| 72 | + expect(result.current.loading).toBe(false) |
| 73 | + }) |
| 74 | + |
| 75 | + it("refetches when direction changes", async () => { |
| 76 | + mockFetchConversionLimits.mockResolvedValue({ |
| 77 | + minFromAmount: 1, |
| 78 | + minToAmount: 1, |
| 79 | + }) |
| 80 | + |
| 81 | + const { rerender } = renderHook( |
| 82 | + (direction: ConvertDirection) => useNonCustodialConversionLimits(direction), |
| 83 | + { initialProps: ConvertDirection.BtcToUsd as ConvertDirection }, |
| 84 | + ) |
| 85 | + |
| 86 | + await waitFor(() => { |
| 87 | + expect(mockFetchConversionLimits).toHaveBeenCalledWith( |
| 88 | + fakeSdk, |
| 89 | + ConvertDirection.BtcToUsd, |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + rerender(ConvertDirection.UsdToBtc) |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(mockFetchConversionLimits).toHaveBeenCalledWith( |
| 97 | + fakeSdk, |
| 98 | + ConvertDirection.UsdToBtc, |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + expect(mockFetchConversionLimits).toHaveBeenCalledTimes(2) |
| 103 | + }) |
| 104 | +}) |
0 commit comments