Skip to content

Commit 11e81ff

Browse files
committed
fix: add specific error message for duplicate SRP imports
1 parent a37efd8 commit 11e81ff

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

app/_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/pages/multi-srp/import-srp/import-srp.test.tsx

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from 'react';
22
import { fireEvent, waitFor } from '@testing-library/react';
3+
import { userEvent } from '@testing-library/user-event';
34
import configureMockStore from 'redux-mock-store';
45
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';
78
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';
1010
import { setShowNewSrpAddedToast } from '../../../components/app/toast-master/utils';
11+
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
12+
import { importMnemonicToVault } from '../../../store/actions';
1113
import { ImportSrp } from './import-srp';
1214

1315
jest.mock('../../../store/actions', () => ({
@@ -95,4 +97,65 @@ describe('ImportSrp', () => {
9597
expect(setShowNewSrpAddedToast).toHaveBeenCalledWith(true);
9698
});
9799
});
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+
});
98161
});

ui/pages/multi-srp/import-srp/import-srp.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ export const ImportSrp = () => {
6868
navigate(DEFAULT_ROUTE);
6969
dispatch(setShowNewSrpAddedToast(true));
7070
} catch (error) {
71-
setSrpError(t('srpAlreadyImportedError'));
71+
switch ((error as Error)?.message) {
72+
case 'KeyringController - The account you are trying to import is a duplicate':
73+
setSrpError(t('srpImportDuplicateAccountError'));
74+
break;
75+
default:
76+
setSrpError(t('srpAlreadyImportedError'));
77+
break;
78+
}
7279
}
7380
}
7481

0 commit comments

Comments
 (0)