|
| 1 | +import React from 'react'; |
| 2 | +import { Image, ActivityIndicator } from 'react-native'; |
| 3 | +import { fireEvent, waitFor } from '@testing-library/react-native'; |
| 4 | +import RestoreWallet from './RestoreWallet'; |
| 5 | +import Routes from '../../../constants/navigation/Routes'; |
| 6 | +import renderWithProvider from '../../../util/test/renderWithProvider'; |
| 7 | +import { MetaMetricsEvents } from '../../../core/Analytics'; |
| 8 | +import EngineService from '../../../core/EngineService'; |
| 9 | +import { strings } from '../../../../locales/i18n'; |
| 10 | + |
| 11 | +const mockReplace = jest.fn(); |
| 12 | +const mockDispatch = jest.fn((action) => { |
| 13 | + if (action.type === 'REPLACE') { |
| 14 | + mockReplace(action.payload.name, action.payload.params); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +jest.mock('@react-navigation/native', () => { |
| 19 | + const actualNav = jest.requireActual('@react-navigation/native'); |
| 20 | + return { |
| 21 | + ...actualNav, |
| 22 | + useNavigation: () => ({ |
| 23 | + dispatch: mockDispatch, |
| 24 | + }), |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +jest.mock('../../../util/navigation/navUtils', () => ({ |
| 29 | + createNavigationDetails: jest.fn( |
| 30 | + (routeName: string, nestedRouteName?: string) => |
| 31 | + (params?: Record<string, unknown>) => [ |
| 32 | + nestedRouteName ?? routeName, |
| 33 | + params, |
| 34 | + ], |
| 35 | + ), |
| 36 | + useParams: jest.fn(() => ({ |
| 37 | + previousScreen: 'Login', |
| 38 | + })), |
| 39 | +})); |
| 40 | + |
| 41 | +const mockTrackEvent = jest.fn(); |
| 42 | +const mockCreateEventBuilder = jest.fn(() => ({ |
| 43 | + addProperties: jest.fn().mockReturnThis(), |
| 44 | + build: jest.fn().mockReturnValue({ category: 'test' }), |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock('../../../components/hooks/useMetrics', () => ({ |
| 48 | + useMetrics: () => ({ |
| 49 | + trackEvent: mockTrackEvent, |
| 50 | + createEventBuilder: mockCreateEventBuilder, |
| 51 | + }), |
| 52 | +})); |
| 53 | + |
| 54 | +jest.mock('../../../util/metrics', () => jest.fn(() => ({ device: 'test' }))); |
| 55 | + |
| 56 | +jest.mock('../../../core/EngineService', () => ({ |
| 57 | + initializeVaultFromBackup: jest.fn(), |
| 58 | +})); |
| 59 | + |
| 60 | +describe('RestoreWallet', () => { |
| 61 | + beforeEach(() => { |
| 62 | + jest.clearAllMocks(); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('rendering', () => { |
| 66 | + it('renders title text', () => { |
| 67 | + const { getByText } = renderWithProvider(<RestoreWallet />); |
| 68 | + |
| 69 | + expect( |
| 70 | + getByText(strings('restore_wallet.restore_needed_title')), |
| 71 | + ).toBeOnTheScreen(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('renders description text', () => { |
| 75 | + const { getByText } = renderWithProvider(<RestoreWallet />); |
| 76 | + |
| 77 | + expect( |
| 78 | + getByText(strings('restore_wallet.restore_needed_description')), |
| 79 | + ).toBeOnTheScreen(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renders Restore button', () => { |
| 83 | + const { getByText } = renderWithProvider(<RestoreWallet />); |
| 84 | + |
| 85 | + expect( |
| 86 | + getByText(strings('restore_wallet.restore_needed_action')), |
| 87 | + ).toBeOnTheScreen(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('renders device image', () => { |
| 91 | + const { UNSAFE_getByType } = renderWithProvider(<RestoreWallet />); |
| 92 | + |
| 93 | + const imageElement = UNSAFE_getByType(Image); |
| 94 | + expect(imageElement).toBeTruthy(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('analytics tracking', () => { |
| 99 | + it('tracks screen viewed event on mount', () => { |
| 100 | + renderWithProvider(<RestoreWallet />); |
| 101 | + |
| 102 | + expect(mockCreateEventBuilder).toHaveBeenCalledWith( |
| 103 | + MetaMetricsEvents.VAULT_CORRUPTION_RESTORE_WALLET_SCREEN_VIEWED, |
| 104 | + ); |
| 105 | + expect(mockTrackEvent).toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('handleOnNext', () => { |
| 110 | + it('navigates to WalletRestored when restore succeeds', async () => { |
| 111 | + (EngineService.initializeVaultFromBackup as jest.Mock).mockResolvedValue({ |
| 112 | + success: true, |
| 113 | + }); |
| 114 | + const { getByText } = renderWithProvider(<RestoreWallet />); |
| 115 | + |
| 116 | + fireEvent.press( |
| 117 | + getByText(strings('restore_wallet.restore_needed_action')), |
| 118 | + ); |
| 119 | + |
| 120 | + await waitFor(() => { |
| 121 | + expect(mockCreateEventBuilder).toHaveBeenCalledWith( |
| 122 | + MetaMetricsEvents.VAULT_CORRUPTION_RESTORE_WALLET_BUTTON_PRESSED, |
| 123 | + ); |
| 124 | + expect(EngineService.initializeVaultFromBackup).toHaveBeenCalled(); |
| 125 | + expect(mockDispatch).toHaveBeenCalled(); |
| 126 | + expect(mockReplace).toHaveBeenCalledWith( |
| 127 | + Routes.VAULT_RECOVERY.WALLET_RESTORED, |
| 128 | + undefined, |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('navigates to WalletResetNeeded when restore fails', async () => { |
| 134 | + (EngineService.initializeVaultFromBackup as jest.Mock).mockResolvedValue({ |
| 135 | + success: false, |
| 136 | + }); |
| 137 | + const { getByText } = renderWithProvider(<RestoreWallet />); |
| 138 | + |
| 139 | + fireEvent.press( |
| 140 | + getByText(strings('restore_wallet.restore_needed_action')), |
| 141 | + ); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + expect(EngineService.initializeVaultFromBackup).toHaveBeenCalled(); |
| 145 | + expect(mockDispatch).toHaveBeenCalled(); |
| 146 | + expect(mockReplace).toHaveBeenCalledWith( |
| 147 | + Routes.VAULT_RECOVERY.WALLET_RESET_NEEDED, |
| 148 | + undefined, |
| 149 | + ); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it('shows loading indicator while restoring', async () => { |
| 154 | + let resolveRestore: (value: { success: boolean }) => void; |
| 155 | + const restorePromise = new Promise<{ success: boolean }>((resolve) => { |
| 156 | + resolveRestore = resolve; |
| 157 | + }); |
| 158 | + (EngineService.initializeVaultFromBackup as jest.Mock).mockReturnValue( |
| 159 | + restorePromise, |
| 160 | + ); |
| 161 | + const { getByText, UNSAFE_getByType } = renderWithProvider( |
| 162 | + <RestoreWallet />, |
| 163 | + ); |
| 164 | + |
| 165 | + fireEvent.press( |
| 166 | + getByText(strings('restore_wallet.restore_needed_action')), |
| 167 | + ); |
| 168 | + |
| 169 | + expect(UNSAFE_getByType(ActivityIndicator)).toBeTruthy(); |
| 170 | + |
| 171 | + // @ts-expect-error resolveRestore is assigned in Promise constructor |
| 172 | + resolveRestore({ success: true }); |
| 173 | + |
| 174 | + await waitFor(() => { |
| 175 | + expect(mockDispatch).toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments