|
| 1 | +import type { Token } from '@/src/types/token' |
| 2 | +import tokenListsCache, { updateTokenListsCache } from '@/src/utils/tokenListsCache' |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { renderHook } from '@testing-library/react' |
| 5 | +import { createElement } from 'react' |
| 6 | +import type { ReactNode } from 'react' |
| 7 | +import { zeroAddress } from 'viem' |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +vi.mock('@/src/utils/tokenListsCache', () => { |
| 11 | + const cache = { tokens: [] as Token[], tokensByChainId: {} as Record<number, Token[]> } |
| 12 | + return { |
| 13 | + default: cache, |
| 14 | + updateTokenListsCache: vi.fn((map: typeof cache) => { |
| 15 | + cache.tokens = map.tokens |
| 16 | + cache.tokensByChainId = map.tokensByChainId |
| 17 | + }), |
| 18 | + addTokenToTokenList: vi.fn(), |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +vi.mock('@/src/env', () => ({ |
| 23 | + env: { |
| 24 | + PUBLIC_NATIVE_TOKEN_ADDRESS: zeroAddress.toLowerCase(), |
| 25 | + PUBLIC_USE_DEFAULT_TOKENS: false, |
| 26 | + }, |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/src/constants/tokenLists', () => ({ |
| 30 | + tokenLists: {}, |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@tanstack/react-query', async (importActual) => { |
| 34 | + const actual = await importActual<typeof import('@tanstack/react-query')>() |
| 35 | + return { ...actual, useSuspenseQueries: vi.fn() } |
| 36 | +}) |
| 37 | + |
| 38 | +import * as tanstackQuery from '@tanstack/react-query' |
| 39 | +import { useTokenLists } from './useTokenLists' |
| 40 | + |
| 41 | +const mockToken1: Token = { |
| 42 | + address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', |
| 43 | + chainId: 1, |
| 44 | + decimals: 6, |
| 45 | + name: 'USD Coin', |
| 46 | + symbol: 'USDC', |
| 47 | +} |
| 48 | +const mockToken2: Token = { |
| 49 | + address: '0xdAC17F958D2ee523a2206206994597C13D831ec7', |
| 50 | + chainId: 1, |
| 51 | + decimals: 6, |
| 52 | + name: 'Tether USD', |
| 53 | + symbol: 'USDT', |
| 54 | +} |
| 55 | + |
| 56 | +const mockSuspenseQueryResult = (tokens: Token[]) => ({ |
| 57 | + data: { name: 'Mock List', timestamp: '', version: { major: 1, minor: 0, patch: 0 }, tokens }, |
| 58 | + isLoading: false, |
| 59 | + isSuccess: true, |
| 60 | + error: null, |
| 61 | +}) |
| 62 | + |
| 63 | +const wrapper = ({ children }: { children: ReactNode }) => |
| 64 | + createElement(QueryClientProvider, { client: new QueryClient() }, children) |
| 65 | + |
| 66 | +beforeEach(() => { |
| 67 | + // Reset cache between tests |
| 68 | + tokenListsCache.tokens = [] |
| 69 | + tokenListsCache.tokensByChainId = {} |
| 70 | + vi.mocked(updateTokenListsCache).mockImplementation((map) => { |
| 71 | + tokenListsCache.tokens = map.tokens |
| 72 | + tokenListsCache.tokensByChainId = map.tokensByChainId |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +describe('useTokenLists', () => { |
| 77 | + it('returns tokens and tokensByChainId', () => { |
| 78 | + vi.mocked(tanstackQuery.useSuspenseQueries).mockReturnValueOnce( |
| 79 | + // biome-ignore lint/suspicious/noExplicitAny: mocking overloaded hook return type |
| 80 | + { tokens: [mockToken1], tokensByChainId: { 1: [mockToken1] } } as any, |
| 81 | + ) |
| 82 | + |
| 83 | + const { result } = renderHook(() => useTokenLists(), { wrapper }) |
| 84 | + expect(result.current.tokens).toBeDefined() |
| 85 | + expect(result.current.tokensByChainId).toBeDefined() |
| 86 | + }) |
| 87 | + |
| 88 | + it('deduplicates tokens with the same chainId and address', () => { |
| 89 | + // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param |
| 90 | + vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation(({ combine }: any) => { |
| 91 | + const results = [ |
| 92 | + mockSuspenseQueryResult([mockToken1, mockToken2]), |
| 93 | + mockSuspenseQueryResult([{ ...mockToken1 }]), // duplicate |
| 94 | + ] |
| 95 | + return combine(results) |
| 96 | + }) |
| 97 | + |
| 98 | + const { result } = renderHook(() => useTokenLists(), { wrapper }) |
| 99 | + const erc20Tokens = result.current.tokens.filter((t) => t.address !== zeroAddress.toLowerCase()) |
| 100 | + expect(erc20Tokens).toHaveLength(2) |
| 101 | + expect(erc20Tokens.map((t) => t.symbol)).toContain('USDC') |
| 102 | + expect(erc20Tokens.map((t) => t.symbol)).toContain('USDT') |
| 103 | + }) |
| 104 | + |
| 105 | + it('injects a native ETH token for mainnet (chainId 1) tokens', () => { |
| 106 | + vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation( |
| 107 | + // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param |
| 108 | + ({ combine }: any) => combine([mockSuspenseQueryResult([mockToken1])]), |
| 109 | + ) |
| 110 | + |
| 111 | + const { result } = renderHook(() => useTokenLists(), { wrapper }) |
| 112 | + const nativeToken = result.current.tokensByChainId[1]?.[0] |
| 113 | + expect(nativeToken?.address).toBe(zeroAddress.toLowerCase()) |
| 114 | + expect(nativeToken?.symbol).toBe('ETH') |
| 115 | + }) |
| 116 | + |
| 117 | + it('filters out tokens that fail schema validation', () => { |
| 118 | + // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param |
| 119 | + vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation(({ combine }: any) => { |
| 120 | + const invalidToken = { |
| 121 | + address: 'not-an-address', |
| 122 | + chainId: 1, |
| 123 | + name: 'Bad', |
| 124 | + symbol: 'BAD', |
| 125 | + decimals: 18, |
| 126 | + } |
| 127 | + // biome-ignore lint/suspicious/noExplicitAny: intentionally testing invalid token input |
| 128 | + return combine([mockSuspenseQueryResult([mockToken1, invalidToken as any])]) |
| 129 | + }) |
| 130 | + |
| 131 | + const { result } = renderHook(() => useTokenLists(), { wrapper }) |
| 132 | + const erc20Tokens = result.current.tokens.filter((t) => t.address !== zeroAddress.toLowerCase()) |
| 133 | + expect(erc20Tokens).toHaveLength(1) |
| 134 | + expect(erc20Tokens[0].symbol).toBe('USDC') |
| 135 | + }) |
| 136 | +}) |
0 commit comments