|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent, waitFor, act } from '@testing-library/react-native'; |
| 3 | +import { useSelector } from 'react-redux'; |
| 4 | +import CampaignReminder from './CampaignReminder'; |
| 5 | +import { reminderStorageKeyForComposite } from '../../hooks/useCampaignReminderSubscriptions'; |
| 6 | +import { |
| 7 | + type CampaignDto, |
| 8 | + CampaignType, |
| 9 | +} from '../../../../../core/Engine/controllers/rewards-controller/types'; |
| 10 | +import { MetaMetricsEvents } from '../../../../../core/Analytics'; |
| 11 | +import { selectRewardsSubscriptionId } from '../../../../../selectors/rewards'; |
| 12 | + |
| 13 | +const mockTrackEvent = jest.fn(); |
| 14 | +const mockCreateEventBuilder = jest.fn(); |
| 15 | +const mockShowToast = jest.fn(); |
| 16 | + |
| 17 | +const TEST_REWARDS_SUBSCRIPTION_ID = 'test-rewards-sub-id'; |
| 18 | + |
| 19 | +const mockGetItemSync = jest.fn((_key: string): string | null => null); |
| 20 | +const mockSetItem = jest.fn( |
| 21 | + (_key: string, _value: string): Promise<void> => Promise.resolve(), |
| 22 | +); |
| 23 | + |
| 24 | +jest.mock('react-redux', () => ({ |
| 25 | + ...jest.requireActual('react-redux'), |
| 26 | + useSelector: jest.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +const mockUseSelector = useSelector as jest.MockedFunction<typeof useSelector>; |
| 30 | + |
| 31 | +jest.mock('../../../../../store/storage-wrapper', () => ({ |
| 32 | + __esModule: true, |
| 33 | + default: { |
| 34 | + getItemSync: (key: string) => mockGetItemSync(key), |
| 35 | + setItem: (key: string, value: string) => mockSetItem(key, value), |
| 36 | + }, |
| 37 | +})); |
| 38 | + |
| 39 | +jest.mock('../../../../hooks/useAnalytics/useAnalytics', () => ({ |
| 40 | + useAnalytics: jest.fn(() => ({ |
| 41 | + trackEvent: mockTrackEvent, |
| 42 | + createEventBuilder: mockCreateEventBuilder, |
| 43 | + })), |
| 44 | +})); |
| 45 | + |
| 46 | +jest.mock('../../hooks/useRewardsToast', () => ({ |
| 47 | + __esModule: true, |
| 48 | + default: jest.fn(() => ({ |
| 49 | + showToast: mockShowToast, |
| 50 | + RewardsToastOptions: { |
| 51 | + success: jest.fn((title: string, subtitle?: string) => ({ |
| 52 | + variant: 'success', |
| 53 | + title, |
| 54 | + subtitle, |
| 55 | + })), |
| 56 | + error: jest.fn((title: string, subtitle?: string) => ({ |
| 57 | + variant: 'error', |
| 58 | + title, |
| 59 | + subtitle, |
| 60 | + })), |
| 61 | + entriesClosed: jest.fn(), |
| 62 | + }, |
| 63 | + })), |
| 64 | +})); |
| 65 | + |
| 66 | +jest.mock('../../../../../images/rewards/notification.svg', () => { |
| 67 | + const ReactActual = jest.requireActual('react'); |
| 68 | + const { View } = jest.requireActual('react-native'); |
| 69 | + return { |
| 70 | + __esModule: true, |
| 71 | + default: () => |
| 72 | + ReactActual.createElement(View, { testID: 'campaign-reminder-svg' }), |
| 73 | + }; |
| 74 | +}); |
| 75 | + |
| 76 | +jest.mock('@metamask/design-system-react-native', () => { |
| 77 | + const actual = jest.requireActual('@metamask/design-system-react-native'); |
| 78 | + return { ...actual }; |
| 79 | +}); |
| 80 | + |
| 81 | +jest.mock('@metamask/design-system-twrnc-preset', () => ({ |
| 82 | + useTailwind: () => ({ style: (...args: unknown[]) => args }), |
| 83 | +})); |
| 84 | + |
| 85 | +jest.mock('../../../../../../locales/i18n', () => ({ |
| 86 | + strings: (key: string) => { |
| 87 | + const translations: Record<string, string> = { |
| 88 | + 'rewards.campaign.up_next': 'Up next', |
| 89 | + 'rewards.campaign.notify_me': 'Notify me', |
| 90 | + 'rewards.campaign.remind_me_success_toast': 'We will notify you.', |
| 91 | + 'rewards.campaign.remind_me_save_error': 'Save failed.', |
| 92 | + }; |
| 93 | + return translations[key] || key; |
| 94 | + }, |
| 95 | +})); |
| 96 | + |
| 97 | +const createTestCampaign = (overrides = {}): CampaignDto => ({ |
| 98 | + id: 'campaign-reminder-1', |
| 99 | + type: CampaignType.ONDO_HOLDING, |
| 100 | + name: 'Preview Campaign', |
| 101 | + startDate: '2028-01-01T00:00:00.000Z', |
| 102 | + endDate: '2028-12-31T23:59:59.999Z', |
| 103 | + termsAndConditions: null, |
| 104 | + excludedRegions: [], |
| 105 | + details: null, |
| 106 | + featured: true, |
| 107 | + showUpcomingDate: false, |
| 108 | + ...overrides, |
| 109 | +}); |
| 110 | + |
| 111 | +describe('CampaignReminder', () => { |
| 112 | + beforeEach(() => { |
| 113 | + jest.clearAllMocks(); |
| 114 | + mockGetItemSync.mockReturnValue(null); |
| 115 | + mockSetItem.mockResolvedValue(undefined); |
| 116 | + mockUseSelector.mockImplementation((selector) => { |
| 117 | + if (selector === selectRewardsSubscriptionId) { |
| 118 | + return TEST_REWARDS_SUBSCRIPTION_ID; |
| 119 | + } |
| 120 | + return undefined; |
| 121 | + }); |
| 122 | + mockCreateEventBuilder.mockImplementation(() => { |
| 123 | + const builder = { |
| 124 | + addProperties: jest.fn(), |
| 125 | + build: jest.fn(() => ({})), |
| 126 | + }; |
| 127 | + (builder.addProperties as jest.Mock).mockReturnValue(builder); |
| 128 | + return builder; |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('renders up next label and campaign name', async () => { |
| 133 | + const campaign = createTestCampaign({ name: 'My Upcoming Campaign' }); |
| 134 | + const { getByText, getByTestId } = render( |
| 135 | + <CampaignReminder campaign={campaign} />, |
| 136 | + ); |
| 137 | + |
| 138 | + await waitFor(() => { |
| 139 | + expect( |
| 140 | + getByTestId('campaign-reminder-notify-campaign-reminder-1'), |
| 141 | + ).toBeOnTheScreen(); |
| 142 | + }); |
| 143 | + expect(getByText('Up next')).toBeOnTheScreen(); |
| 144 | + expect(getByText('My Upcoming Campaign')).toBeOnTheScreen(); |
| 145 | + expect(getByText('Notify me')).toBeOnTheScreen(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('tracks reminder subscribed when Notify me is pressed', async () => { |
| 149 | + const campaign = createTestCampaign({ id: 'cr-analytics' }); |
| 150 | + const { getByTestId } = render(<CampaignReminder campaign={campaign} />); |
| 151 | + |
| 152 | + await waitFor(() => { |
| 153 | + expect( |
| 154 | + getByTestId('campaign-reminder-notify-cr-analytics'), |
| 155 | + ).toBeOnTheScreen(); |
| 156 | + }); |
| 157 | + |
| 158 | + await act(async () => { |
| 159 | + fireEvent.press(getByTestId('campaign-reminder-notify-cr-analytics')); |
| 160 | + }); |
| 161 | + |
| 162 | + expect(mockSetItem).toHaveBeenCalledWith( |
| 163 | + reminderStorageKeyForComposite( |
| 164 | + `${TEST_REWARDS_SUBSCRIPTION_ID}:cr-analytics`, |
| 165 | + ), |
| 166 | + '1', |
| 167 | + ); |
| 168 | + expect(mockCreateEventBuilder).toHaveBeenCalledWith( |
| 169 | + MetaMetricsEvents.REWARDS_CAMPAIGN_REMINDER_SUBSCRIBED, |
| 170 | + ); |
| 171 | + expect(mockTrackEvent).toHaveBeenCalledTimes(1); |
| 172 | + expect(mockShowToast).toHaveBeenCalledTimes(1); |
| 173 | + }); |
| 174 | +}); |
0 commit comments