|
| 1 | +import { useGetPosition } from "@/hooks/useGetPosition"; |
| 2 | +import { getPosition } from "@/utils/getPosition"; |
| 3 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 4 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 5 | +import { Token } from "@uniswap/sdk-core"; |
| 6 | +import { jsx as _jsx } from "react/jsx-runtime"; |
| 7 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 8 | + |
| 9 | +// Mock getPosition |
| 10 | +vi.mock("@/utils/getPosition", () => ({ |
| 11 | + getPosition: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe("useGetPosition", () => { |
| 15 | + let queryClient: QueryClient; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + queryClient = new QueryClient({ |
| 19 | + defaultOptions: { |
| 20 | + queries: { |
| 21 | + retry: false, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }); |
| 25 | + vi.resetAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + const wrapper = ({ children }: { children: React.ReactNode }) => |
| 29 | + _jsx(QueryClientProvider, { client: queryClient, children }); |
| 30 | + |
| 31 | + it("should fetch position data successfully", async () => { |
| 32 | + const mockPosition = { |
| 33 | + token0: new Token( |
| 34 | + 1, |
| 35 | + "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", |
| 36 | + 6, |
| 37 | + "USDC", |
| 38 | + "USD Coin", |
| 39 | + ), |
| 40 | + token1: new Token( |
| 41 | + 1, |
| 42 | + "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", |
| 43 | + 18, |
| 44 | + "WETH", |
| 45 | + "Wrapped Ether", |
| 46 | + ), |
| 47 | + amounts: { |
| 48 | + amount0: "1000000", |
| 49 | + amount1: "1000000000000000000", |
| 50 | + }, |
| 51 | + tickLower: -100, |
| 52 | + tickUpper: 100, |
| 53 | + liquidity: BigInt("1000000000000000000"), |
| 54 | + poolId: "0x1234567890123456789012345678901234567890", |
| 55 | + }; |
| 56 | + |
| 57 | + (getPosition as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce( |
| 58 | + mockPosition, |
| 59 | + ); |
| 60 | + |
| 61 | + const { result } = renderHook( |
| 62 | + () => |
| 63 | + useGetPosition({ |
| 64 | + tokenId: "123", |
| 65 | + chainId: 1, |
| 66 | + }), |
| 67 | + { wrapper }, |
| 68 | + ); |
| 69 | + |
| 70 | + expect(result.current.isLoading).toBe(true); |
| 71 | + expect(result.current.data).toBeUndefined(); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(result.current.isLoading).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(result.current.data).toEqual(mockPosition); |
| 78 | + expect(getPosition).toHaveBeenCalledWith("123", 1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should handle errors", async () => { |
| 82 | + const error = new Error("Failed to fetch position"); |
| 83 | + (getPosition as unknown as ReturnType<typeof vi.fn>).mockRejectedValueOnce( |
| 84 | + error, |
| 85 | + ); |
| 86 | + |
| 87 | + const { result } = renderHook( |
| 88 | + () => |
| 89 | + useGetPosition({ |
| 90 | + tokenId: "123", |
| 91 | + chainId: 1, |
| 92 | + }), |
| 93 | + { wrapper }, |
| 94 | + ); |
| 95 | + |
| 96 | + await waitFor(() => { |
| 97 | + expect(result.current.isLoading).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.current.error).toEqual(error); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should throw error if no tokenId provided", () => { |
| 104 | + expect(() => { |
| 105 | + renderHook(() => useGetPosition(), { wrapper }); |
| 106 | + }).toThrow("No tokenId provided"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should handle custom query options", async () => { |
| 110 | + const mockPosition = { |
| 111 | + token0: new Token( |
| 112 | + 1, |
| 113 | + "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", |
| 114 | + 6, |
| 115 | + "USDC", |
| 116 | + "USD Coin", |
| 117 | + ), |
| 118 | + token1: new Token( |
| 119 | + 1, |
| 120 | + "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", |
| 121 | + 18, |
| 122 | + "WETH", |
| 123 | + "Wrapped Ether", |
| 124 | + ), |
| 125 | + amounts: { |
| 126 | + amount0: "1000000", |
| 127 | + amount1: "1000000000000000000", |
| 128 | + }, |
| 129 | + tickLower: -100, |
| 130 | + tickUpper: 100, |
| 131 | + liquidity: BigInt("1000000000000000000"), |
| 132 | + poolId: "0x1234567890123456789012345678901234567890", |
| 133 | + }; |
| 134 | + |
| 135 | + (getPosition as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce( |
| 136 | + mockPosition, |
| 137 | + ); |
| 138 | + |
| 139 | + const { result } = renderHook( |
| 140 | + () => |
| 141 | + useGetPosition({ |
| 142 | + tokenId: "123", |
| 143 | + chainId: 1, |
| 144 | + queryOptions: { |
| 145 | + enabled: true, |
| 146 | + staleTime: 30000, |
| 147 | + }, |
| 148 | + }), |
| 149 | + { wrapper }, |
| 150 | + ); |
| 151 | + |
| 152 | + await waitFor(() => { |
| 153 | + expect(result.current.isLoading).toBe(false); |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result.current.data).toEqual(mockPosition); |
| 157 | + }); |
| 158 | +}); |
0 commit comments