-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseERC20Tokens.test.ts
More file actions
112 lines (93 loc) · 3.07 KB
/
useERC20Tokens.test.ts
File metadata and controls
112 lines (93 loc) · 3.07 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
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';
const TOKEN_ADDRESS_MOCK = '0x0439e60f02a8900a951603950d8d4527f400c3f1';
const CHAIN_ID_MOCK = CHAIN_IDS.MAINNET;
const ASSET_ID_MOCK = `eip155:1/erc20:${TOKEN_ADDRESS_MOCK}`;
jest.mock('../useTokensData/useTokensData', () => ({
useTokensData: jest.fn(),
}));
import { useTokensData } from '../useTokensData/useTokensData';
const mockUseTokensData = useTokensData as jest.Mock;
function renderHook(requests: Parameters<typeof useERC20Tokens>[0]) {
return renderHookWithProvider(() => useERC20Tokens(requests), { state: {} });
}
describe('useERC20Tokens', () => {
beforeEach(() => {
jest.clearAllMocks();
mockUseTokensData.mockReturnValue({
[ASSET_ID_MOCK]: {
assetId: ASSET_ID_MOCK,
name: TOKEN_NAME_MOCK,
symbol: TOKEN_SYMBOL_MOCK,
iconUrl: TOKEN_ICON_URL_MOCK,
},
});
});
it('returns undefined if type is not EthereumAddress', () => {
const { result } = renderHook([
{
type: 'alternateType' as NameType,
value: TOKEN_ADDRESS_MOCK,
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]).toBeUndefined();
});
it('returns name when token is found', () => {
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: TOKEN_ADDRESS_MOCK,
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
it('returns symbol when preferContractSymbol is true', () => {
const { result } = renderHook([
{
preferContractSymbol: true,
type: NameType.EthereumAddress,
value: TOKEN_ADDRESS_MOCK,
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]?.name).toBe(TOKEN_SYMBOL_MOCK);
});
it('returns image when token is found', () => {
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: TOKEN_ADDRESS_MOCK,
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]?.image).toBe(TOKEN_ICON_URL_MOCK);
});
it('returns name and image as undefined when token is not found', () => {
mockUseTokensData.mockReturnValue({});
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: TOKEN_ADDRESS_MOCK,
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]).toEqual({ name: undefined, image: undefined });
});
it('normalizes addresses to lowercase when building the asset ID', () => {
const { result } = renderHook([
{
type: NameType.EthereumAddress,
value: TOKEN_ADDRESS_MOCK.toUpperCase(),
variation: CHAIN_ID_MOCK,
},
]);
expect(result.current[0]?.name).toBe(TOKEN_NAME_MOCK);
});
});