-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.test.tsx
More file actions
269 lines (218 loc) · 8.17 KB
/
Copy pathindex.test.tsx
File metadata and controls
269 lines (218 loc) · 8.17 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import React from 'react';
import DeleteWalletModal from './';
import { backgroundState } from '../../../util/test/initial-root-state';
import { fireEvent } from '@testing-library/react-native';
import renderWithProvider, {
DeepPartial,
} from '../../../util/test/renderWithProvider';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { RootState } from '../../../reducers';
import { strings } from '../../../../locales/i18n';
import { ForgotPasswordModalSelectorsIDs } from '../../../util/ForgotPasswordModal.testIds';
import { InteractionManager } from 'react-native';
import { clearHistory } from '../../../actions/browser';
import { Authentication } from '../../../core/Authentication/Authentication';
const mockInitialState = {
engine: { backgroundState },
security: {
dataCollectionForMarketing: false,
},
};
const mockUseDispatch = jest.fn();
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: () => mockUseDispatch,
useSelector: jest.fn(),
}));
const mockNavigate = jest.fn();
// Mock useRoute with default params
const mockUseRoute = jest.fn().mockReturnValue({ params: {} });
jest.mock('@react-navigation/native', () => {
const actualNav = jest.requireActual('@react-navigation/native');
return {
...actualNav,
useNavigation: () => ({
navigate: mockNavigate,
goBack: jest.fn(),
reset: jest.fn(),
}),
useRoute: () => mockUseRoute(),
};
});
jest.mock('@react-native-cookies/cookies', () => ({
set: jest.fn(),
get: jest.fn(),
clearAll: jest.fn(),
}));
jest.mock('../../../actions/browser', () => ({
clearHistory: jest.fn(),
BrowserActionTypes: {
ADD_TO_VIEWED_DAPP: 'ADD_TO_VIEWED_DAPP',
},
}));
const mockSignOut = jest.fn();
jest.mock('../../../util/identity/hooks/useAuthentication', () => ({
useSignOut: () => ({
signOut: mockSignOut,
}),
}));
jest.mock('../../../core/Authentication/Authentication', () => ({
Authentication: {
deleteWallet: jest.fn(() => Promise.resolve()),
},
}));
const Stack = createNativeStackNavigator();
const renderComponent = (
state: DeepPartial<RootState> = {},
routeParams = {},
) => {
// Update the mock to return the provided params
mockUseRoute.mockReturnValue({ params: routeParams });
return renderWithProvider(
<Stack.Navigator>
<Stack.Screen name="DeleteWalletModal" options={{}}>
{() => <DeleteWalletModal />}
</Stack.Screen>
</Stack.Navigator>,
{ state },
);
};
describe('DeleteWalletModal', () => {
const mockRunAfterInteractions = jest.fn().mockImplementation((cb) => {
cb();
return {
then: (onfulfilled: () => void) => Promise.resolve(onfulfilled()),
done: (onfulfilled: () => void, onrejected: () => void) =>
Promise.resolve().then(onfulfilled, onrejected),
cancel: jest.fn(),
};
});
jest
.spyOn(InteractionManager, 'runAfterInteractions')
.mockImplementation(mockRunAfterInteractions);
describe('bottom sheet', () => {
it('renders the forgot password description', () => {
const wrapper = renderComponent(mockInitialState);
expect(
wrapper.getByText(strings('login.forgot_password_desc')),
).toBeOnTheScreen();
});
});
describe('forgot password flow', () => {
it('renders matching snapshot for forgot password', async () => {
const wrapper = renderComponent(mockInitialState);
const title = wrapper.getByText(strings('login.forgot_password_desc'));
expect(title).toBeOnTheScreen();
const button = wrapper.getByRole('button', {
name: strings('login.reset_wallet'),
});
expect(button).toBeOnTheScreen();
fireEvent.press(button);
const title2 = wrapper.getByText(strings('login.are_you_sure'));
expect(title2).toBeOnTheScreen();
const button2 = wrapper.getByRole('button', {
name: strings('login.erase_my'),
});
expect(button2).toBeOnTheScreen();
fireEvent.press(button2);
// Wait for all promises to resolve
await Promise.resolve();
expect(mockSignOut).toHaveBeenCalled();
});
it('signs the user out when deleting the wallet', async () => {
const { getByTestId } = renderComponent(mockInitialState);
fireEvent.press(
getByTestId(ForgotPasswordModalSelectorsIDs.RESET_WALLET_BUTTON),
);
fireEvent.press(
getByTestId(ForgotPasswordModalSelectorsIDs.YES_RESET_WALLET_BUTTON),
);
expect(mockSignOut).toHaveBeenCalled();
});
it('calls deleteWallet when deleting the wallet', async () => {
const { getByTestId } = renderComponent(mockInitialState);
fireEvent.press(
getByTestId(ForgotPasswordModalSelectorsIDs.RESET_WALLET_BUTTON),
);
fireEvent.press(
getByTestId(ForgotPasswordModalSelectorsIDs.YES_RESET_WALLET_BUTTON),
);
// Wait for async operations
await Promise.resolve();
expect(Authentication.deleteWallet).toHaveBeenCalled();
});
});
describe('reset wallet flow passed as route param', () => {
it('shows reset wallet confirmation when isResetWalletFromParams is true', () => {
const wrapper = renderComponent(mockInitialState, {
isResetWallet: true,
});
// Should show the confirmation screen directly (not the forgot password screen)
const title = wrapper.getByText(strings('login.are_you_sure'));
expect(title).toBeOnTheScreen();
// Should not show the forgot password description
expect(
wrapper.queryByText(strings('login.forgot_password_desc')),
).toBeNull();
// Should not show the back button when coming from params
const backButton = wrapper.queryByRole('button', { name: /arrow left/i });
expect(backButton).toBeNull();
});
it('shows forgot password flow when isResetWalletFromParams is false', () => {
const wrapper = renderComponent(mockInitialState, {
isResetWallet: false,
});
// Should show the forgot password screen first
const title = wrapper.getByText(strings('login.forgot_password_desc'));
expect(title).toBeOnTheScreen();
// Should not show the confirmation screen initially
expect(wrapper.queryByText(strings('login.are_you_sure'))).toBeNull();
});
it('shows back button when not coming from params', () => {
const wrapper = renderComponent(mockInitialState, {
isResetWallet: false,
});
// Click the reset wallet button to show confirmation screen
const resetButton = wrapper.getByRole('button', {
name: strings('login.reset_wallet'),
});
fireEvent.press(resetButton);
// Should show the confirmation screen with back button
const title = wrapper.getByText(strings('login.are_you_sure'));
expect(title).toBeOnTheScreen();
// Should show the back button when not coming from params
const backButton = wrapper.getByTestId(
ForgotPasswordModalSelectorsIDs.BACK_BUTTON,
);
expect(backButton).toBeTruthy();
});
});
describe('error handling', () => {
it('handles errors during wallet deletion and resets loading state', async () => {
// Arrange - Mock console.error to track error logging
const consoleSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => undefined);
// Mock clearHistory to return a thunk that throws an error
const mockClearHistoryThunk = jest
.fn()
.mockRejectedValue(new Error('Test error'));
(clearHistory as jest.Mock).mockReturnValue(mockClearHistoryThunk);
const { getByTestId } = renderComponent(mockInitialState);
// Act - Trigger the reset wallet flow and attempt deletion
fireEvent.press(
getByTestId(ForgotPasswordModalSelectorsIDs.RESET_WALLET_BUTTON),
);
const deleteButton = getByTestId(
ForgotPasswordModalSelectorsIDs.YES_RESET_WALLET_BUTTON,
);
fireEvent.press(deleteButton);
// Wait for async operations to complete
await new Promise((resolve) => setTimeout(resolve, 0));
// Assert - Verify error was logged and loading state was reset
expect(consoleSpy).toHaveBeenCalled();
// Cleanup
consoleSpy.mockRestore();
});
});
});