|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 3 | +import { Linking } from 'react-native'; |
| 4 | +import Clipboard from '@react-native-clipboard/clipboard'; |
| 5 | +import OndoCampaignWinningView, { |
| 6 | + ONDO_CAMPAIGN_WINNING_VIEW_TEST_IDS, |
| 7 | +} from './OndoCampaignWinningView'; |
| 8 | +import { useSelector } from 'react-redux'; |
| 9 | +import { useGetOndoLeaderboardPosition } from '../hooks/useGetOndoLeaderboardPosition'; |
| 10 | + |
| 11 | +jest.mock('../../../../images/rewards/campaign_winning.png', () => ({ |
| 12 | + __esModule: true, |
| 13 | + default: 1, |
| 14 | +})); |
| 15 | + |
| 16 | +const mockGoBack = jest.fn(); |
| 17 | + |
| 18 | +jest.mock('@react-navigation/native', () => ({ |
| 19 | + useNavigation: () => ({ goBack: mockGoBack }), |
| 20 | + useRoute: () => ({ |
| 21 | + params: { campaignId: 'campaign-ondo-1', campaignName: 'Ondo Campaign' }, |
| 22 | + }), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('@metamask/design-system-twrnc-preset', () => ({ |
| 26 | + useTailwind: () => ({ style: (...args: unknown[]) => args }), |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('react-native-safe-area-context', () => { |
| 30 | + const actual = jest.requireActual('react-native-safe-area-context'); |
| 31 | + return { |
| 32 | + ...actual, |
| 33 | + useSafeAreaInsets: () => ({ top: 44, bottom: 34, left: 0, right: 0 }), |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +jest.mock('react-redux', () => ({ |
| 38 | + useSelector: jest.fn(), |
| 39 | + useDispatch: jest.fn(() => jest.fn()), |
| 40 | +})); |
| 41 | + |
| 42 | +const mockUseSelector = useSelector as jest.MockedFunction<typeof useSelector>; |
| 43 | + |
| 44 | +jest.mock('../../../Views/ErrorBoundary', () => { |
| 45 | + const ReactActual = jest.requireActual('react'); |
| 46 | + const { View } = jest.requireActual('react-native'); |
| 47 | + return { |
| 48 | + __esModule: true, |
| 49 | + default: ({ children }: { children: React.ReactNode }) => |
| 50 | + ReactActual.createElement(View, null, children), |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +jest.mock('../hooks/useTrackRewardsPageView', () => ({ |
| 55 | + __esModule: true, |
| 56 | + default: jest.fn(), |
| 57 | +})); |
| 58 | + |
| 59 | +const mockTrackEvent = jest.fn(); |
| 60 | +const mockBuild = jest.fn(() => ({})); |
| 61 | +jest.mock('../../../hooks/useAnalytics/useAnalytics', () => ({ |
| 62 | + useAnalytics: () => ({ |
| 63 | + trackEvent: mockTrackEvent, |
| 64 | + createEventBuilder: () => ({ |
| 65 | + addProperties: () => ({ build: mockBuild }), |
| 66 | + }), |
| 67 | + }), |
| 68 | +})); |
| 69 | + |
| 70 | +const mockPosition = { |
| 71 | + projectedTier: 'MID', |
| 72 | + rank: 3, |
| 73 | + totalInTier: 100, |
| 74 | + rateOfReturn: 0.2823, |
| 75 | + currentUsdValue: 2000, |
| 76 | + totalUsdDeposited: 1000, |
| 77 | + netDeposit: 900, |
| 78 | + qualifiedDays: 10, |
| 79 | + qualified: true, |
| 80 | + neighbors: [], |
| 81 | + computedAt: '2024-01-01T00:00:00.000Z', |
| 82 | +}; |
| 83 | + |
| 84 | +jest.mock('../hooks/useGetOndoLeaderboardPosition', () => ({ |
| 85 | + useGetOndoLeaderboardPosition: jest.fn(), |
| 86 | +})); |
| 87 | + |
| 88 | +const mockUseGetOndoLeaderboardPosition = |
| 89 | + useGetOndoLeaderboardPosition as jest.MockedFunction< |
| 90 | + typeof useGetOndoLeaderboardPosition |
| 91 | + >; |
| 92 | + |
| 93 | +jest.mock('../components/ReferralDetails/CopyableField', () => { |
| 94 | + const ReactActual = jest.requireActual('react'); |
| 95 | + const { View, Text, Pressable } = jest.requireActual('react-native'); |
| 96 | + return { |
| 97 | + __esModule: true, |
| 98 | + default: ({ |
| 99 | + label, |
| 100 | + value, |
| 101 | + onCopy, |
| 102 | + }: { |
| 103 | + label: string; |
| 104 | + value?: string | null; |
| 105 | + onCopy?: () => void; |
| 106 | + }) => |
| 107 | + ReactActual.createElement( |
| 108 | + View, |
| 109 | + { testID: 'copyable-field' }, |
| 110 | + ReactActual.createElement(Text, null, label), |
| 111 | + ReactActual.createElement( |
| 112 | + Text, |
| 113 | + { testID: 'copyable-value' }, |
| 114 | + value ?? '', |
| 115 | + ), |
| 116 | + ReactActual.createElement(Pressable, { |
| 117 | + testID: 'copyable-trigger', |
| 118 | + onPress: onCopy, |
| 119 | + }), |
| 120 | + ), |
| 121 | + }; |
| 122 | +}); |
| 123 | + |
| 124 | +jest.mock('../../../../../locales/i18n', () => ({ |
| 125 | + strings: jest.fn( |
| 126 | + (key: string, params?: { place?: string; code?: string }) => { |
| 127 | + const map: Record<string, string> = { |
| 128 | + 'rewards.ondo_campaign_winning.you_won': 'You won', |
| 129 | + 'rewards.ondo_campaign_winning.email_instructions': |
| 130 | + 'Email ondocampaign@consensys.net with your code to claim your prize.', |
| 131 | + 'rewards.ondo_campaign_winning.open_mail': 'Open mail', |
| 132 | + 'rewards.ondo_campaign_winning.skip_for_now': 'Skip for now', |
| 133 | + 'rewards.ondo_campaign_winning.mail_subject': |
| 134 | + 'Ondo campaign prize claim', |
| 135 | + 'rewards.ondo_campaign_winning.mail_body': `My referral code: ${params?.code ?? ''}`, |
| 136 | + 'rewards.ondo_campaign_winning.close_a11y': 'Close', |
| 137 | + 'rewards.referral.referral_code': 'Referral code', |
| 138 | + }; |
| 139 | + if (key === 'rewards.ondo_campaign_winning.rank_label' && params?.place) { |
| 140 | + return `${params.place} place`; |
| 141 | + } |
| 142 | + return map[key] ?? key; |
| 143 | + }, |
| 144 | + ), |
| 145 | +})); |
| 146 | + |
| 147 | +describe('OndoCampaignWinningView', () => { |
| 148 | + beforeEach(() => { |
| 149 | + jest.clearAllMocks(); |
| 150 | + mockUseGetOndoLeaderboardPosition.mockReturnValue({ |
| 151 | + position: mockPosition, |
| 152 | + isLoading: false, |
| 153 | + hasError: false, |
| 154 | + hasFetched: true, |
| 155 | + refetch: jest.fn(), |
| 156 | + }); |
| 157 | + mockUseSelector.mockImplementation((selector) => |
| 158 | + selector({ |
| 159 | + rewards: { |
| 160 | + referralCode: 'LVL346', |
| 161 | + referralDetailsLoading: false, |
| 162 | + }, |
| 163 | + } as never), |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it('renders the main container', () => { |
| 168 | + const { getByTestId } = render(<OndoCampaignWinningView />); |
| 169 | + expect( |
| 170 | + getByTestId(ONDO_CAMPAIGN_WINNING_VIEW_TEST_IDS.CONTAINER), |
| 171 | + ).toBeTruthy(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('shows you won, prize, rank place, and rate from leaderboard position', () => { |
| 175 | + const { getByText } = render(<OndoCampaignWinningView />); |
| 176 | + expect(getByText('You won')).toBeTruthy(); |
| 177 | + expect(getByText('3rd place')).toBeTruthy(); |
| 178 | + expect(getByText('+28.23%')).toBeTruthy(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('calls goBack when Skip for now is pressed', () => { |
| 182 | + const { getByText } = render(<OndoCampaignWinningView />); |
| 183 | + fireEvent.press(getByText('Skip for now')); |
| 184 | + expect(mockGoBack).toHaveBeenCalledTimes(1); |
| 185 | + }); |
| 186 | + |
| 187 | + it('calls goBack when close is pressed', () => { |
| 188 | + const { getByLabelText } = render(<OndoCampaignWinningView />); |
| 189 | + fireEvent.press(getByLabelText('Close')); |
| 190 | + expect(mockGoBack).toHaveBeenCalledTimes(1); |
| 191 | + }); |
| 192 | + |
| 193 | + it('copies referral code and tracks analytics when copy is triggered', () => { |
| 194 | + const setStringSpy = jest.spyOn(Clipboard, 'setString'); |
| 195 | + const { getByTestId } = render(<OndoCampaignWinningView />); |
| 196 | + fireEvent.press(getByTestId('copyable-trigger')); |
| 197 | + expect(setStringSpy).toHaveBeenCalledWith('LVL346'); |
| 198 | + expect(mockTrackEvent).toHaveBeenCalled(); |
| 199 | + }); |
| 200 | + |
| 201 | + it('opens mailto when Open mail is pressed', async () => { |
| 202 | + const openSpy = jest.spyOn(Linking, 'openURL').mockResolvedValue(undefined); |
| 203 | + const { getByText } = render(<OndoCampaignWinningView />); |
| 204 | + fireEvent.press(getByText('Open mail')); |
| 205 | + expect(openSpy).toHaveBeenCalled(); |
| 206 | + const url = openSpy.mock.calls[0][0] as string; |
| 207 | + expect(url).toContain('mailto:ondocampaign@consensys.net'); |
| 208 | + expect(url).toContain(encodeURIComponent('LVL346')); |
| 209 | + openSpy.mockRestore(); |
| 210 | + }); |
| 211 | +}); |
0 commit comments