Skip to content

Commit

Permalink
refactor: use selectedAccount instead of selectedAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
montelaidev committed Jun 3, 2024
1 parent 00c043e commit 043c08f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 108 deletions.
8 changes: 6 additions & 2 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@
"@metamask/contract-metadata": "^2.4.0",
"@metamask/controller-utils": "^11.0.0",
"@metamask/eth-query": "^4.0.0",
"@metamask/keyring-api": "^6.4.0",
"@metamask/keyring-controller": "^17.0.0",
"@metamask/metamask-eth-abis": "^3.1.1",
"@metamask/network-controller": "^19.0.0",
"@metamask/polling-controller": "^8.0.0",
"@metamask/preferences-controller": "^13.0.0",
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "^4.2.0",
"@metamask/snaps-utils": "^7.4.0",
"@metamask/utils": "^8.3.0",
"@types/bn.js": "^5.1.5",
"@types/uuid": "^8.3.0",
Expand All @@ -73,11 +76,11 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@metamask/ethjs-provider-http": "^0.3.0",
"@metamask/keyring-api": "^6.4.0",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.191",
"@types/node": "^16.18.54",
"deepmerge": "^4.2.2",
"immer": "^9.0.6",
"jest": "^27.5.1",
"jest-environment-jsdom": "^27.5.1",
"nock": "^13.3.1",
Expand All @@ -92,7 +95,8 @@
"@metamask/approval-controller": "^7.0.0",
"@metamask/keyring-controller": "^17.0.0",
"@metamask/network-controller": "^19.0.0",
"@metamask/preferences-controller": "^13.0.0"
"@metamask/preferences-controller": "^13.0.0",
"@metamask/snaps-controllers": "^8.1.1"
},
"engines": {
"node": "^18.18 || >=20"
Expand Down
151 changes: 62 additions & 89 deletions packages/assets-controllers/src/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { createMockInternalAccount } from '@metamask/accounts-controller';
import { query } from '@metamask/controller-utils';
import HttpProvider from '@metamask/ethjs-provider-http';
import {
getDefaultPreferencesState,
type Identity,
type PreferencesState,
} from '@metamask/preferences-controller';
import type { InternalAccount } from '@metamask/keyring-api';
import * as sinon from 'sinon';

import { advanceTime } from '../../../tests/helpers';
Expand All @@ -18,7 +15,9 @@ jest.mock('@metamask/controller-utils', () => {
});

const ADDRESS_1 = '0xc38bf1ad06ef69f0c04e29dbeb4152b4175f0a8d';
const ACCOUNT_1 = createMockInternalAccount({ address: ADDRESS_1 });
const ADDRESS_2 = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
const ACCOUNT_2 = createMockInternalAccount({ address: ADDRESS_2 });

const mockedQuery = query as jest.Mock<
ReturnType<typeof query>,
Expand All @@ -44,9 +43,9 @@ describe('AccountTrackerController', () => {

it('should set default state', () => {
const controller = new AccountTrackerController({
onPreferencesStateChange: sinon.stub(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand All @@ -61,9 +60,11 @@ describe('AccountTrackerController', () => {

it('should throw when provider property is accessed', () => {
const controller = new AccountTrackerController({
onPreferencesStateChange: sinon.stub(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [],
getSelectedAccount: () => {
return {} as InternalAccount;
},
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand All @@ -73,31 +74,31 @@ describe('AccountTrackerController', () => {
);
});

it('should refresh when preferences state changes', async () => {
const preferencesStateChangeListeners: ((
state: PreferencesState,
it('should refresh when selectedAccount changes', async () => {
const selectedAccountChangeListeners: ((
internalAccount: InternalAccount,
) => void)[] = [];
const controller = new AccountTrackerController(
{
onPreferencesStateChange: (listener) => {
preferencesStateChangeListeners.push(listener);
onSelectedAccountChange: (listener) => {
selectedAccountChangeListeners.push(listener);
},
getIdentities: () => ({}),
getSelectedAddress: () => '0x0',
getInternalAccounts: () => [],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
},
{ provider },
);
const triggerPreferencesStateChange = (state: PreferencesState) => {
for (const listener of preferencesStateChangeListeners) {
listener(state);
const triggerSelectedAccountChange = (internalAccount: InternalAccount) => {
for (const listener of selectedAccountChangeListeners) {
listener(internalAccount);
}
};
controller.refresh = sinon.stub();

triggerPreferencesStateChange(getDefaultPreferencesState());
triggerSelectedAccountChange(ACCOUNT_1);

// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -113,16 +114,13 @@ describe('AccountTrackerController', () => {

describe('without networkClientId', () => {
it('should sync addresses', async () => {
const bazAccount = createMockInternalAccount({ address: 'baz' });
const barAccount = createMockInternalAccount({ address: 'bar' });
const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
bar: {} as Identity,
baz: {} as Identity,
};
},
getSelectedAddress: () => '0x0',
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [bazAccount, barAccount],
getSelectedAccount: () => barAccount,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand Down Expand Up @@ -169,11 +167,9 @@ describe('AccountTrackerController', () => {

const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return { [ADDRESS_1]: {} as Identity };
},
getSelectedAddress: () => ADDRESS_1,
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [ACCOUNT_1],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand Down Expand Up @@ -206,14 +202,9 @@ describe('AccountTrackerController', () => {

const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
[ADDRESS_1]: {} as Identity,
[ADDRESS_2]: {} as Identity,
};
},
getSelectedAddress: () => ADDRESS_1,
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [ACCOUNT_1, ACCOUNT_2],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => false,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand Down Expand Up @@ -244,14 +235,9 @@ describe('AccountTrackerController', () => {

const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
[ADDRESS_1]: {} as Identity,
[ADDRESS_2]: {} as Identity,
};
},
getSelectedAddress: () => ADDRESS_1,
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [ACCOUNT_1, ACCOUNT_2],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand All @@ -278,16 +264,13 @@ describe('AccountTrackerController', () => {

describe('with networkClientId', () => {
it('should sync addresses', async () => {
const bazAccount = createMockInternalAccount({ address: 'baz' });
const barAccount = createMockInternalAccount({ address: 'bar' });
const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
bar: {} as Identity,
baz: {} as Identity,
};
},
getSelectedAddress: () => '0x0',
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [bazAccount, barAccount],
getSelectedAccount: () => bazAccount,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -342,11 +325,9 @@ describe('AccountTrackerController', () => {
mockedQuery.mockReturnValueOnce(Promise.resolve('0x10'));

const controller = new AccountTrackerController({
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return { [ADDRESS_1]: {} as Identity };
},
getSelectedAddress: () => ADDRESS_1,
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [ACCOUNT_1],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -386,14 +367,11 @@ describe('AccountTrackerController', () => {
.mockReturnValueOnce(Promise.resolve('0x11'));

const controller = new AccountTrackerController({
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
[ADDRESS_1]: {} as Identity,
[ADDRESS_2]: {} as Identity,
};
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => {
return [ACCOUNT_1, ACCOUNT_2];
},
getSelectedAddress: () => ADDRESS_1,
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => false,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -430,14 +408,11 @@ describe('AccountTrackerController', () => {
.mockReturnValueOnce(Promise.resolve('0x12'));

const controller = new AccountTrackerController({
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {
[ADDRESS_1]: {} as Identity,
[ADDRESS_2]: {} as Identity,
};
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => {
return [ACCOUNT_1, ACCOUNT_2];
},
getSelectedAddress: () => ADDRESS_1,
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -474,11 +449,9 @@ describe('AccountTrackerController', () => {
it('should sync balance with addresses', async () => {
const controller = new AccountTrackerController(
{
onPreferencesStateChange: sinon.stub(),
getIdentities: () => {
return {};
},
getSelectedAddress: () => ADDRESS_1,
onSelectedAccountChange: sinon.stub(),
getInternalAccounts: () => [],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand All @@ -501,9 +474,9 @@ describe('AccountTrackerController', () => {
const poll = sinon.spy(AccountTrackerController.prototype, 'poll');
const controller = new AccountTrackerController(
{
onPreferencesStateChange: jest.fn(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
onSelectedAccountChange: jest.fn(),
getInternalAccounts: () => [],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand All @@ -523,9 +496,9 @@ describe('AccountTrackerController', () => {
sinon.stub(AccountTrackerController.prototype, 'poll');
const controller = new AccountTrackerController(
{
onPreferencesStateChange: jest.fn(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
onSelectedAccountChange: jest.fn(),
getInternalAccounts: () => [],
getSelectedAccount: () => ACCOUNT_1,
getMultiAccountBalancesEnabled: () => true,
getCurrentChainId: () => '0x1',
getNetworkClientById: jest.fn(),
Expand Down
Loading

0 comments on commit 043c08f

Please sign in to comment.