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 I added a `getDefaultState` static
method. This was added as a method rather than an export to protect
against accidental mutations.

Relates to #3708
  • Loading branch information
Gudahtt committed Jan 5, 2024
1 parent 9687332 commit ecb824b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 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 {
PreferencesController,
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(PreferencesController.getDefaultState());

// 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
21 changes: 15 additions & 6 deletions packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ export class PreferencesController extends BaseControllerV1<
override name = 'PreferencesController';

/**
* Creates a PreferencesController instance.
* Get the default PreferencesController state.
*
* @param config - Initial options used to configure this controller.
* @param state - Initial state to set on this controller.
* @returns The default PreferencesController state.
*/
constructor(config?: Partial<BaseConfig>, state?: Partial<PreferencesState>) {
super(config, state);
this.defaultState = {
static getDefaultState() {
return {
disabledRpcMethodPreferences: {
eth_sign: false,
},
Expand Down Expand Up @@ -166,6 +164,17 @@ export class PreferencesController extends BaseControllerV1<
useNftDetection: false,
useTokenDetection: true,
};
}

/**
* Creates a PreferencesController instance.
*
* @param config - Initial options used to configure this controller.
* @param state - Initial state to set on this controller.
*/
constructor(config?: Partial<BaseConfig>, state?: Partial<PreferencesState>) {
super(config, state);
this.defaultState = PreferencesController.getDefaultState();
this.initialize();
}

Expand Down

0 comments on commit ecb824b

Please sign in to comment.