Skip to content

Commit

Permalink
refactor(assets-controllers): Simplify AccountTrackerController tests
Browse files Browse the repository at this point in the history
The `AccountTrackerController` tests have been updated to remove
instances of the `PreferencesController`. Mocks are now used instead of
the real controller.

It was useful to have a reference in tests to the default
`PreferencesController` state, so that has been added as an export.

Relates to #3708
  • Loading branch information
Gudahtt committed Jan 5, 2024
1 parent 9687332 commit 8b32898
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 48 deletions.
31 changes: 21 additions & 10 deletions packages/assets-controllers/src/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { query } from '@metamask/controller-utils';
import HttpProvider from '@metamask/ethjs-provider-http';
import type { Identity } from '@metamask/preferences-controller';
import { PreferencesController } from '@metamask/preferences-controller';
import {
defaultPreferencesState,
type Identity,
type PreferencesState,
} from '@metamask/preferences-controller';
import * as sinon from 'sinon';

import { advanceTime } from '../../../tests/helpers';
Expand Down Expand Up @@ -70,11 +73,15 @@ describe('AccountTrackerController', () => {
);
});

it('should subscribe to new sibling preference controllers', async () => {
const preferences = new PreferencesController();
it('should refresh when preferences state changes', async () => {
const preferencesStateChangeListeners: ((
state: PreferencesState,
) => void)[] = [];
const controller = new AccountTrackerController(
{
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
onPreferencesStateChange: (listener) => {
preferencesStateChangeListeners.push(listener);
},
getIdentities: () => ({}),
getSelectedAddress: () => '0x0',
getMultiAccountBalancesEnabled: () => true,
Expand All @@ -83,9 +90,15 @@ describe('AccountTrackerController', () => {
},
{ provider },
);
const triggerPreferencesStateChange = (state: PreferencesState) => {
for (const listener of preferencesStateChangeListeners) {
listener(state);
}
};
controller.refresh = sinon.stub();

preferences.setFeatureFlag('foo', true);
triggerPreferencesStateChange(defaultPreferencesState);

// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((controller.refresh as any).called).toBe(true);
Expand Down Expand Up @@ -485,11 +498,10 @@ describe('AccountTrackerController', () => {
});

it('should call refresh every interval on legacy polling', async () => {
const preferences = new PreferencesController();
const poll = sinon.spy(AccountTrackerController.prototype, 'poll');
const controller = new AccountTrackerController(
{
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
onPreferencesStateChange: jest.fn(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
getMultiAccountBalancesEnabled: () => true,
Expand All @@ -508,11 +520,10 @@ describe('AccountTrackerController', () => {
});

it('should call refresh every interval for each networkClientId being polled', async () => {
const preferences = new PreferencesController();
sinon.stub(AccountTrackerController.prototype, 'poll');
const controller = new AccountTrackerController(
{
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
onPreferencesStateChange: jest.fn(),
getIdentities: () => ({}),
getSelectedAddress: () => '',
getMultiAccountBalancesEnabled: () => true,
Expand Down
81 changes: 43 additions & 38 deletions packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ export type PreferencesState = {
useTokenDetection: boolean;
};

/**
* Default PreferencesController state.
*/
export const defaultPreferencesState = {
disabledRpcMethodPreferences: {
eth_sign: false,
},
featureFlags: {},
identities: {},
ipfsGateway: 'https://ipfs.io/ipfs/',
isIpfsGatewayEnabled: true,
isMultiAccountBalancesEnabled: true,
lostIdentities: {},
openSeaEnabled: false,
securityAlertsEnabled: false,
selectedAddress: '',
showIncomingTransactions: {
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MAINNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.GOERLI]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.SEPOLIA]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_GOERLI]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_MAINNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONRIVER]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.GNOSIS]: true,
},
showTestNetworks: false,
useNftDetection: false,
useTokenDetection: true,
};

/**
* Controller that stores shared settings and exposes convenience methods
*/
Expand All @@ -128,44 +170,7 @@ export class PreferencesController extends BaseControllerV1<
*/
constructor(config?: Partial<BaseConfig>, state?: Partial<PreferencesState>) {
super(config, state);
this.defaultState = {
disabledRpcMethodPreferences: {
eth_sign: false,
},
featureFlags: {},
identities: {},
ipfsGateway: 'https://ipfs.io/ipfs/',
isIpfsGatewayEnabled: true,
isMultiAccountBalancesEnabled: true,
lostIdentities: {},
openSeaEnabled: false,
securityAlertsEnabled: false,
selectedAddress: '',
showIncomingTransactions: {
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MAINNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.GOERLI]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.SEPOLIA]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_GOERLI]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_MAINNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM_TESTNET]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONRIVER]: true,
[ETHERSCAN_SUPPORTED_CHAIN_IDS.GNOSIS]: true,
},
showTestNetworks: false,
useNftDetection: false,
useTokenDetection: true,
};
this.defaultState = defaultPreferencesState;
this.initialize();
}

Expand Down

0 comments on commit 8b32898

Please sign in to comment.