-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseTokenInput.test.ts
More file actions
88 lines (71 loc) · 2.56 KB
/
Copy pathuseTokenInput.test.ts
File metadata and controls
88 lines (71 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { createElement, type ReactNode } from 'react'
import { zeroAddress } from 'viem'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { Token } from '@/src/types/token'
import { useTokenInput } from './useTokenInput'
const walletAddress = '0x71C7656EC7ab88b098defB751B7401B5f6d8976F' as const
const mockUsePublicClient = vi.fn()
const mockGetBalance = vi.fn()
vi.mock('wagmi', () => ({
useAccount: () => ({ address: walletAddress }),
usePublicClient: (args: { chainId?: number } = {}) => {
mockUsePublicClient(args)
return { getBalance: mockGetBalance }
},
}))
vi.mock('@/src/hooks/useErc20Balance', () => ({
useErc20Balance: () => ({ balance: undefined, balanceError: null, isLoadingBalance: false }),
}))
vi.mock('@/src/env', () => ({
env: { PUBLIC_NATIVE_TOKEN_ADDRESS: zeroAddress.toLowerCase() },
}))
const mainnetUsdc: Token = {
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
chainId: 1,
decimals: 6,
name: 'USD Coin',
symbol: 'USDC',
}
const sepoliaEth: Token = {
address: zeroAddress,
chainId: 11155111,
decimals: 18,
name: 'Sepolia Ether',
symbol: 'ETH',
}
const wrapper = ({ children }: { children: ReactNode }) =>
createElement(
QueryClientProvider,
{ client: new QueryClient({ defaultOptions: { queries: { retry: false } } }) },
children,
)
describe('useTokenInput', () => {
beforeEach(() => {
mockUsePublicClient.mockClear()
mockGetBalance.mockReset()
})
it('rebinds the native public client to the selected token chain when the user switches chains', async () => {
mockGetBalance.mockResolvedValue(42n)
const { result } = renderHook(() => useTokenInput(mainnetUsdc), { wrapper })
act(() => {
result.current.setTokenSelected(sepoliaEth)
})
await waitFor(() =>
expect(mockUsePublicClient).toHaveBeenLastCalledWith({ chainId: sepoliaEth.chainId }),
)
await waitFor(() => expect(result.current.balance).toBe(42n))
})
it('binds the native public client to the selected token chain when no initial token is given', async () => {
mockGetBalance.mockResolvedValue(7n)
const { result } = renderHook(() => useTokenInput(), { wrapper })
act(() => {
result.current.setTokenSelected(sepoliaEth)
})
await waitFor(() =>
expect(mockUsePublicClient).toHaveBeenLastCalledWith({ chainId: sepoliaEth.chainId }),
)
await waitFor(() => expect(result.current.balance).toBe(7n))
})
})