-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseERC20Tokens.test.ts
More file actions
240 lines (199 loc) · 6.73 KB
/
useERC20Tokens.test.ts
File metadata and controls
240 lines (199 loc) · 6.73 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { act, waitFor } from '@testing-library/react-native';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { NameType } from '../../UI/Name/Name.types';
import { useERC20Tokens } from './useERC20Tokens';
import { renderHookWithProvider } from '../../../util/test/renderWithProvider';
const TOKEN_NAME_MOCK = 'Test Token';
const TOKEN_SYMBOL_MOCK = 'TT';
const TOKEN_ICON_URL_MOCK = 'https://example.com/icon.png';
// CHAIN_IDS.MAINNET = '0x1' → decimal 1
const CHAIN_ID_MOCK = CHAIN_IDS.MAINNET;
// Each test gets a unique address to avoid module-level cache pollution.
let addressCounter = 0;
const makeAddress = () =>
`0x${(++addressCounter).toString().padStart(40, '0')}`;
const makeAssetId = (address: string) =>
`eip155:1/erc20:${address.toLowerCase()}`;
function makeTokenResponse(address: string) {
return [
{
assetId: makeAssetId(address),
name: TOKEN_NAME_MOCK,
symbol: TOKEN_SYMBOL_MOCK,
iconUrl: TOKEN_ICON_URL_MOCK,
},
];
}
function mockFetch(response: unknown) {
return jest.spyOn(global, 'fetch').mockResolvedValue({
json: () => Promise.resolve(response),
ok: true,
} as Response);
}
function renderHook(requests: Parameters<typeof useERC20Tokens>[0]) {
return renderHookWithProvider(() => useERC20Tokens(requests), { state: {} });
}
describe('useERC20Tokens', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('returns undefined initially before fetch resolves', () => {
mockFetch(makeTokenResponse(makeAddress()));
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: makeAddress(),
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]).toEqual({ name: undefined, image: undefined });
});
it('returns name after fetch resolves', async () => {
const address = makeAddress();
mockFetch(makeTokenResponse(address));
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
]);
await waitFor(() => {
expect(result.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
});
it('returns symbol when preferContractSymbol is true', async () => {
const address = makeAddress();
mockFetch(makeTokenResponse(address));
const { result } = renderHook([
{
preferContractSymbol: true,
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
]);
await waitFor(() => {
expect(result.current[0]?.name).toBe(TOKEN_SYMBOL_MOCK);
});
});
it('returns image after fetch resolves', async () => {
const address = makeAddress();
mockFetch(makeTokenResponse(address));
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
]);
await waitFor(() => {
expect(result.current[0]?.image).toBe(TOKEN_ICON_URL_MOCK);
});
});
it('returns undefined if type is not EthereumAddress', () => {
const { result } = renderHook([
{
type: 'alternateType' as NameType,
value: makeAddress(),
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]).toBeUndefined();
});
it('normalizes addresses to lowercase', async () => {
const address = makeAddress();
mockFetch(makeTokenResponse(address));
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: address.toUpperCase(),
variation: CHAIN_ID_MOCK,
},
]);
await waitFor(() => {
expect(result.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
});
it('returns undefined if fetch fails', async () => {
jest.spyOn(global, 'fetch').mockRejectedValue(new Error('Network error'));
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: makeAddress(),
variation: CHAIN_ID_MOCK,
},
]);
// Give fetch time to fail, state should remain empty
await act(async () => {
await new Promise((r) => setTimeout(r, 50));
});
expect(result.current[0]).toEqual({ name: undefined, image: undefined });
});
it('uses correct API URL with comma-separated assetIds', async () => {
const address = makeAddress();
const fetchMock = mockFetch(makeTokenResponse(address));
renderHook([
{
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
]);
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});
const calledUrl = fetchMock.mock.calls[0][0] as string;
expect(calledUrl).toContain('tokens.api.cx.metamask.io/v3/assets');
expect(calledUrl).toContain(encodeURIComponent(makeAssetId(address)));
expect(calledUrl).toContain('includeIconUrl=true');
});
// Note: this test produces one act() warning because each renderHook call creates
// an independent React tree. When the shared in-flight promise resolves, all three
// trees update simultaneously, but only the one observed by waitFor is wrapped in act.
// This is a known RNTL limitation when testing cross-hook module-level state sharing.
it('deduplicates concurrent requests for the same token', async () => {
const address = makeAddress();
const fetchMock = mockFetch(makeTokenResponse(address));
const request = [
{
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
];
const { result: r1 } = renderHook(request);
const { result: r2 } = renderHook(request);
const { result: r3 } = renderHook(request);
await waitFor(() => {
expect(r1.current[0]?.name).toBe(TOKEN_NAME_MOCK);
expect(r2.current[0]?.name).toBe(TOKEN_NAME_MOCK);
expect(r3.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('returns cached data synchronously on second mount without re-fetching', async () => {
const address = makeAddress();
const fetchMock = mockFetch(makeTokenResponse(address));
const request = [
{
type: NameType.EthereumAddress,
value: address,
variation: CHAIN_ID_MOCK,
},
];
// First mount populates the cache
const { result: result1 } = renderHook(request);
await waitFor(() => {
expect(result1.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
// Second mount should read from cache synchronously without fetching
fetchMock.mockClear();
const { result: result2 } = renderHook(request);
expect(result2.current[0]?.name).toBe(TOKEN_NAME_MOCK);
await act(async () => {
await new Promise((r) => setTimeout(r, 50));
});
expect(fetchMock).not.toHaveBeenCalled();
});
});