-
Notifications
You must be signed in to change notification settings - Fork 4
test: add hook test coverage (Tier 3) #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import type { Token } from '@/src/types/token' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { renderHook, waitFor } from '@testing-library/react' | ||
| import type { ReactNode } from 'react' | ||
| import { createElement } from 'react' | ||
| import { zeroAddress } from 'viem' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { useErc20Balance } from './useErc20Balance' | ||
|
|
||
| const mockReadContract = vi.fn() | ||
|
|
||
| vi.mock('wagmi', () => ({ | ||
| usePublicClient: vi.fn(() => ({ | ||
| readContract: mockReadContract, | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('@/src/env', () => ({ | ||
| env: { PUBLIC_NATIVE_TOKEN_ADDRESS: zeroAddress.toLowerCase() }, | ||
| })) | ||
|
|
||
| const wrapper = ({ children }: { children: ReactNode }) => | ||
| createElement( | ||
| QueryClientProvider, | ||
| { client: new QueryClient({ defaultOptions: { queries: { retry: false } } }) }, | ||
| children, | ||
| ) | ||
|
|
||
| const mockToken: Token = { | ||
| address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', | ||
| chainId: 1, | ||
| decimals: 6, | ||
| name: 'USD Coin', | ||
| symbol: 'USDC', | ||
| } | ||
|
|
||
| const walletAddress = '0x71C7656EC7ab88b098defB751B7401B5f6d8976F' as `0x${string}` | ||
|
|
||
| describe('useErc20Balance', () => { | ||
| beforeEach(() => { | ||
| mockReadContract.mockClear() | ||
| }) | ||
|
|
||
| it('returns undefined balance when address is missing', () => { | ||
| const { result } = renderHook(() => useErc20Balance({ token: mockToken }), { wrapper }) | ||
| expect(result.current.balance).toBeUndefined() | ||
| expect(result.current.isLoadingBalance).toBe(false) | ||
| }) | ||
|
|
||
| it('returns undefined balance when token is missing', () => { | ||
| const { result } = renderHook(() => useErc20Balance({ address: walletAddress }), { wrapper }) | ||
| expect(result.current.balance).toBeUndefined() | ||
| expect(result.current.isLoadingBalance).toBe(false) | ||
| }) | ||
|
|
||
| it('does not fetch balance for native token address', () => { | ||
| const nativeToken: Token = { ...mockToken, address: zeroAddress } | ||
| const { result } = renderHook( | ||
| () => useErc20Balance({ address: walletAddress, token: nativeToken }), | ||
| { wrapper }, | ||
| ) | ||
| expect(mockReadContract).not.toHaveBeenCalled() | ||
| expect(result.current.isLoadingBalance).toBe(false) | ||
| }) | ||
|
|
||
| it('returns balance when query resolves', async () => { | ||
| mockReadContract.mockResolvedValueOnce(BigInt(1_000_000)) | ||
| const { result } = renderHook( | ||
| () => useErc20Balance({ address: walletAddress, token: mockToken }), | ||
| { wrapper }, | ||
| ) | ||
| await waitFor(() => expect(result.current.isLoadingBalance).toBe(false)) | ||
| expect(result.current.balance).toBe(BigInt(1_000_000)) | ||
| expect(result.current.balanceError).toBeNull() | ||
| }) | ||
|
|
||
| it('returns error when query fails', async () => { | ||
| mockReadContract.mockRejectedValueOnce(new Error('RPC error')) | ||
| const { result } = renderHook( | ||
| () => useErc20Balance({ address: walletAddress, token: mockToken }), | ||
| { wrapper }, | ||
| ) | ||
| await waitFor(() => expect(result.current.balanceError).toBeTruthy()) | ||
| expect(result.current.balance).toBeUndefined() | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import type { Token } from '@/src/types/token' | ||
| import tokenListsCache, { updateTokenListsCache } from '@/src/utils/tokenListsCache' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { renderHook } from '@testing-library/react' | ||
| import { createElement } from 'react' | ||
| import type { ReactNode } from 'react' | ||
| import { zeroAddress } from 'viem' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@/src/utils/tokenListsCache', () => { | ||
| const cache = { tokens: [] as Token[], tokensByChainId: {} as Record<number, Token[]> } | ||
| return { | ||
| default: cache, | ||
| updateTokenListsCache: vi.fn((map: typeof cache) => { | ||
| cache.tokens = map.tokens | ||
| cache.tokensByChainId = map.tokensByChainId | ||
| }), | ||
| addTokenToTokenList: vi.fn(), | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('@/src/env', () => ({ | ||
| env: { | ||
| PUBLIC_NATIVE_TOKEN_ADDRESS: zeroAddress.toLowerCase(), | ||
| PUBLIC_USE_DEFAULT_TOKENS: false, | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/src/constants/tokenLists', () => ({ | ||
| tokenLists: {}, | ||
| })) | ||
|
|
||
| vi.mock('@tanstack/react-query', async (importActual) => { | ||
| const actual = await importActual<typeof import('@tanstack/react-query')>() | ||
| return { ...actual, useSuspenseQueries: vi.fn() } | ||
| }) | ||
|
|
||
| import * as tanstackQuery from '@tanstack/react-query' | ||
| import { useTokenLists } from './useTokenLists' | ||
|
|
||
| const mockToken1: Token = { | ||
| address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', | ||
| chainId: 1, | ||
| decimals: 6, | ||
| name: 'USD Coin', | ||
| symbol: 'USDC', | ||
| } | ||
| const mockToken2: Token = { | ||
| address: '0xdAC17F958D2ee523a2206206994597C13D831ec7', | ||
| chainId: 1, | ||
| decimals: 6, | ||
| name: 'Tether USD', | ||
| symbol: 'USDT', | ||
| } | ||
|
|
||
| const mockSuspenseQueryResult = (tokens: Token[]) => ({ | ||
| data: { name: 'Mock List', timestamp: '', version: { major: 1, minor: 0, patch: 0 }, tokens }, | ||
| isLoading: false, | ||
| isSuccess: true, | ||
| error: null, | ||
| }) | ||
|
|
||
| const wrapper = ({ children }: { children: ReactNode }) => | ||
| createElement(QueryClientProvider, { client: new QueryClient() }, children) | ||
|
|
||
| beforeEach(() => { | ||
| // Reset cache between tests | ||
| tokenListsCache.tokens = [] | ||
| tokenListsCache.tokensByChainId = {} | ||
| vi.mocked(updateTokenListsCache).mockImplementation((map) => { | ||
| tokenListsCache.tokens = map.tokens | ||
| tokenListsCache.tokensByChainId = map.tokensByChainId | ||
| }) | ||
| }) | ||
|
|
||
| describe('useTokenLists', () => { | ||
| it('returns tokens and tokensByChainId', () => { | ||
| vi.mocked(tanstackQuery.useSuspenseQueries).mockReturnValueOnce( | ||
| // biome-ignore lint/suspicious/noExplicitAny: mocking overloaded hook return type | ||
| { tokens: [mockToken1], tokensByChainId: { 1: [mockToken1] } } as any, | ||
| ) | ||
|
|
||
| const { result } = renderHook(() => useTokenLists(), { wrapper }) | ||
| expect(result.current.tokens).toBeDefined() | ||
| expect(result.current.tokensByChainId).toBeDefined() | ||
| }) | ||
|
|
||
| it('deduplicates tokens with the same chainId and address', () => { | ||
| // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param | ||
| vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation(({ combine }: any) => { | ||
| const results = [ | ||
| mockSuspenseQueryResult([mockToken1, mockToken2]), | ||
| mockSuspenseQueryResult([{ ...mockToken1 }]), // duplicate | ||
| ] | ||
| return combine(results) | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useTokenLists(), { wrapper }) | ||
| const erc20Tokens = result.current.tokens.filter((t) => t.address !== zeroAddress.toLowerCase()) | ||
| expect(erc20Tokens).toHaveLength(2) | ||
| expect(erc20Tokens.map((t) => t.symbol)).toContain('USDC') | ||
| expect(erc20Tokens.map((t) => t.symbol)).toContain('USDT') | ||
| }) | ||
|
|
||
| it('injects a native ETH token for mainnet (chainId 1) tokens', () => { | ||
| vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation( | ||
| // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param | ||
| ({ combine }: any) => combine([mockSuspenseQueryResult([mockToken1])]), | ||
| ) | ||
|
|
||
| const { result } = renderHook(() => useTokenLists(), { wrapper }) | ||
| const nativeToken = result.current.tokensByChainId[1]?.[0] | ||
| expect(nativeToken?.address).toBe(zeroAddress.toLowerCase()) | ||
| expect(nativeToken?.symbol).toBe('ETH') | ||
| }) | ||
|
|
||
| it('filters out tokens that fail schema validation', () => { | ||
| // biome-ignore lint/suspicious/noExplicitAny: mocking internal combine param | ||
| vi.mocked(tanstackQuery.useSuspenseQueries).mockImplementation(({ combine }: any) => { | ||
| const invalidToken = { | ||
| address: 'not-an-address', | ||
| chainId: 1, | ||
| name: 'Bad', | ||
| symbol: 'BAD', | ||
| decimals: 18, | ||
| } | ||
| // biome-ignore lint/suspicious/noExplicitAny: intentionally testing invalid token input | ||
| return combine([mockSuspenseQueryResult([mockToken1, invalidToken as any])]) | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useTokenLists(), { wrapper }) | ||
| const erc20Tokens = result.current.tokens.filter((t) => t.address !== zeroAddress.toLowerCase()) | ||
| expect(erc20Tokens).toHaveLength(1) | ||
| expect(erc20Tokens[0].symbol).toBe('USDC') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { renderHook } from '@testing-library/react' | ||
| import type { Address } from 'viem' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { useWeb3Status, useWeb3StatusConnected } from './useWeb3Status' | ||
|
|
||
| const mockDisconnect = vi.fn() | ||
| const mockSwitchChain = vi.fn() | ||
|
|
||
gabitoesmiapodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vi.mock('wagmi', () => ({ | ||
| useAccount: vi.fn(() => ({ | ||
| address: undefined, | ||
| chainId: undefined, | ||
| isConnected: false, | ||
| isConnecting: false, | ||
| })), | ||
| useChainId: vi.fn(() => 1), | ||
| useSwitchChain: vi.fn(() => ({ isPending: false, switchChain: mockSwitchChain })), | ||
| usePublicClient: vi.fn(() => undefined), | ||
| useWalletClient: vi.fn(() => ({ data: undefined })), | ||
| useBalance: vi.fn(() => ({ data: undefined })), | ||
| useDisconnect: vi.fn(() => ({ disconnect: mockDisconnect })), | ||
| })) | ||
|
|
||
| import * as wagmi from 'wagmi' | ||
|
|
||
| type MockAccount = ReturnType<typeof wagmi.useAccount> | ||
| type MockSwitchChain = ReturnType<typeof wagmi.useSwitchChain> | ||
|
|
||
| describe('useWeb3Status', () => { | ||
| beforeEach(() => { | ||
| mockDisconnect.mockClear() | ||
| mockSwitchChain.mockClear() | ||
| }) | ||
|
|
||
| it('returns disconnected state when no wallet connected', () => { | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.isWalletConnected).toBe(false) | ||
| expect(result.current.address).toBeUndefined() | ||
| expect(result.current.walletChainId).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns connected state with wallet address', () => { | ||
| const mock = { | ||
| address: '0xabc123' as Address, | ||
| chainId: 1, | ||
| isConnected: true, | ||
| isConnecting: false, | ||
| } as unknown as MockAccount | ||
| vi.mocked(wagmi.useAccount).mockReturnValueOnce(mock) | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.isWalletConnected).toBe(true) | ||
| expect(result.current.address).toBe('0xabc123') | ||
| }) | ||
|
|
||
| it('sets isWalletSynced true when wallet chainId matches app chainId', () => { | ||
| const mock = { | ||
| address: '0xabc123' as Address, | ||
| chainId: 1, | ||
| isConnected: true, | ||
| isConnecting: false, | ||
| } as unknown as MockAccount | ||
| vi.mocked(wagmi.useAccount).mockReturnValueOnce(mock) | ||
| vi.mocked(wagmi.useChainId).mockReturnValueOnce(1) | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.isWalletSynced).toBe(true) | ||
| }) | ||
|
|
||
| it('sets isWalletSynced false when wallet chainId differs from app chainId', () => { | ||
| const mock = { | ||
| address: '0xabc123' as Address, | ||
| chainId: 137, | ||
| isConnected: true, | ||
| isConnecting: false, | ||
| } as unknown as MockAccount | ||
| vi.mocked(wagmi.useAccount).mockReturnValueOnce(mock) | ||
| vi.mocked(wagmi.useChainId).mockReturnValueOnce(1) | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.isWalletSynced).toBe(false) | ||
| }) | ||
|
|
||
| it('sets switchingChain when useSwitchChain is pending', () => { | ||
| const mock = { isPending: true, switchChain: mockSwitchChain } as unknown as MockSwitchChain | ||
| vi.mocked(wagmi.useSwitchChain).mockReturnValueOnce(mock) | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.switchingChain).toBe(true) | ||
| }) | ||
|
|
||
| it('exposes disconnect function', () => { | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| result.current.disconnect() | ||
| expect(mockDisconnect).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('calls switchChain with chainId when switchChain action is invoked', () => { | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| result.current.switchChain(137) | ||
| expect(mockSwitchChain).toHaveBeenCalledWith({ chainId: 137 }) | ||
| }) | ||
|
|
||
| it('exposes appChainId from useChainId', () => { | ||
| vi.mocked(wagmi.useChainId).mockReturnValueOnce(42161) | ||
| const { result } = renderHook(() => useWeb3Status()) | ||
| expect(result.current.appChainId).toBe(42161) | ||
| }) | ||
| }) | ||
|
|
||
| describe('useWeb3StatusConnected', () => { | ||
| it('throws when wallet is not connected', () => { | ||
| expect(() => renderHook(() => useWeb3StatusConnected())).toThrow( | ||
| 'Use useWeb3StatusConnected only when a wallet is connected', | ||
| ) | ||
| }) | ||
|
|
||
| it('returns status when wallet is connected', () => { | ||
| const mock = { | ||
| address: '0xdeadbeef' as Address, | ||
| chainId: 1, | ||
| isConnected: true, | ||
| isConnecting: false, | ||
| } as unknown as MockAccount | ||
| // useWeb3StatusConnected calls useWeb3Status twice; both calls must see connected state | ||
| vi.mocked(wagmi.useAccount).mockReturnValueOnce(mock).mockReturnValueOnce(mock) | ||
| const { result } = renderHook(() => useWeb3StatusConnected()) | ||
| expect(result.current.isWalletConnected).toBe(true) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.