-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathCampaignWinningView.test.tsx
More file actions
297 lines (266 loc) · 8.62 KB
/
CampaignWinningView.test.tsx
File metadata and controls
297 lines (266 loc) · 8.62 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { Linking } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import CampaignWinningView, {
CampaignWinningViewProps,
} from './CampaignWinningView';
import useTrackRewardsPageView from '../hooks/useTrackRewardsPageView';
jest.mock('../../../../images/rewards/campaign_winning.png', () => ({
__esModule: true,
default: 1,
}));
const mockGoBack = jest.fn();
const mockNavigate = jest.fn();
jest.mock('@react-navigation/native', () => ({
useNavigation: () => ({ goBack: mockGoBack, navigate: mockNavigate }),
}));
jest.mock('@metamask/design-system-twrnc-preset', () => {
const tw = (...args: unknown[]) => args;
tw.style = (...args: unknown[]) => args;
return { useTailwind: () => tw };
});
jest.mock('react-native/Libraries/Utilities/useWindowDimensions', () => ({
default: () => ({ width: 390, height: 844 }),
}));
jest.mock('react-native-safe-area-context', () => {
const actual = jest.requireActual('react-native-safe-area-context');
return {
...actual,
useSafeAreaInsets: () => ({ top: 44, bottom: 34, left: 0, right: 0 }),
};
});
jest.mock('../../../Views/ErrorBoundary', () => {
const ReactActual = jest.requireActual('react');
const { View } = jest.requireActual('react-native');
return {
__esModule: true,
default: ({ children }: { children: React.ReactNode }) =>
ReactActual.createElement(View, null, children),
};
});
jest.mock('../hooks/useTrackRewardsPageView', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock('../../../../core/Analytics', () => ({
MetaMetricsEvents: {
REWARDS_PAGE_BUTTON_CLICKED: 'REWARDS_PAGE_BUTTON_CLICKED',
},
}));
jest.mock('../utils', () => ({
RewardsMetricsButtons: {
COPY_WINNER_VERIFICATION_CODE: 'copy_winner_verification_code',
},
}));
const mockTrackEvent = jest.fn();
const mockBuild = jest.fn(() => ({}));
jest.mock('../../../hooks/useAnalytics/useAnalytics', () => ({
useAnalytics: () => ({
trackEvent: mockTrackEvent,
createEventBuilder: () => ({
addProperties: () => ({ build: mockBuild }),
}),
}),
}));
jest.mock('../components/ReferralDetails/CopyableField', () => {
const ReactActual = jest.requireActual('react');
const { View, Text, Pressable } = jest.requireActual('react-native');
return {
__esModule: true,
default: ({
label,
value,
onCopy,
}: {
label: string;
value?: string | null;
onCopy?: () => void;
}) =>
ReactActual.createElement(
View,
{ testID: 'copyable-field' },
ReactActual.createElement(Text, null, label),
ReactActual.createElement(
Text,
{ testID: 'copyable-value' },
value ?? '',
),
ReactActual.createElement(Pressable, {
testID: 'copyable-trigger',
onPress: onCopy,
}),
),
};
});
jest.mock('../../../../../locales/i18n', () => ({
strings: jest.fn(
(
key: string,
params?: {
code?: string;
campaignName?: string;
email?: string;
},
) => {
if (
key === 'rewards.campaign_winning.mail_subject' &&
params?.campaignName
)
return `${params.campaignName} prize claim`;
if (key === 'rewards.campaign_winning.mail_body' && params?.code)
return `My winning code: ${params.code}`;
if (
key === 'rewards.campaign_winning.email_instructions' &&
params?.email
)
return `Email ${params.email} with your code`;
const map: Record<string, string> = {
'rewards.campaign_winning.you_won': 'You won',
'rewards.campaign_winning.open_mail': 'Open mail',
'rewards.campaign_winning.skip_for_now': 'Skip for now',
'rewards.campaign_winning.winning_code': 'Winning code',
'rewards.campaign_winning.close_a11y': 'Close',
};
return map[key] ?? key;
},
),
}));
const PRIZE_EMAIL = 'test@consensys.net';
const CAMPAIGN_NAME = 'Test Campaign';
const CAMPAIGN_ID = 'campaign-test-1';
const WINNING_CODE = 'WIN-123';
const mockUseTrackRewardsPageView =
useTrackRewardsPageView as jest.MockedFunction<
typeof useTrackRewardsPageView
>;
const defaultProps: CampaignWinningViewProps = {
testID: 'test-winning-view',
viewName: 'TestWinningView',
prizeEmail: PRIZE_EMAIL,
campaignName: CAMPAIGN_NAME,
campaignId: CAMPAIGN_ID,
analyticsPageType: 'test_campaign_winning',
winningCode: WINNING_CODE,
hasOutcomeLoaded: true,
isLoading: false,
rankDisplay: null,
};
describe('CampaignWinningView', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders the main container with the provided testID', () => {
const { getByTestId } = render(<CampaignWinningView {...defaultProps} />);
expect(getByTestId('test-winning-view')).toBeTruthy();
});
it('renders "You won" text', () => {
const { getByText } = render(<CampaignWinningView {...defaultProps} />);
expect(getByText('You won')).toBeTruthy();
});
it('renders email instructions with the prizeEmail', () => {
const { getByText } = render(<CampaignWinningView {...defaultProps} />);
expect(getByText(`Email ${PRIZE_EMAIL} with your code`)).toBeTruthy();
});
it('tracks page view with the campaign id', () => {
render(<CampaignWinningView {...defaultProps} />);
expect(mockUseTrackRewardsPageView).toHaveBeenCalledWith({
page_type: 'test_campaign_winning',
campaign_id: CAMPAIGN_ID,
});
});
it('renders rank and result display when provided', () => {
const { getByText } = render(
<CampaignWinningView
{...defaultProps}
rankDisplay="3rd"
resultDisplay="+12.34%"
/>,
);
expect(getByText('3rd')).toBeTruthy();
expect(getByText('+12.34%')).toBeTruthy();
});
it('calls goBack when Skip for now is pressed', () => {
const { getByText } = render(<CampaignWinningView {...defaultProps} />);
fireEvent.press(getByText('Skip for now'));
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
it('calls goBack when Close button is pressed', () => {
const { getByLabelText } = render(
<CampaignWinningView {...defaultProps} />,
);
fireEvent.press(getByLabelText('Close'));
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
it('copies winning code and fires analytics when copy is triggered', () => {
const setStringSpy = jest.spyOn(Clipboard, 'setString');
const { getByTestId } = render(<CampaignWinningView {...defaultProps} />);
fireEvent.press(getByTestId('copyable-trigger'));
expect(setStringSpy).toHaveBeenCalledWith(WINNING_CODE);
expect(mockTrackEvent).toHaveBeenCalled();
setStringSpy.mockRestore();
});
it('opens mailto with the correct email and code when Open mail is pressed', async () => {
const openSpy = jest.spyOn(Linking, 'openURL').mockResolvedValue(undefined);
const { getByText } = render(<CampaignWinningView {...defaultProps} />);
fireEvent.press(getByText('Open mail'));
expect(openSpy).toHaveBeenCalled();
const url = openSpy.mock.calls[0][0] as string;
expect(url).toContain(`mailto:${PRIZE_EMAIL}`);
expect(url).toContain(encodeURIComponent(WINNING_CODE));
openSpy.mockRestore();
});
it('navigates to fallback route when outcome loads without a winning code', () => {
const fallbackRoute = {
route: 'CampaignDetails',
params: { campaignId: CAMPAIGN_ID },
};
render(
<CampaignWinningView
{...defaultProps}
winningCode={null}
hasOutcomeLoaded
isLoading={false}
fallbackRoute={fallbackRoute}
/>,
);
expect(mockNavigate).toHaveBeenCalledWith(
fallbackRoute.route,
fallbackRoute.params,
);
expect(mockGoBack).not.toHaveBeenCalled();
});
it('falls back to goBack when outcome loads without a winning code and no fallback route is provided', () => {
render(
<CampaignWinningView
{...defaultProps}
winningCode={null}
hasOutcomeLoaded
isLoading={false}
/>,
);
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
it('does not call goBack before outcome has loaded', () => {
render(
<CampaignWinningView
{...defaultProps}
winningCode={null}
hasOutcomeLoaded={false}
isLoading={false}
/>,
);
expect(mockGoBack).not.toHaveBeenCalled();
});
it('does not call goBack while still loading', () => {
render(
<CampaignWinningView
{...defaultProps}
winningCode={null}
hasOutcomeLoaded
isLoading
/>,
);
expect(mockGoBack).not.toHaveBeenCalled();
});
});