|
1 | 1 | import React from 'react'; |
2 | 2 | import { fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import { userEvent } from '@testing-library/user-event'; |
3 | 4 | import configureMockStore from 'redux-mock-store'; |
4 | 5 | import thunk from 'redux-thunk'; |
5 | | -import { userEvent } from '@testing-library/user-event'; |
6 | | -import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate'; |
| 6 | +// eslint-disable-next-line import/no-restricted-paths |
| 7 | +import messages from '../../../../app/_locales/en/messages.json'; |
7 | 8 | import mockState from '../../../../test/data/mock-state.json'; |
8 | | -import { importMnemonicToVault } from '../../../store/actions'; |
9 | | -import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'; |
| 9 | +import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate'; |
10 | 10 | import { setShowNewSrpAddedToast } from '../../../components/app/toast-master/utils'; |
| 11 | +import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'; |
| 12 | +import { importMnemonicToVault } from '../../../store/actions'; |
11 | 13 | import { ImportSrp } from './import-srp'; |
12 | 14 |
|
13 | 15 | jest.mock('../../../store/actions', () => ({ |
@@ -95,4 +97,65 @@ describe('ImportSrp', () => { |
95 | 97 | expect(setShowNewSrpAddedToast).toHaveBeenCalledWith(true); |
96 | 98 | }); |
97 | 99 | }); |
| 100 | + |
| 101 | + describe('error handling', () => { |
| 102 | + const testImportError = async ( |
| 103 | + errorMessage: string, |
| 104 | + expectedErrorMessage: string, |
| 105 | + ) => { |
| 106 | + const mockStore = configureMockStore([thunk])(mockState); |
| 107 | + |
| 108 | + // Mock importMnemonicToVault to reject with the specified error |
| 109 | + (importMnemonicToVault as jest.Mock).mockReturnValueOnce( |
| 110 | + jest.fn().mockRejectedValue(new Error(errorMessage)), |
| 111 | + ); |
| 112 | + |
| 113 | + const { queryByTestId, getByText } = renderWithProvider( |
| 114 | + <ImportSrp />, |
| 115 | + mockStore, |
| 116 | + ); |
| 117 | + |
| 118 | + const srpNote = queryByTestId('srp-input-import__srp-note'); |
| 119 | + expect(srpNote).toBeInTheDocument(); |
| 120 | + |
| 121 | + srpNote?.focus(); |
| 122 | + |
| 123 | + if (srpNote) { |
| 124 | + await userEvent.type(srpNote, TEST_SEED); |
| 125 | + } |
| 126 | + |
| 127 | + const confirmSrpButton = queryByTestId('import-srp-confirm'); |
| 128 | + expect(confirmSrpButton).not.toBeDisabled(); |
| 129 | + |
| 130 | + if (confirmSrpButton) { |
| 131 | + fireEvent.click(confirmSrpButton); |
| 132 | + } |
| 133 | + |
| 134 | + // Wait for error to be displayed |
| 135 | + await waitFor(() => { |
| 136 | + expect(getByText(expectedErrorMessage)).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + |
| 139 | + // Verify the button is now disabled due to error |
| 140 | + expect(confirmSrpButton).toBeDisabled(); |
| 141 | + |
| 142 | + // Verify navigation did not happen |
| 143 | + expect(mockNavigate).not.toHaveBeenCalledWith(DEFAULT_ROUTE); |
| 144 | + expect(setShowNewSrpAddedToast).not.toHaveBeenCalled(); |
| 145 | + }; |
| 146 | + |
| 147 | + it('should display duplicate account error when trying to import a duplicate account', async () => { |
| 148 | + await testImportError( |
| 149 | + 'KeyringController - The account you are trying to import is a duplicate', |
| 150 | + messages.srpImportDuplicateAccountError.message, |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should display already imported error for any other import error', async () => { |
| 155 | + await testImportError( |
| 156 | + 'Some other error', |
| 157 | + messages.srpAlreadyImportedError.message, |
| 158 | + ); |
| 159 | + }); |
| 160 | + }); |
98 | 161 | }); |
0 commit comments