-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTabBar.test.tsx
More file actions
258 lines (232 loc) · 7.24 KB
/
TabBar.test.tsx
File metadata and controls
258 lines (232 loc) · 7.24 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
// Third party dependencies.
import React from 'react';
import { fireEvent } from '@testing-library/react-native';
import {
ParamListBase,
TabNavigationState,
NavigationHelpers,
} from '@react-navigation/native';
// External dependencies
import renderWithProvider from '../../../../util/test/renderWithProvider';
import { backgroundState } from '../../../../util/test/initial-root-state';
// Internal dependencies
import TabBar from './TabBar';
import { TabBarIconKey, ExtendedBottomTabDescriptor } from './TabBar.types';
import Routes from '../../../../constants/navigation/Routes';
import { selectAssetsTrendingTokensEnabled } from '../../../../selectors/featureFlagController/assetsTrendingTokens';
// Minimal descriptor interface for tests - only includes what TabBar component uses
interface TestTabDescriptor {
options: {
tabBarIconKey: TabBarIconKey;
rootScreenName: string;
callback?: () => void;
};
}
interface TestDescriptors {
[key: string]: TestTabDescriptor;
}
// Mock trending tokens feature flag selector
jest.mock('../../../../selectors/featureFlagController/assetsTrendingTokens');
// Mock the navigation object with proper typing
const navigation: NavigationHelpers<ParamListBase> = {
navigate: jest.fn(),
goBack: jest.fn(),
reset: jest.fn(),
setParams: jest.fn(),
dispatch: jest.fn(),
isFocused: jest.fn(),
canGoBack: jest.fn(),
dangerouslyGetParent: jest.fn(),
dangerouslyGetState: jest.fn(),
emit: jest.fn(),
};
const mockInitialState = {
engine: {
backgroundState: {
...backgroundState,
RemoteFeatureFlagController: {
remoteFeatureFlags: {
rewardsEnabled: {
enabled: true,
minimumVersion: '0.0.1',
},
},
cacheTimestamp: 0,
},
},
},
};
// Define the test cases.
describe('TabBar', () => {
beforeEach(() => {
jest.clearAllMocks();
});
const state = {
index: 0,
routes: [
{ key: '1', name: 'Tab 1' },
{ key: '2', name: 'Tab 2' },
{ key: '3', name: 'Tab 3' },
{ key: '4', name: 'Tab 4' },
{ key: '5', name: 'Tab 5' },
],
};
const descriptors: TestDescriptors = {
'1': {
options: {
tabBarIconKey: TabBarIconKey.Wallet,
rootScreenName: Routes.WALLET_VIEW,
},
},
'2': {
options: {
tabBarIconKey: TabBarIconKey.Browser,
rootScreenName: Routes.BROWSER.VIEW,
},
},
'3': {
options: {
tabBarIconKey: TabBarIconKey.Actions,
rootScreenName: Routes.MODAL.WALLET_ACTIONS,
},
},
'4': {
options: {
tabBarIconKey: TabBarIconKey.Activity,
rootScreenName: Routes.TRANSACTIONS_VIEW,
},
},
'5': {
options: {
tabBarIconKey: TabBarIconKey.Setting,
rootScreenName: Routes.SETTINGS_VIEW,
},
},
};
it('renders correctly', () => {
const { toJSON } = renderWithProvider(
<TabBar
state={state as TabNavigationState<ParamListBase>}
descriptors={descriptors as Record<string, ExtendedBottomTabDescriptor>}
navigation={navigation}
/>,
{ state: mockInitialState },
);
expect(toJSON()).toMatchSnapshot();
});
it('navigates to the correct screen when a tab is pressed', () => {
const { getByTestId } = renderWithProvider(
<TabBar
state={state as TabNavigationState<ParamListBase>}
descriptors={descriptors as Record<string, ExtendedBottomTabDescriptor>}
navigation={navigation}
/>,
{ state: mockInitialState },
);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Wallet}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.WALLET.HOME, {
screen: Routes.WALLET.TAB_STACK_FLOW,
params: {
screen: Routes.WALLET_VIEW,
},
});
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Browser}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.BROWSER.HOME, {
screen: Routes.BROWSER.VIEW,
});
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Actions}`));
expect(navigation.navigate).toHaveBeenCalledWith(
Routes.MODAL.ROOT_MODAL_FLOW,
{
screen: Routes.MODAL.WALLET_ACTIONS,
},
);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Activity}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.TRANSACTIONS_VIEW);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Setting}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.SETTINGS_VIEW, {
screen: Routes.ACCOUNTS_MENU_VIEW,
});
});
it('navigates to rewards when rewards tab is pressed', () => {
const rewardsState = {
index: 0,
routes: [{ key: '1', name: 'Tab 1' }],
};
const rewardsDescriptors: TestDescriptors = {
'1': {
options: {
tabBarIconKey: TabBarIconKey.Rewards,
rootScreenName: Routes.REWARDS_VIEW,
callback: () => ({}),
},
},
};
const { getByTestId } = renderWithProvider(
<TabBar
state={rewardsState as TabNavigationState<ParamListBase>}
descriptors={
rewardsDescriptors as Record<string, ExtendedBottomTabDescriptor>
}
navigation={navigation}
/>,
{ state: mockInitialState },
);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Rewards}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.REWARDS_VIEW);
});
it('navigates to trending when trending tab is pressed', () => {
jest.mocked(selectAssetsTrendingTokensEnabled).mockReturnValue(true);
const trendingState = {
index: 0,
routes: [{ key: '1', name: 'Tab 1' }],
};
const trendingDescriptors: TestDescriptors = {
'1': {
options: {
tabBarIconKey: TabBarIconKey.Trending,
rootScreenName: Routes.TRENDING_VIEW,
},
},
};
const { getByTestId } = renderWithProvider(
<TabBar
state={trendingState as TabNavigationState<ParamListBase>}
descriptors={
trendingDescriptors as Record<string, ExtendedBottomTabDescriptor>
}
navigation={navigation}
/>,
{ state: mockInitialState },
);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Trending}`));
expect(navigation.navigate).toHaveBeenCalledWith(Routes.TRENDING_VIEW);
});
it('does not navigate to trending when trending feature flag is disabled', () => {
jest.mocked(selectAssetsTrendingTokensEnabled).mockReturnValue(false);
const trendingState = {
index: 0,
routes: [{ key: '1', name: 'Tab 1' }],
};
const trendingDescriptors: TestDescriptors = {
'1': {
options: {
tabBarIconKey: TabBarIconKey.Trending,
rootScreenName: Routes.TRENDING_VIEW,
},
},
};
const { getByTestId } = renderWithProvider(
<TabBar
state={trendingState as TabNavigationState<ParamListBase>}
descriptors={
trendingDescriptors as Record<string, ExtendedBottomTabDescriptor>
}
navigation={navigation}
/>,
{ state: mockInitialState },
);
fireEvent.press(getByTestId(`tab-bar-item-${TabBarIconKey.Trending}`));
expect(navigation.navigate).not.toHaveBeenCalledWith(Routes.TRENDING_VIEW);
});
});