-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseWithdrawValidation.test.ts
More file actions
247 lines (206 loc) · 6.78 KB
/
useWithdrawValidation.test.ts
File metadata and controls
247 lines (206 loc) · 6.78 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
241
242
243
244
245
246
247
import { renderHook } from '@testing-library/react-hooks';
import Engine from '../../../../core/Engine';
import { WITHDRAWAL_CONSTANTS } from '@metamask/perps-controller';
import { useWithdrawValidation } from './useWithdrawValidation';
// Mock Engine
jest.mock('../../../../core/Engine', () => ({
context: {
PerpsController: {
getWithdrawalRoutes: jest.fn(),
},
},
}));
// Mock other hooks
jest.mock('./index', () => ({
usePerpsNetwork: jest.fn(),
}));
jest.mock('./stream', () => ({
usePerpsLiveAccount: jest.fn(),
}));
// Mock i18n
jest.mock('../../../../../locales/i18n', () => ({
strings: jest.fn((key: string, params?: Record<string, unknown>) => {
if (key === 'perps.withdrawal.insufficient_funds') {
return 'Insufficient funds';
}
if (key === 'perps.withdrawal.minimum_amount_error') {
return params?.amount
? `Minimum amount: ${params.amount}`
: 'Minimum amount required';
}
if (key === 'perps.withdrawal.enter_amount') {
return 'Enter amount';
}
if (key === 'perps.withdrawal.withdraw_usdc') {
return 'Withdraw USDC';
}
return key;
}),
}));
import { usePerpsNetwork } from './index';
import { usePerpsLiveAccount } from './stream';
describe('useWithdrawValidation', () => {
const mockRoute = {
assetId:
'eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831/default',
chainId: 'eip155:42161',
constraints: {
minAmount: '2.00',
fees: {
fixed: 1,
token: 'USDC',
},
},
};
beforeEach(() => {
jest.clearAllMocks();
(usePerpsLiveAccount as jest.Mock).mockReturnValue({
account: {
availableBalance: '$1000.00',
},
isInitialLoading: false,
});
(usePerpsNetwork as jest.Mock).mockReturnValue('mainnet');
(
Engine.context.PerpsController.getWithdrawalRoutes as jest.Mock
).mockReturnValue([mockRoute]);
});
it('should parse available balance correctly', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.availableBalance).toBe('1000');
});
it('prefers availableToTradeBalance for Unified Account target state', () => {
(usePerpsLiveAccount as jest.Mock).mockReturnValue({
account: {
availableBalance: '$0.00',
availableToTradeBalance: '$2500.00',
},
isInitialLoading: false,
});
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.availableBalance).toBe('2500');
expect(result.current.hasInsufficientBalance).toBe(false);
});
it('should handle empty balance', () => {
(usePerpsLiveAccount as jest.Mock).mockReturnValue({
account: {
availableBalance: null,
},
isInitialLoading: false,
});
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.availableBalance).toBe('0');
});
it('should detect insufficient balance', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '1500' }),
);
expect(result.current.hasInsufficientBalance).toBe(true);
});
it('should truncate available balance to 2 decimal places for validation', () => {
(usePerpsLiveAccount as jest.Mock).mockReturnValue({
account: {
availableBalance: '$16.069',
},
isInitialLoading: false,
});
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '16.06' }),
);
expect(result.current.availableBalance).toBe('16.06');
expect(result.current.hasInsufficientBalance).toBe(false);
});
it('should show insufficient balance when typing more than truncated balance', () => {
(usePerpsLiveAccount as jest.Mock).mockReturnValue({
account: {
availableBalance: '$16.069',
},
isInitialLoading: false,
});
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '16.07' }),
);
expect(result.current.hasInsufficientBalance).toBe(true);
});
it('should detect amount below minimum', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '1.5' }),
);
expect(result.current.isBelowMinimum).toBe(true);
});
it('should validate valid amount', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.hasInsufficientBalance).toBe(false);
expect(result.current.isBelowMinimum).toBe(false);
expect(result.current.hasAmount).toBe(true);
});
it('should use default minimum when route constraints missing', () => {
(
Engine.context.PerpsController.getWithdrawalRoutes as jest.Mock
).mockReturnValue([]);
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '1' }),
);
// Default minimum is 1.01
expect(result.current.isBelowMinimum).toBe(true);
expect(result.current.getMinimumAmount()).toBe(
Number.parseFloat(WITHDRAWAL_CONSTANTS.DefaultMinAmount),
);
});
describe('getButtonLabel', () => {
it('should return insufficient funds message', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '1500' }),
);
expect(result.current.getButtonLabel()).toBe('Insufficient funds');
});
it('should return minimum amount error', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '1' }),
);
expect(result.current.getButtonLabel()).toBe('Minimum amount: 2');
});
it('should return enter amount message', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '' }),
);
expect(result.current.getButtonLabel()).toBe('Enter amount');
});
it('should return withdraw message for valid amount', () => {
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.getButtonLabel()).toBe('Withdraw USDC');
});
});
it('should handle testnet network', () => {
(usePerpsNetwork as jest.Mock).mockReturnValue('testnet');
const testnetRoute = {
assetId:
'eip155:421614/erc20:0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d/default',
chainId: 'eip155:421614',
constraints: {
minAmount: '2.00',
fees: {
fixed: 1,
token: 'USDC',
},
},
};
(
Engine.context.PerpsController.getWithdrawalRoutes as jest.Mock
).mockReturnValue([testnetRoute]);
const { result } = renderHook(() =>
useWithdrawValidation({ withdrawAmount: '100' }),
);
expect(result.current.withdrawalRoute).toEqual(testnetRoute);
});
});