-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseEarnNetworkPolling.test.ts
More file actions
188 lines (166 loc) · 5.8 KB
/
useEarnNetworkPolling.test.ts
File metadata and controls
188 lines (166 loc) · 5.8 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
// Mock all the polling hooks
jest.mock('../../../hooks/AssetPolling/useTokenBalancesPolling', () =>
jest.fn(),
);
jest.mock('../../../hooks/AssetPolling/useCurrencyRatePolling', () =>
jest.fn(),
);
jest.mock('../../../hooks/AssetPolling/useTokenRatesPolling', () => jest.fn());
jest.mock('../../../hooks/AssetPolling/useAccountTrackerPolling', () =>
jest.fn(),
);
jest.mock('../../../hooks/AssetPolling/useTokenDetectionPolling', () =>
jest.fn(),
);
// Mock Engine
jest.mock('../../../../core/Engine', () => ({
context: {
NetworkController: {
findNetworkClientIdByChainId: jest.fn(),
},
TokenDetectionController: {
detectTokens: jest.fn(),
},
},
}));
import { renderHookWithProvider } from '../../../../util/test/renderWithProvider';
import useEarnNetworkPolling from './useEarnNetworkPolling';
import { RootState } from '../../../../reducers';
import { MOCK_ACCOUNTS_CONTROLLER_STATE } from '../../../../util/test/accountsControllerTestUtils';
import useTokenBalancesPolling from '../../../hooks/AssetPolling/useTokenBalancesPolling';
import useCurrencyRatePolling from '../../../hooks/AssetPolling/useCurrencyRatePolling';
import useTokenRatesPolling from '../../../hooks/AssetPolling/useTokenRatesPolling';
import useTokenDetectionPolling from '../../../hooks/AssetPolling/useTokenDetectionPolling';
import Engine from '../../../../core/Engine';
// Mock console.warn to avoid noise in tests
const originalConsoleWarn = console.warn;
beforeAll(() => {
console.warn = jest.fn();
});
afterAll(() => {
console.warn = originalConsoleWarn;
});
describe('useEarnNetworkPolling', () => {
const mockUseTokenBalancesPolling = jest.mocked(useTokenBalancesPolling);
const mockUseCurrencyRatePolling = jest.mocked(useCurrencyRatePolling);
const mockUseTokenRatesPolling = jest.mocked(useTokenRatesPolling);
const mockUseTokenDetectionPolling = jest.mocked(useTokenDetectionPolling);
const mockFindNetworkClientIdByChainId = jest.mocked(
Engine.context.NetworkController.findNetworkClientIdByChainId,
);
const mockSelectedAccount =
MOCK_ACCOUNTS_CONTROLLER_STATE.internalAccounts.accounts[
MOCK_ACCOUNTS_CONTROLLER_STATE.internalAccounts.selectedAccount
];
const mockState = {
engine: {
backgroundState: {
AccountsController: MOCK_ACCOUNTS_CONTROLLER_STATE,
AccountTreeController: {
accountTree: {
wallets: {
'keyring:test-wallet': {
id: 'test-wallet',
name: 'Test Wallet',
groups: {
'keyring:test-wallet/ethereum': {
accounts: [mockSelectedAccount.id],
},
},
},
},
},
selectedAccountGroup: 'keyring:test-wallet/ethereum',
},
PreferencesController: {
useTokenDetection: true,
},
},
},
} as unknown as RootState;
beforeEach(() => {
jest.clearAllMocks();
mockFindNetworkClientIdByChainId.mockImplementation((chainId: string) => {
if (chainId === '0x1') return 'mainnet';
if (chainId === '0x89') return 'polygon';
throw new Error(`Network client not found for chain ${chainId}`);
});
});
it('should call all polling hooks when mounted', () => {
renderHookWithProvider(() => useEarnNetworkPolling(), {
state: mockState,
});
expect(mockUseTokenBalancesPolling).toHaveBeenCalledWith({
chainIds: expect.any(Array),
});
expect(mockUseCurrencyRatePolling).toHaveBeenCalledWith({
chainIds: expect.any(Array),
});
expect(mockUseTokenRatesPolling).toHaveBeenCalledWith({
chainIds: expect.any(Array),
});
expect(mockUseTokenDetectionPolling).toHaveBeenCalledWith({
chainIds: expect.any(Array),
address: mockSelectedAccount.address,
});
});
it('should initialize with empty chain IDs and network client IDs', () => {
renderHookWithProvider(() => useEarnNetworkPolling(), {
state: mockState,
});
// Initially called with empty arrays
expect(mockUseTokenBalancesPolling).toHaveBeenCalledWith({
chainIds: [],
});
expect(mockUseCurrencyRatePolling).toHaveBeenCalledWith({
chainIds: [],
});
expect(mockUseTokenRatesPolling).toHaveBeenCalledWith({
chainIds: [],
});
expect(mockUseTokenDetectionPolling).toHaveBeenCalledWith({
chainIds: [],
address: mockSelectedAccount.address,
});
});
it('should pass empty chainIds to useTokenDetectionPolling when useTokenDetection is false', () => {
const stateWithoutTokenDetection = {
...mockState,
engine: {
...mockState.engine,
backgroundState: {
...mockState.engine.backgroundState,
PreferencesController: {
useTokenDetection: false,
},
},
},
} as unknown as RootState;
renderHookWithProvider(() => useEarnNetworkPolling(), {
state: stateWithoutTokenDetection,
});
expect(mockUseTokenDetectionPolling).toHaveBeenCalledWith({
chainIds: [],
address: mockSelectedAccount.address,
});
});
it('should return null', () => {
const { result } = renderHookWithProvider(() => useEarnNetworkPolling(), {
state: mockState,
});
expect(result.current).toBeNull();
});
it('should call all polling hooks on every render', () => {
const { rerender } = renderHookWithProvider(() => useEarnNetworkPolling(), {
state: mockState,
});
// Clear mocks and rerender
jest.clearAllMocks();
rerender({});
// All polling hooks should be called again
expect(mockUseTokenBalancesPolling).toHaveBeenCalled();
expect(mockUseCurrencyRatePolling).toHaveBeenCalled();
expect(mockUseTokenRatesPolling).toHaveBeenCalled();
expect(mockUseTokenDetectionPolling).toHaveBeenCalled();
});
});