Skip to content

Commit

Permalink
Hotfix issue with selecting empty accounts when HW scanning (#4184)
Browse files Browse the repository at this point in the history
* Fix a bug with selecting empty accounts when HW scanning

* Add test
  • Loading branch information
FrederikBolding authored Nov 1, 2021
1 parent 765a8b9 commit f8f0687
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 40 deletions.
27 changes: 27 additions & 0 deletions jest_config/__fixtures__/hdWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DWAccountDisplay, ExtendedDPath } from '@services/WalletService/deterministic';
import { TAddress } from '@types';

import { fAccount } from './account';

const addressToTestWith = fAccount.address as TAddress;

export const fExtendedDPath: ExtendedDPath = {
name: 'Ledger (ETH)',
path: "m/44'/60'/0'",
offset: 0,
numOfAddresses: 1
};

export const fDWAccountDisplayPreBalance: DWAccountDisplay = {
address: addressToTestWith,
pathItem: {
baseDPath: fExtendedDPath,
path: "m/44'/60'/0'/0",
index: 0
}
};

export const fDWAccountDisplay: DWAccountDisplay = {
...fDWAccountDisplayPreBalance,
balance: '0'
};
1 change: 1 addition & 0 deletions jest_config/__fixtures__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ export * from './swapQuote';
export { fBalances } from './balances';
export * from './ens';
export * from './nft';
export * from './hdWallet';
68 changes: 68 additions & 0 deletions src/features/AddAccount/components/HDWList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';

import { LEDGER_ETH } from '@mycrypto/wallets';
import { fireEvent, simpleRender, waitFor } from 'test-utils';

import { fAssets, fDWAccounts, fNetwork } from '@fixtures';
import { translateRaw } from '@translations';

import HDWList from './HDWList';

function getComponent(props: React.ComponentProps<typeof HDWList>) {
return simpleRender(<HDWList {...props} />);
}

const defaultProps = {
scannedAccounts: fDWAccounts,
asset: fAssets[0],
isCompleted: true,
network: fNetwork,
displayEmptyAddresses: true,
selectedDPath: LEDGER_ETH,
onScanMoreAddresses: jest.fn(),
onUnlock: jest.fn(),
handleUpdate: jest.fn()
};

describe('HDWList', () => {
beforeEach(() => {
window.URL.createObjectURL = jest.fn();
});

afterEach(() => {
jest.restoreAllMocks();
});

it('can render', () => {
const props = { ...defaultProps };
const { getByText } = getComponent(props);
expect(
getByText(translateRaw('DETERMINISTIC_SEE_SUMMARY'), { exact: false })
).toBeInTheDocument();
});

it('can select/unselect address', async () => {
const props = { ...defaultProps };
const { getByText, getByTestId } = getComponent(props);
const testId = `row-${fDWAccounts[1].address}`;
const address = getByTestId(testId);
fireEvent.click(address);
await waitFor(() =>
expect(
getByText(translateRaw('DETERMINISTIC_SCANNING_EMPTY_ADDR', { $count: '1', $total: '5' }), {
exact: false
})
).toBeInTheDocument()
);

const address2 = getByTestId(testId);
fireEvent.click(address2);
await waitFor(() =>
expect(
getByText(translateRaw('DETERMINISTIC_SCANNING_EMPTY_ADDR', { $count: '0', $total: '5' }), {
exact: false
})
).toBeInTheDocument()
);
});
});
21 changes: 7 additions & 14 deletions src/features/AddAccount/components/HDWList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,18 @@ export default function HDWList({
};

const handleSelection = (account: TableAccountDisplay) => {
const filtered = tableAccounts.filter(
(a) =>
a.pathItem.index !== account.pathItem.index ||
a.pathItem.baseDPath.path !== account.pathItem.baseDPath.path
);
if (account.isSelected) {
setTableAccounts({
...tableAccounts,
[account.address]: {
...account,
isSelected: !account.isSelected
}
});
setTableAccounts([...filtered, { ...account, isSelected: !account.isSelected }]);
return;
}
// disallows selecting an account that is empty if MAX_EMPTY_ADDRESSES is already met
if (emptySelectedAccounts.length >= MAX_EMPTY_ADDRESSES && !hasBalance(account.balance)) return;
setTableAccounts({
...tableAccounts,
[account.address]: {
...account,
isSelected: !account.isSelected
}
});
setTableAccounts([...filtered, { ...account, isSelected: !account.isSelected }]);
};
return (
<ListContainer variant="columnAlign" justifyContent="center">
Expand Down
7 changes: 6 additions & 1 deletion src/features/AddAccount/components/HDWTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ const HDTable = ({
) : (
<Body>
{accountsToDisplay.map((account: TableAccountDisplay, index) => (
<Row key={index} onClick={() => onSelect(account)} isSelected={account.isSelected}>
<Row
key={index}
onClick={() => onSelect(account)}
isSelected={account.isSelected}
data-testid={`row-${account.address}`}
>
<SelectedContainer isSelected={account.isSelected}>
<Icon type="check" />
</SelectedContainer>
Expand Down
35 changes: 10 additions & 25 deletions src/features/AddAccount/components/hdWallet.slice.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { DeterministicWallet } from '@mycrypto/wallets';
import { expectSaga, mockAppState } from 'test-utils';

import { fAccount, fAssets, fNetwork, fNetworks } from '@fixtures';
import { DWAccountDisplay, ExtendedDPath, getWallet } from '@services/WalletService';
import {
fAssets,
fDWAccountDisplay,
fDWAccountDisplayPreBalance,
fExtendedDPath,
fNetwork,
fNetworks
} from '@fixtures';
import { getWallet } from '@services/WalletService';
import { AppState } from '@store/root.reducer';
import { DPathFormat, TAddress, WalletId } from '@types';
import { DPathFormat, WalletId } from '@types';
import { bigify as mockBigify, noOp } from '@utils';

import {
Expand Down Expand Up @@ -37,28 +44,6 @@ const {
addCustomDPaths,
triggerComplete
} = slice.actions;
const addressToTestWith = fAccount.address as TAddress;

const fExtendedDPath: ExtendedDPath = {
name: 'Ledger (ETH)',
path: "m/44'/60'/0'",
offset: 0,
numOfAddresses: 1
};

const fDWAccountDisplayPreBalance: DWAccountDisplay = {
address: addressToTestWith,
pathItem: {
baseDPath: fExtendedDPath,
path: "m/44'/60'/0'/0",
index: 0
}
};

const fDWAccountDisplay: DWAccountDisplay = {
...fDWAccountDisplayPreBalance,
balance: '0'
};

describe('HD Wallet Slice', () => {
const err = {
Expand Down

0 comments on commit f8f0687

Please sign in to comment.