-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathNetworkConnectionBanner.test.tsx
More file actions
233 lines (201 loc) · 6.94 KB
/
NetworkConnectionBanner.test.tsx
File metadata and controls
233 lines (201 loc) · 6.94 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
import React from 'react';
import { fireEvent } from '@testing-library/react-native';
import { useNetworkConnectionBanner } from '../../hooks/useNetworkConnectionBanner';
import NetworkConnectionBanner from './NetworkConnectionBanner';
import renderWithProvider from '../../../util/test/renderWithProvider';
jest.mock('../../hooks/useNetworkConnectionBanner');
jest.mock('../../../util/theme', () => {
const { mockTheme } = jest.requireActual('../../../util/theme');
return {
useAppTheme: jest.fn(() => mockTheme),
mockTheme,
};
});
// Necessary because we mock SVGs by default
jest.mock('@metamask/design-system-react-native', () => {
const Icon = ({ size }: { size: string }) => {
// We can't import this at the top because the mock gets hoisted before any imports.
const { View, Text } = jest.requireActual('react-native');
return (
<View testID="icon">
<Text testID="icon-size">{size}</Text>
</View>
);
};
return {
__esModule: true,
...jest.requireActual('@metamask/design-system-react-native'),
Icon,
};
});
const useNetworkConnectionBannerMock = jest.mocked(useNetworkConnectionBanner);
describe('NetworkConnectionBanner', () => {
const mockUpdateRpc = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
});
describe('when banner is not visible', () => {
it('does not render when visible is false', () => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: { visible: false },
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { root } = renderWithProvider(<NetworkConnectionBanner />);
expect(root).toBeUndefined();
});
});
describe('when banner is visible', () => {
const customNetworkStatusTestCases = [
{
status: 'degraded' as const,
expectedMessage: 'Still connecting to Polygon Mainnet...',
updateRpcButtonText: 'Update RPC',
},
{
status: 'unavailable' as const,
expectedMessage: 'Unable to connect to Polygon Mainnet.',
updateRpcButtonText: 'update RPC',
},
];
const infuraNetworkStatusTestCases = [
{
status: 'degraded' as const,
expectedMessage: 'Still connecting to Ethereum Mainnet...',
},
{
status: 'unavailable' as const,
expectedMessage: 'Unable to connect to Ethereum Mainnet.',
},
];
it.each(customNetworkStatusTestCases)(
'renders the banner with correct structure for $status status with custom network',
({ status, expectedMessage, updateRpcButtonText }) => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: {
visible: true,
chainId: '0x89',
status,
networkName: 'Polygon Mainnet',
rpcUrl: 'https://polygon-rpc.com',
isInfuraEndpoint: false,
},
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { getByTestId, getByText } = renderWithProvider(
<NetworkConnectionBanner />,
);
expect(getByTestId('icon')).toBeTruthy();
expect(getByText(expectedMessage)).toBeTruthy();
expect(getByText(updateRpcButtonText)).toBeTruthy();
},
);
it.each(infuraNetworkStatusTestCases)(
'renders the banner with correct structure for $status status with Infura network',
({ status, expectedMessage }) => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: {
visible: true,
chainId: '0x1',
status,
networkName: 'Ethereum Mainnet',
rpcUrl: 'https://mainnet.infura.io/v3/test',
isInfuraEndpoint: true,
},
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { getByTestId, getByText, queryByText } = renderWithProvider(
<NetworkConnectionBanner />,
);
expect(getByTestId('icon')).toBeTruthy();
expect(getByText(expectedMessage)).toBeTruthy();
expect(queryByText('Update RPC')).toBeNull();
expect(queryByText('update RPC')).toBeNull();
},
);
it.each(customNetworkStatusTestCases)(
'calls updateRpc when Update RPC button is pressed for $status status',
({ status, updateRpcButtonText }) => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: {
visible: true,
chainId: '0x89',
status,
networkName: 'Polygon Mainnet',
rpcUrl: 'https://polygon-rpc.com',
isInfuraEndpoint: false,
},
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { getByText } = renderWithProvider(<NetworkConnectionBanner />);
const updateButton = getByText(updateRpcButtonText);
fireEvent.press(updateButton);
expect(mockUpdateRpc).toHaveBeenCalledWith(
'https://polygon-rpc.com',
status,
'0x89',
);
},
);
});
describe('edge cases', () => {
const emptyNameTestCases = [
{
status: 'degraded' as const,
expectedMessage: 'Still connecting to ...',
},
{
status: 'unavailable' as const,
expectedMessage: 'Unable to connect to .',
},
];
it.each(emptyNameTestCases)(
'handles network with empty name for $status status',
({ status, expectedMessage }) => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: {
visible: true,
chainId: '0x1',
status,
networkName: '',
rpcUrl: 'https://mainnet.infura.io/v3/test',
isInfuraEndpoint: true,
},
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { getByText } = renderWithProvider(<NetworkConnectionBanner />);
expect(getByText(expectedMessage)).toBeTruthy();
},
);
it('handles multiple rapid button presses', () => {
useNetworkConnectionBannerMock.mockReturnValue({
networkConnectionBannerState: {
visible: true,
chainId: '0x89',
status: 'degraded',
networkName: 'Polygon Mainnet',
rpcUrl: 'https://polygon-rpc.com',
isInfuraEndpoint: false,
},
updateRpc: mockUpdateRpc,
switchToInfura: jest.fn(),
});
const { getByText } = renderWithProvider(<NetworkConnectionBanner />);
const updateButton = getByText('Update RPC');
// Press button multiple times rapidly
fireEvent.press(updateButton);
fireEvent.press(updateButton);
fireEvent.press(updateButton);
expect(mockUpdateRpc).toHaveBeenCalledTimes(3);
expect(mockUpdateRpc).toHaveBeenCalledWith(
'https://polygon-rpc.com',
'degraded',
'0x89',
);
});
});
});