-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePredictDeposit.test.ts
More file actions
234 lines (199 loc) · 6.26 KB
/
usePredictDeposit.test.ts
File metadata and controls
234 lines (199 loc) · 6.26 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
import { renderHook, act } from '@testing-library/react-hooks';
import { usePredictDeposit } from './usePredictDeposit';
import Engine from '../../../../core/Engine';
import Logger from '../../../../util/Logger';
import { ConfirmationLoader } from '../../../Views/confirmations/components/confirm/confirm-component';
import Routes from '../../../../constants/navigation/Routes';
const mockGoBack = jest.fn();
const mockNavigateToConfirmation = jest.fn();
const mockDepositWithConfirmation = jest.fn();
jest.mock('@react-navigation/native', () => ({
useNavigation: () => ({
goBack: mockGoBack,
}),
}));
jest.mock('../../../../core/Engine', () => ({
context: {
PredictController: {
trackPredictOrderEvent: jest.fn(),
},
},
}));
jest.mock('../../../../util/Logger', () => ({
error: jest.fn(),
}));
jest.mock('../../../../util/theme', () => {
const { mockTheme } = jest.requireActual('../../../../util/theme');
return {
useAppThemeFromContext: () => mockTheme,
};
});
jest.mock('../../../../component-library/components/Toast', () => {
const actualReact = jest.requireActual('react');
return {
ToastContext: actualReact.createContext({
toastRef: {
current: {
showToast: jest.fn(),
},
},
}),
ToastVariants: {
Icon: 'icon',
},
};
});
jest.mock('../../../Views/confirmations/hooks/useConfirmNavigation', () => ({
useConfirmNavigation: () => ({
navigateToConfirmation: mockNavigateToConfirmation,
}),
}));
jest.mock('./usePredictTrading', () => ({
usePredictTrading: () => ({
deposit: mockDepositWithConfirmation,
}),
}));
jest.mock('../selectors/predictController', () => ({
selectPredictPendingDepositByAddress: () => () => null,
}));
jest.mock('../utils/accounts', () => ({
getEvmAccountFromSelectedAccountGroup: () => ({
address: '0x1234567890123456789012345678901234567890',
}),
}));
jest.mock(
'../../../../selectors/multichainAccounts/accountTreeController',
() => ({
selectSelectedAccountGroupId: jest.fn(() => 'mock-account-group-id'),
}),
);
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: (selector: () => unknown) => selector(),
}));
// Mock the entire confirm-component to avoid deep dependency chain
jest.mock(
'../../../Views/confirmations/components/confirm/confirm-component',
() => ({
__esModule: true,
default: () => null,
ConfirmationLoader: {
Default: 'default',
CustomAmount: 'customAmount',
PredictClaim: 'predictClaim',
Transfer: 'transfer',
},
}),
);
// Mock useConfirmationAlerts to avoid deep dependency chain
jest.mock(
'../../../Views/confirmations/hooks/alerts/useConfirmationAlerts',
() => ({
useConfirmationAlerts: () => ({
alerts: [],
}),
}),
);
// Mock useInsufficientBalanceAlert
jest.mock(
'../../../Views/confirmations/hooks/alerts/useInsufficientBalanceAlert',
() => ({
useInsufficientBalanceAlert: () => undefined,
}),
);
// Mock useRampNavigation
jest.mock('../../../UI/Ramp/hooks/useRampNavigation', () => ({
useRampNavigation: () => ({
goToBuy: jest.fn(),
}),
}));
describe('usePredictDeposit', () => {
beforeEach(() => {
jest.clearAllMocks();
mockDepositWithConfirmation.mockReturnValue(Promise.resolve());
});
afterEach(() => {
jest.resetAllMocks();
});
it('returns deposit function and isDepositPending flag', () => {
// Arrange & Act
const { result } = renderHook(() => usePredictDeposit());
// Assert
expect(result.current.deposit).toBeDefined();
expect(typeof result.current.deposit).toBe('function');
expect(result.current.isDepositPending).toBe(false);
});
it('calls navigateToConfirmation when deposit is called', async () => {
// Arrange
const { result } = renderHook(() => usePredictDeposit());
// Act
await act(async () => {
await result.current.deposit();
});
// Assert
expect(mockNavigateToConfirmation).toHaveBeenCalledWith({
loader: ConfirmationLoader.CustomAmount,
stack: Routes.PREDICT.ROOT,
});
});
it('calls depositWithConfirmation when deposit is called', async () => {
// Arrange
const { result } = renderHook(() => usePredictDeposit());
// Act
await act(async () => {
await result.current.deposit();
// Allow fire-and-forget depositWithConfirmation to be called
await Promise.resolve();
});
// Assert
expect(mockDepositWithConfirmation).toHaveBeenCalledWith({});
});
it('does not fire initiated event (removed to fix double-fire — INITIATED is now only fired from PredictBuyPreview mount)', async () => {
// Arrange
const { result } = renderHook(() => usePredictDeposit());
const analyticsParams = {
amountUsd: 100,
analyticsProperties: {
marketId: 'test-market',
},
};
// Act
await act(async () => {
await result.current.deposit(analyticsParams);
await Promise.resolve();
});
// Assert — deposit no longer fires INITIATED; it was causing a double-fire
// with the PredictBuyPreview mount effect which is the single source of truth.
expect(
Engine.context.PredictController.trackPredictOrderEvent,
).not.toHaveBeenCalled();
});
it('logs error and shows toast when deposit fails', async () => {
// Arrange
const mockError = new Error('Deposit failed');
mockDepositWithConfirmation.mockRejectedValue(mockError);
const { result } = renderHook(() => usePredictDeposit());
// Act
await act(async () => {
await result.current.deposit();
// Allow fire-and-forget promise to settle and error handler to run
await new Promise((resolve) => setTimeout(resolve, 10));
});
// Assert
expect(Logger.error).toHaveBeenCalled();
});
it('navigates back when deposit fails', async () => {
// Arrange
const mockError = new Error('Deposit failed');
mockDepositWithConfirmation.mockRejectedValue(mockError);
const { result } = renderHook(() => usePredictDeposit());
// Act
await act(async () => {
await result.current.deposit();
// Allow fire-and-forget promise to settle and error handler to run
await new Promise((resolve) => setTimeout(resolve, 10));
});
// Assert
expect(mockGoBack).toHaveBeenCalled();
});
});