Skip to content

Commit 00a5bbf

Browse files
chore: adding tests fixing lint errors
1 parent b8f528f commit 00a5bbf

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

ui/components/multichain-accounts/add-wallet-modal/add-wallet-modal.test.tsx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@ import { screen, fireEvent } from '@testing-library/react';
33
import { renderWithProvider } from '../../../../test/jest';
44
import { AddWalletModal } from './add-wallet-modal';
55

6+
const mockHistoryPush = jest.fn();
7+
const mockOpenExtensionInBrowser = jest.fn();
8+
9+
jest.mock('react-router-dom', () => ({
10+
...jest.requireActual('react-router-dom'),
11+
useHistory: () => ({
12+
push: mockHistoryPush,
13+
}),
14+
}));
15+
616
describe('AddWalletModal', () => {
717
const mockOnClose = jest.fn();
818

919
beforeEach(() => {
1020
mockOnClose.mockClear();
21+
mockHistoryPush.mockClear();
22+
mockOpenExtensionInBrowser.mockClear();
23+
24+
// @ts-expect-error mocking platform
25+
global.platform = {
26+
openExtensionInBrowser: mockOpenExtensionInBrowser,
27+
};
1128
});
1229

1330
it('renders the modal when isOpen is true', () => {
@@ -24,12 +41,47 @@ describe('AddWalletModal', () => {
2441
expect(screen.getByText('Add a hardware wallet')).toBeInTheDocument();
2542
});
2643

27-
it('calls onClose when a wallet option is clicked', () => {
44+
it('calls onClose and navigates when import wallet option is clicked', () => {
2845
renderWithProvider(<AddWalletModal isOpen={true} onClose={mockOnClose} />);
2946

3047
fireEvent.click(screen.getByText('Import a wallet'));
3148

3249
expect(mockOnClose).toHaveBeenCalledTimes(1);
50+
expect(mockHistoryPush).toHaveBeenCalledWith('/import-srp');
51+
});
52+
53+
it('calls onClose and navigates when import account option is clicked', () => {
54+
renderWithProvider(<AddWalletModal isOpen={true} onClose={mockOnClose} />);
55+
56+
fireEvent.click(screen.getByText('Import an account'));
57+
58+
expect(mockOnClose).toHaveBeenCalledTimes(1);
59+
expect(mockHistoryPush).toHaveBeenCalledWith('/new-account');
60+
});
61+
62+
it('calls onClose and opens hardware wallet route in expanded view', () => {
63+
renderWithProvider(<AddWalletModal isOpen={true} onClose={mockOnClose} />);
64+
65+
fireEvent.click(screen.getByText('Add a hardware wallet'));
66+
67+
expect(mockOnClose).toHaveBeenCalledTimes(1);
68+
expect(mockOpenExtensionInBrowser).toHaveBeenCalledWith(
69+
'/new-account/connect',
70+
);
71+
expect(mockHistoryPush).not.toHaveBeenCalled();
72+
});
73+
74+
it('falls back to history.push when openExtensionInBrowser is not available for hardware wallet', () => {
75+
// @ts-expect-error mocking platform
76+
global.platform = {};
77+
78+
renderWithProvider(<AddWalletModal isOpen={true} onClose={mockOnClose} />);
79+
80+
fireEvent.click(screen.getByText('Add a hardware wallet'));
81+
82+
expect(mockOnClose).toHaveBeenCalledTimes(1);
83+
expect(mockOpenExtensionInBrowser).not.toHaveBeenCalled();
84+
expect(mockHistoryPush).toHaveBeenCalledWith('/new-account/connect');
3385
});
3486

3587
it('does not render when isOpen is false', () => {

ui/components/multichain-accounts/add-wallet-modal/add-wallet-modal.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ export const AddWalletModal: React.FC<AddWalletModalProps> = ({
7878

7979
const handleOptionClick = (option: WalletOption) => {
8080
onClose?.();
81-
history.push(option.route);
81+
82+
// Hardware wallet connections require expanded view
83+
if (option.id === 'hardware-wallet') {
84+
if (global.platform.openExtensionInBrowser) {
85+
global.platform.openExtensionInBrowser(option.route);
86+
} else {
87+
history.push(option.route);
88+
}
89+
} else {
90+
history.push(option.route);
91+
}
8292
};
8393

8494
return (

ui/pages/multichain-accounts/account-list/account-list.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import configureStore from '../../../store/store';
77
import { AccountList } from './account-list';
88

99
const mockHistoryGoBack = jest.fn();
10+
const mockHistoryPush = jest.fn();
1011

1112
jest.mock('react-router-dom', () => ({
1213
...jest.requireActual('react-router-dom'),
1314
useHistory: () => ({
1415
goBack: mockHistoryGoBack,
16+
push: mockHistoryPush,
1517
}),
1618
}));
1719

1820
describe('AccountList', () => {
1921
beforeEach(() => {
2022
jest.clearAllMocks();
23+
// Disable reselect dev mode checks for testing
24+
process.env.NODE_ENV = 'production';
2125
});
2226

2327
const renderComponent = () => {
@@ -68,6 +72,10 @@ describe('AccountList', () => {
6872
current: {
6973
back: 'Back',
7074
accounts: 'Accounts',
75+
addWallet: 'Add wallet',
76+
importAWallet: 'Import a wallet',
77+
importAnAccount: 'Import an account',
78+
addAHardwareWallet: 'Add a hardware wallet',
7179
},
7280
},
7381
});
@@ -100,4 +108,19 @@ describe('AccountList', () => {
100108

101109
expect(mockHistoryGoBack).toHaveBeenCalledTimes(1);
102110
});
111+
112+
it('opens the add wallet modal when the add wallet button is clicked', () => {
113+
renderComponent();
114+
115+
// First, let's verify the button is rendered by looking for it with role
116+
const addWalletButton = screen.getByRole('button', { name: 'Add wallet' });
117+
expect(addWalletButton).toBeInTheDocument();
118+
119+
fireEvent.click(addWalletButton);
120+
121+
// The modal renders with portal, so we need to look for modal content
122+
expect(screen.getByText('Import a wallet')).toBeInTheDocument();
123+
expect(screen.getByText('Import an account')).toBeInTheDocument();
124+
expect(screen.getByText('Add a hardware wallet')).toBeInTheDocument();
125+
});
103126
});

ui/pages/multichain-accounts/account-list/account-list.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,14 @@ import {
1111
ButtonIcon,
1212
ButtonIconSize,
1313
IconName,
14-
Text,
1514
} from '@metamask/design-system-react';
1615
import {
1716
Content,
1817
Footer,
1918
Header,
2019
Page,
2120
} from '../../../components/multichain/pages/page';
22-
import {
23-
Display,
24-
FlexDirection,
25-
TextVariant,
26-
} from '../../../helpers/constants/design-system';
21+
import { TextVariant } from '../../../helpers/constants/design-system';
2722
import { useI18nContext } from '../../../hooks/useI18nContext';
2823
import { MultichainAccountList } from '../../../components/multichain-accounts/multichain-account-list';
2924
import { getAccountTree } from '../../../selectors/multichain-accounts/account-tree';

0 commit comments

Comments
 (0)