-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseTokenInput.test.ts
More file actions
155 lines (124 loc) · 4.7 KB
/
Copy pathuseTokenInput.test.ts
File metadata and controls
155 lines (124 loc) · 4.7 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 mockUseAccount = vi.fn()
const mockUsePublicClient = vi.fn()
const mockGetBalance = vi.fn()
const mockUseTokens = vi.fn()
vi.mock('wagmi', () => ({
useAccount: () => mockUseAccount(),
usePublicClient: (args: { chainId?: number } = {}) => {
mockUsePublicClient(args)
return { getBalance: mockGetBalance }
},
}))
vi.mock('@/src/hooks/useErc20Balance', () => ({
useErc20Balance: () => ({ balance: undefined, balanceError: null, isLoadingBalance: false }),
}))
vi.mock('@/src/hooks/useTokens', () => ({
useTokens: (args: unknown) => mockUseTokens(args),
}))
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(() => {
mockUseAccount.mockReturnValue({ address: walletAddress })
mockUsePublicClient.mockClear()
mockGetBalance.mockReset()
mockUseTokens.mockReturnValue({ tokensByChainId: {}, isLoadingBalances: false, tokens: [] })
})
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))
})
it('does not fetch native balance when wallet is disconnected', () => {
mockUseAccount.mockReturnValue({ address: undefined })
const { result } = renderHook(() => useTokenInput(sepoliaEth), { wrapper })
expect(result.current.balance).toBeUndefined()
expect(result.current.balanceError).toBeNull()
expect(result.current.isLoadingBalance).toBe(false)
expect(mockGetBalance).not.toHaveBeenCalled()
})
it('exposes priceUSD for the selected token from useTokens', async () => {
mockUseTokens.mockReturnValue({
tokensByChainId: {
1: [{ ...mainnetUsdc, extensions: { priceUSD: '1.00' } }],
},
isLoadingBalances: false,
tokens: [],
})
const { result } = renderHook(() => useTokenInput(mainnetUsdc), { wrapper })
await waitFor(() => expect(result.current.priceUSD).toBe('1.00'))
})
it('exposes isLoadingPrice as true while useTokens is loading', () => {
mockUseTokens.mockReturnValue({ tokensByChainId: {}, isLoadingBalances: true, tokens: [] })
const { result } = renderHook(() => useTokenInput(mainnetUsdc), { wrapper })
expect(result.current.isLoadingPrice).toBe(true)
})
it('updates priceUSD when a different token is selected', async () => {
const mainnetEth: Token = {
address: zeroAddress,
chainId: 1,
decimals: 18,
name: 'Ether',
symbol: 'ETH',
}
mockUseTokens.mockReturnValue({
tokensByChainId: {
1: [{ ...mainnetEth, extensions: { priceUSD: '3000.00' } }],
},
isLoadingBalances: false,
tokens: [],
})
mockGetBalance.mockResolvedValue(1000000000000000000n)
const { result } = renderHook(() => useTokenInput(mainnetUsdc), { wrapper })
act(() => {
result.current.setTokenSelected(mainnetEth)
})
await waitFor(() => expect(result.current.priceUSD).toBe('3000.00'))
})
})