|
| 1 | +import React from 'react'; |
| 2 | +import { useSelector } from 'react-redux'; |
| 3 | +import { render, fireEvent, waitFor } from '@testing-library/react-native'; |
| 4 | +import { SafeAreaProvider, Metrics } from 'react-native-safe-area-context'; |
| 5 | +import { KeyringClient } from '@metamask/keyring-snap-client'; |
| 6 | +import SolanaNewFeatureContent from './SolanaNewFeatureContent'; |
| 7 | +import StorageWrapper from '../../../store/storage-wrapper'; |
| 8 | + |
| 9 | +const mockUseTheme = jest.fn(); |
| 10 | +jest.mock('../../../util/theme', () => ({ |
| 11 | + useTheme: () => mockUseTheme(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('../../../../locales/i18n', () => ({ |
| 15 | + strings: (key: string) => key, |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('react-redux', () => ({ |
| 19 | + useSelector: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../../../store/storage-wrapper', () => ({ |
| 23 | + getItem: jest.fn(), |
| 24 | + setItem: jest.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('@metamask/keyring-snap-client', () => ({ |
| 28 | + KeyringClient: jest.fn().mockImplementation(() => ({ |
| 29 | + createAccount: jest.fn(), |
| 30 | + })), |
| 31 | +})); |
| 32 | + |
| 33 | +jest.mock('../../../core/SnapKeyring/SolanaWalletSnap', () => ({ |
| 34 | + SolanaWalletSnapSender: jest.fn(), |
| 35 | +})); |
| 36 | + |
| 37 | +jest.mock('@react-navigation/native', () => ({ |
| 38 | + useNavigation: () => ({ |
| 39 | + navigate: jest.fn(), |
| 40 | + goBack: jest.fn(), |
| 41 | + }), |
| 42 | +})); |
| 43 | + |
| 44 | +const initialMetrics: Metrics = { |
| 45 | + frame: { x: 0, y: 0, width: 320, height: 640 }, |
| 46 | + insets: { top: 0, left: 0, right: 0, bottom: 0 }, |
| 47 | +}; |
| 48 | + |
| 49 | +const renderWithProviders = (component: React.ReactElement) => |
| 50 | + render( |
| 51 | + <SafeAreaProvider initialMetrics={initialMetrics}> |
| 52 | + {component} |
| 53 | + </SafeAreaProvider>, |
| 54 | + ); |
| 55 | + |
| 56 | +describe('SolanaNewFeatureContent', () => { |
| 57 | + beforeEach(() => { |
| 58 | + jest.clearAllMocks(); |
| 59 | + (useSelector as jest.Mock).mockReturnValue(false); |
| 60 | + (StorageWrapper.getItem as jest.Mock).mockResolvedValue('false'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders correctly', async () => { |
| 64 | + const { getByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 65 | + |
| 66 | + await waitFor(() => { |
| 67 | + expect(getByText('solana_new_feature_content.title')).toBeTruthy(); |
| 68 | + expect( |
| 69 | + getByText('solana_new_feature_content.feature_1_title'), |
| 70 | + ).toBeTruthy(); |
| 71 | + expect( |
| 72 | + getByText('solana_new_feature_content.feature_2_title'), |
| 73 | + ).toBeTruthy(); |
| 74 | + expect( |
| 75 | + getByText('solana_new_feature_content.feature_3_title'), |
| 76 | + ).toBeTruthy(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('calls setItem when the "close" button is pressed', async () => { |
| 81 | + const { getByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + const closeButton = getByText('solana_new_feature_content.not_now'); |
| 85 | + fireEvent.press(closeButton); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(StorageWrapper.setItem).toHaveBeenCalledWith( |
| 89 | + '@MetaMask:solanaFeatureModalShown', |
| 90 | + 'true', |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('shows the "create account" button for new users', async () => { |
| 95 | + const { getByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 96 | + |
| 97 | + await waitFor(() => { |
| 98 | + expect( |
| 99 | + getByText('solana_new_feature_content.create_solana_account'), |
| 100 | + ).toBeTruthy(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('shows the "got it" button for existing users', async () => { |
| 105 | + (useSelector as jest.Mock).mockReturnValue(true); |
| 106 | + const { getByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 107 | + |
| 108 | + await waitFor(() => { |
| 109 | + expect(getByText('solana_new_feature_content.got_it')).toBeTruthy(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('creates an account when "create account" button is pressed', async () => { |
| 114 | + const mockCreateAccount = jest.fn(); |
| 115 | + (KeyringClient as jest.Mock).mockImplementation(() => ({ |
| 116 | + createAccount: mockCreateAccount, |
| 117 | + })); |
| 118 | + |
| 119 | + const { getByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + const createButton = getByText( |
| 123 | + 'solana_new_feature_content.create_solana_account', |
| 124 | + ); |
| 125 | + fireEvent.press(createButton); |
| 126 | + }); |
| 127 | + |
| 128 | + expect(mockCreateAccount).toHaveBeenCalledWith({ |
| 129 | + scope: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', |
| 130 | + }); |
| 131 | + expect(StorageWrapper.setItem).toHaveBeenCalledWith( |
| 132 | + '@MetaMask:solanaFeatureModalShown', |
| 133 | + 'true', |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does not render when modal has been shown before', async () => { |
| 138 | + (StorageWrapper.getItem as jest.Mock).mockResolvedValue('true'); |
| 139 | + const { queryByText } = renderWithProviders(<SolanaNewFeatureContent />); |
| 140 | + |
| 141 | + await waitFor(() => { |
| 142 | + expect(queryByText('solana_new_feature_content.title')).toBeNull(); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments