forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnlockModal.test.tsx
More file actions
255 lines (215 loc) · 8.42 KB
/
Copy pathUnlockModal.test.tsx
File metadata and controls
255 lines (215 loc) · 8.42 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
248
249
250
251
252
253
254
255
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import UnlockModal from './UnlockModal';
import { useFarmStore } from '@/store/farmStore';
import { useStellarWallet } from '@/context/StellarWalletContext';
import { useUnlockAssetsFeePreview } from '@/hooks/useSorobanQuery';
import { unlockAssets } from '@/lib/soroban';
import { useCountdown } from '@/hooks/useCountdown';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ChakraProvider } from '@chakra-ui/react';
import { createElement, type ReactNode } from 'react';
// Mock dependencies
vi.mock('@/store/farmStore', () => ({
useFarmStore: vi.fn(),
}));
vi.mock('@/context/StellarWalletContext', () => ({
useStellarWallet: vi.fn(),
}));
vi.mock('@/hooks/useSorobanQuery', () => ({
useUnlockAssetsFeePreview: vi.fn(),
QUERY_KEYS: {
USER_POSITION: 'userPosition',
USER_CREDITS: 'userCredits',
POOLS: 'pools',
PLATFORM_STATS: 'platformStats',
}
}));
vi.mock('@/lib/soroban', () => ({
unlockAssets: vi.fn(),
computePartialUnlockPreview: vi.fn((lockedAmount: number, unlockAmount: number, dailyRate: number) => ({
remainingStake: Math.max(0, lockedAmount - unlockAmount),
newDailyRate: lockedAmount > 0 ? (Math.max(0, lockedAmount - unlockAmount) / lockedAmount) * dailyRate : 0,
})),
getContractErrorMessage: vi.fn(() => undefined),
stellarExpertTxUrl: vi.fn((hash: string) => `https://stellar.expert/explorer/testnet/tx/${hash}`),
}));
vi.mock('@/hooks/useCountdown', () => ({
useCountdown: vi.fn(),
}));
vi.mock('@/context/ErrorContext', () => ({
useErrorHandler: vi.fn(() => ({
success: vi.fn(),
error: vi.fn(),
handleError: vi.fn((err: unknown) => ({
userMessage: err instanceof Error ? err.message : String(err),
code: 'ERR',
message: err instanceof Error ? err.message : String(err),
})),
})),
}));
vi.mock('@/lib/analytics', () => ({
trackEvent: vi.fn(),
}));
function createQueryClient() {
return new QueryClient({
defaultOptions: {
queries: { retry: false },
},
});
}
function renderWithProviders(ui: ReactNode) {
const queryClient = createQueryClient();
return render(
createElement(ChakraProvider, null,
createElement(QueryClientProvider, { client: queryClient }, ui)
)
);
}
const mockPosition = {
id: 'pool-1',
contractAddress: 'test-contract-id',
symbol: 'XLM',
name: 'XLM Pool',
lockedAmount: 100,
lockedAt: Date.now() - 100000,
lockPeriodSeconds: 60,
dailyRate: '0.1',
minDepositAmount: 1,
img: '',
earned: '10',
stake: '100',
totalStakedLiquidity: '$1000',
};
describe('UnlockModal', () => {
beforeEach(() => {
vi.clearAllMocks();
// Default mock implementations
vi.mocked(useFarmStore).mockImplementation((selector: (state: Record<string, unknown>) => unknown) => {
const state = {
activeModal: 'unlock',
selectedPosition: mockPosition,
close: vi.fn(),
};
return selector(state);
});
vi.mocked(useStellarWallet).mockReturnValue({
publicKey: 'G_TEST_USER',
walletApi: { signTransaction: vi.fn() } as never,
isNetworkMismatch: false,
isConnected: true,
connect: vi.fn(),
disconnect: vi.fn(),
} as ReturnType<typeof useStellarWallet>);
vi.mocked(useUnlockAssetsFeePreview).mockReturnValue({
data: { feePreview: '100' },
isFetching: false,
} as ReturnType<typeof useUnlockAssetsFeePreview>);
vi.mocked(useCountdown).mockReturnValue({
isElapsed: true,
remainingMs: 0,
label: 'Unlocked',
});
});
it('renders modal when activeModal is "unlock"', () => {
renderWithProviders(createElement(UnlockModal));
expect(screen.getByText('Unlock XLM')).toBeTruthy();
expect(screen.getByText('Amount locked')).toBeTruthy();
// "100 XLM" appears in both "Amount locked" and "Available to unlock"
expect(screen.getAllByText('100 XLM').length).toBeGreaterThanOrEqual(1);
});
it('does not render modal content when position is null', () => {
vi.mocked(useFarmStore).mockImplementation((selector: (state: Record<string, unknown>) => unknown) => {
return selector({ activeModal: 'unlock', selectedPosition: null, close: vi.fn() });
});
renderWithProviders(createElement(UnlockModal));
// Modal should not show Unlock header
expect(screen.queryByText('Unlock XLM')).toBeNull();
});
it('shows countdown warning and disables input if lock period not elapsed', () => {
vi.mocked(useCountdown).mockReturnValue({
isElapsed: false,
remainingMs: 50000,
label: '1 hour remaining',
});
renderWithProviders(createElement(UnlockModal));
expect(screen.getByText(/Assets are time-locked for security/i)).toBeTruthy();
const input = screen.getByPlaceholderText('Amount') as HTMLInputElement;
expect(input.disabled).toBe(true);
const button = screen.getByRole('button', { name: /Unlock with Freighter/i });
expect(button).toHaveProperty('disabled', true);
});
it('validates amount: below minimum', () => {
renderWithProviders(createElement(UnlockModal));
const input = screen.getByPlaceholderText('Amount');
fireEvent.change(input, { target: { value: '0.005' } });
expect(screen.getByText('Minimum unlock amount is 0.01 XLM.')).toBeTruthy();
const button = screen.getByRole('button', { name: /Unlock with Freighter/i });
expect(button).toHaveProperty('disabled', true);
});
it('validates amount: exceeds balance', () => {
renderWithProviders(createElement(UnlockModal));
const input = screen.getByPlaceholderText('Amount');
fireEvent.change(input, { target: { value: '150' } });
expect(screen.getByText('Amount exceeds locked balance of 100 XLM.')).toBeTruthy();
const button = screen.getByRole('button', { name: /Unlock with Freighter/i });
expect(button).toHaveProperty('disabled', true);
});
it('sets 50% and max amounts correctly', () => {
renderWithProviders(createElement(UnlockModal));
const input = screen.getByPlaceholderText('Amount') as HTMLInputElement;
// Test 50%
fireEvent.click(screen.getByText('50%'));
expect(input.value).toBe('50');
// Test Max
fireEvent.click(screen.getByText('Max'));
expect(input.value).toBe('100');
});
it('handles successful unlock flow', async () => {
vi.mocked(unlockAssets).mockImplementation(async (args: Record<string, unknown>) => {
const onStep = args.onStep as ((s: string) => void) | undefined;
const onHash = args.onHash as ((h: string) => void) | undefined;
onStep?.('simulating');
onStep?.('signing');
onStep?.('submitting');
onHash?.('test-hash-abc');
return { success: true, hash: 'test-hash-abc', status: 'SUCCESS' };
});
renderWithProviders(createElement(UnlockModal));
const input = screen.getByPlaceholderText('Amount');
fireEvent.change(input, { target: { value: '50' } });
const button = screen.getByRole('button', { name: /Unlock with Freighter/i });
fireEvent.click(button);
// Wait for the modal to show success state — the Badge text
await waitFor(() => {
expect(screen.getByText('Unlock confirmed')).toBeTruthy();
});
// Check the explorer link
const link = screen.getByRole('link', { name: /View on Stellar Expert/i });
expect(link.getAttribute('href')).toContain('test-hash-abc');
});
it('handles unlock error flow — result.success === false', async () => {
vi.mocked(unlockAssets).mockImplementation(async (args: Record<string, unknown>) => {
const onStep = args.onStep as ((s: string) => void) | undefined;
onStep?.('simulating');
return {
success: false,
status: 'FAILED',
error: 'Insufficient balance to cover this transaction. Please ensure your wallet has enough funds.',
};
});
renderWithProviders(createElement(UnlockModal));
const input = screen.getByPlaceholderText('Amount');
fireEvent.change(input, { target: { value: '50' } });
const button = screen.getByRole('button', { name: /Unlock with Freighter/i });
fireEvent.click(button);
await waitFor(() => {
expect(screen.getByText(/Insufficient balance/i)).toBeTruthy();
});
// Button should be re-enabled after error
await waitFor(() => {
const btn = screen.getByRole('button', { name: /Unlock with Freighter/i });
expect(btn).toHaveProperty('disabled', false);
});
});
});