Skip to content

Commit 274ac4c

Browse files
committed
chore: unit test defensive get on KeyringController
1 parent ec553eb commit 274ac4c

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

app/components/UI/NetworkMultiSelectorList/NetworkMultiSelectorList.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ jest.mock('react-redux', () => ({
3131
useSelector: jest.fn(),
3232
}));
3333

34+
// Avoid loading keyring-utils, keyring-api, and the network/Engine chain in this test
35+
jest.mock('../../../selectors/accountsController', () => ({
36+
selectSelectedInternalAccountFormattedAddress: jest.fn(),
37+
}));
38+
39+
jest.mock('../../../util/address', () => ({
40+
isHardwareAccount: jest.fn(() => false),
41+
}));
42+
43+
jest.mock('@metamask/keyring-api', () => ({
44+
EntropySourceId: {},
45+
BtcMethod: {},
46+
EthMethod: {},
47+
SolAccountType: {},
48+
SolMethod: {},
49+
TrxMethod: {},
50+
isEvmAccountType: jest.fn(),
51+
KeyringAccountType: {},
52+
EthScope: {},
53+
}));
54+
3455
jest.mock('react-native-safe-area-context', () => ({
3556
useSafeAreaInsets: jest.fn(),
3657
}));

app/util/address/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,11 @@ export function getKeyringByAddress(address: string) {
266266
if (!isValidHexAddress(address)) {
267267
return undefined;
268268
}
269-
const { KeyringController } = Engine.context;
270-
const { keyrings } = KeyringController.state;
269+
const KeyringController = Engine.context?.KeyringController;
270+
const keyrings = KeyringController?.state?.keyrings;
271+
if (!keyrings?.length) {
272+
return undefined;
273+
}
271274
return keyrings.find((keyring) =>
272275
keyring.accounts
273276
.map((account) => toFormattedAddress(account))

0 commit comments

Comments
 (0)