Skip to content

Commit

Permalink
fix: add test to account list
Browse files Browse the repository at this point in the history
  • Loading branch information
montelaidev committed Jun 19, 2024
1 parent 5741013 commit e101367
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
1 change: 1 addition & 0 deletions ui/components/ui/account-list/account-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const AccountList = ({
display={Display.Flex}
width={BlockSize.Full}
key={`choose-account-list-${index}`}
data-testid={`choose-account-list-${index}`}
onClick={() => handleEVMAccountClick(account)}
className="choose-account-list__account"
ref={
Expand Down
107 changes: 81 additions & 26 deletions ui/components/ui/account-list/account-list.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,59 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { screen, fireEvent } from '@testing-library/react';
import {
BtcAccountType,
BtcMethod,
EthAccountType,
} from '@metamask/keyring-api';
import { renderWithProvider } from '../../../../test/jest';
import configureStore from '../../../store/store';
import mockState from '../../../../test/data/mock-state.json';
import { ETH_EOA_METHODS } from '../../../../shared/constants/eth-methods';
import AccountList from './account-list';

const render = () => {
const mockHandleAccountClick = jest.fn();

const defaultArgs = {
accounts: [
{
address: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4',
addressLabel: 'Account 1',
label: 'Account 1',
balance: '87a73149c048545a3fe58',
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
name: 'Account 1',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: ETH_EOA_METHODS,
type: EthAccountType.Eoa,
has: () => {
/** nothing to do */
},
},
],
selectedAccounts: new Set(['0x64a845a5b02460acf8a3d84503b0d68d028b4bb4']),
addressLastConnectedMap: {
'0x64a845a5b02460acf8a3d84503b0d68d028b4bb4': 'Feb-22-2022',
},
allAreSelected: () => true,
nativeCurrency: 'USD',
selectNewAccountViaModal: jest.fn(),
deselectAll: jest.fn(),
selectAll: jest.fn(),
handleAccountClick: mockHandleAccountClick,
};

const render = (args = defaultArgs) => {
const store = configureStore({
metamask: {
...mockState.metamask,
},
});

const args = {
accounts: [
{
address: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4',
addressLabel: 'Account 1',
label: 'Account 1',
lastConnectedDate: 'Feb-22-2022',
balance: '87a73149c048545a3fe58',
has: () => {
/** nothing to do */
},
},
],
selectedAccounts: new Set(['0x64a845a5b02460acf8a3d84503b0d68d028b4bb4']),
addressLastConnectedMap: {
'0x64a845a5b02460acf8a3d84503b0d68d028b4bb4': 'Feb-22-2022',
},
allAreSelected: () => true,
nativeCurrency: 'USD',
selectNewAccountViaModal: jest.fn(),
deselectAll: jest.fn(),
selectAll: jest.fn(),
handleAccountClick: jest.fn(),
};
return renderWithProvider(<AccountList {...args} />, store);
};

Expand All @@ -54,4 +72,41 @@ describe('AccountList', () => {
render();
expect(screen.getByText('ETH')).toBeInTheDocument();
});

it('handleAccountClick is disabled for non-EVM accounts', () => {
const { container } = render({
...defaultArgs,
accounts: [
{
id: 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3',
metadata: {
name: 'Btc account',
keyring: {
type: 'HD Key Tree',
},
},
options: {},
methods: [BtcMethod.SendMany],
type: BtcAccountType.P2wpkh,
address: 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
},
],
});
const accountButton = container.querySelector(
'[data-testid="choose-account-list-0"]',
);

fireEvent.click(accountButton);
expect(mockHandleAccountClick).not.toHaveBeenCalled();
});

it('handleAccountClick is enabled for EVM accounts', () => {
const { container } = render();
const accountButton = container.querySelector(
'[data-testid="choose-account-list-0"]',
);

fireEvent.click(accountButton);
expect(mockHandleAccountClick).toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion ui/selectors/institutional/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { toChecksumAddress } from 'ethereumjs-util';
import { getAccountType, getSelectedInternalAccount } from '../selectors';
import { getProviderConfig } from '../../ducks/metamask/metamask';
import { hexToDecimal } from '../../../shared/modules/conversion.utils';
import { normalizeSafeAddress } from '../../../app/scripts/lib/multichain/address';

export function getWaitForConfirmDeepLinkDialog(state) {
return state.metamask.waitForConfirmDeepLinkDialog;
Expand Down Expand Up @@ -36,7 +37,7 @@ export function getConfiguredCustodians(state) {
export function getCustodianIconForAddress(state, address) {
let custodianIcon;

const checksummedAddress = address && toChecksumAddress(address);
const checksummedAddress = address && normalizeSafeAddress(address);
if (
checksummedAddress &&
state.metamask.custodyAccountDetails?.[checksummedAddress]
Expand Down

0 comments on commit e101367

Please sign in to comment.