Skip to content

Commit

Permalink
refactor(assets-controllers): Simplify TokensController tests (#3745)
Browse files Browse the repository at this point in the history
## Explanation

The `TokensController` tests have been refactored to use mocks instead
of a real `PreferencesController` instance. This should make the tests
easier to read, and it will decouple them from the
`PreferencesController`.

## References

Relates to #3708

## Changelog

No changes

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
Gudahtt authored Jan 8, 2024
1 parent d6c472e commit afdbf0f
Showing 1 changed file with 76 additions and 26 deletions.
102 changes: 76 additions & 26 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import type {
ProviderConfig,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { PreferencesController } from '@metamask/preferences-controller';
import {
getDefaultPreferencesState,
type PreferencesState,
} from '@metamask/preferences-controller';
import nock from 'nock';
import * as sinon from 'sinon';

Expand Down Expand Up @@ -66,7 +69,7 @@ type ApprovalActions = AddApprovalRequest;

describe('TokensController', () => {
let tokensController: TokensController;
let preferences: PreferencesController;
let triggerPreferencesStateChange: (state: PreferencesState) => void;
const messenger = new ControllerMessenger<
ApprovalActions,
ApprovalControllerEvents
Expand Down Expand Up @@ -108,10 +111,14 @@ describe('TokensController', () => {

beforeEach(async () => {
const defaultSelectedAddress = '0x1';
preferences = new PreferencesController();
const preferencesStateChangeListeners: ((
state: PreferencesState,
) => void)[] = [];
tokensController = new TokensController({
chainId: ChainId.mainnet,
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
onPreferencesStateChange: (listener) => {
preferencesStateChangeListeners.push(listener);
},
onNetworkDidChange: (listener) => (onNetworkDidChangeListener = listener),
onTokenListStateChange,
config: {
Expand All @@ -123,6 +130,11 @@ describe('TokensController', () => {
getNetworkClientById: sinon.stub() as any,
messenger: tokensControllerMessenger,
});
triggerPreferencesStateChange = (state: PreferencesState) => {
for (const listener of preferencesStateChangeListeners) {
listener(state);
}
};
});

afterEach(() => {
Expand Down Expand Up @@ -325,15 +337,24 @@ describe('TokensController', () => {
const firstAddress = '0x123';
const secondAddress = '0x321';

preferences.update({ selectedAddress: firstAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: firstAddress,
});
await tokensController.addToken({
address: '0x01',
symbol: 'bar',
decimals: 2,
});
preferences.update({ selectedAddress: secondAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: secondAddress,
});
expect(tokensController.state.tokens).toHaveLength(0);
preferences.update({ selectedAddress: firstAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: firstAddress,
});
expect(tokensController.state.tokens[0]).toStrictEqual({
address: '0x01',
decimals: 2,
Expand Down Expand Up @@ -431,21 +452,30 @@ describe('TokensController', () => {
const stub = stubCreateEthers(tokensController, () => false);
const firstAddress = '0x123';
const secondAddress = '0x321';
preferences.update({ selectedAddress: firstAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: firstAddress,
});
await tokensController.addToken({
address: '0x02',
symbol: 'baz',
decimals: 2,
});
preferences.update({ selectedAddress: secondAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: secondAddress,
});
await tokensController.addToken({
address: '0x01',
symbol: 'bar',
decimals: 2,
});
tokensController.ignoreTokens(['0x01']);
expect(tokensController.state.tokens).toHaveLength(0);
preferences.update({ selectedAddress: firstAddress });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: firstAddress,
});
expect(tokensController.state.tokens[0]).toStrictEqual({
address: '0x02',
decimals: 2,
Expand Down Expand Up @@ -490,19 +520,15 @@ describe('TokensController', () => {
stub.restore();
});

it('should subscribe to new sibling preference controllers', async () => {
const address = '0x123';
preferences.update({ selectedAddress: address });
changeNetwork(SEPOLIA);
expect(preferences.state.selectedAddress).toStrictEqual(address);
});

describe('ignoredTokens', () => {
const defaultSelectedAddress = '0x0001';

let createEthersStub: sinon.SinonStub;
beforeEach(() => {
preferences.setSelectedAddress(defaultSelectedAddress);
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: defaultSelectedAddress,
});
changeNetwork(SEPOLIA);

createEthersStub = stubCreateEthers(tokensController, () => false);
Expand Down Expand Up @@ -539,7 +565,10 @@ describe('TokensController', () => {

it('should remove a token from the ignoredTokens/allIgnoredTokens lists if re-added as part of a bulk addTokens add', async () => {
const selectedAddress = '0x0001';
preferences.setSelectedAddress(selectedAddress);
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress,
});
changeNetwork(SEPOLIA);
await tokensController.addToken({
address: '0x01',
Expand Down Expand Up @@ -596,7 +625,10 @@ describe('TokensController', () => {
const selectedAddress1 = '0x0001';
const selectedAddress2 = '0x0002';

preferences.setSelectedAddress(selectedAddress1);
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: selectedAddress1,
});
changeNetwork(SEPOLIA);

await tokensController.addToken({
Expand All @@ -620,7 +652,10 @@ describe('TokensController', () => {
tokensController.ignoreTokens(['0x02']);
expect(tokensController.state.ignoredTokens).toStrictEqual(['0x02']);

preferences.setSelectedAddress(selectedAddress2);
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: selectedAddress2,
});
expect(tokensController.state.ignoredTokens).toHaveLength(0);
await tokensController.addToken({
address: '0x03',
Expand Down Expand Up @@ -921,7 +956,10 @@ describe('TokensController', () => {
const CONFIGURED_CHAIN = SEPOLIA;
const CONFIGURED_ADDRESS = '0xConfiguredAddress';
changeNetwork(CONFIGURED_CHAIN);
preferences.update({ selectedAddress: CONFIGURED_ADDRESS });
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: CONFIGURED_ADDRESS,
});

// A different chain + address
const OTHER_CHAIN = '0xOtherChainId';
Expand Down Expand Up @@ -1656,7 +1694,10 @@ describe('TokensController', () => {
describe('onPreferencesStateChange', function () {
it('should update tokens list when set address changes', async function () {
const stub = stubCreateEthers(tokensController, () => false);
preferences.setSelectedAddress('0x1');
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: '0x1',
});
await tokensController.addToken({
address: '0x01',
symbol: 'A',
Expand All @@ -1667,14 +1708,20 @@ describe('TokensController', () => {
symbol: 'B',
decimals: 5,
});
preferences.setSelectedAddress('0x2');
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: '0x2',
});
expect(tokensController.state.tokens).toStrictEqual([]);
await tokensController.addToken({
address: '0x03',
symbol: 'C',
decimals: 6,
});
preferences.setSelectedAddress('0x1');
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: '0x1',
});
expect(tokensController.state.tokens).toStrictEqual([
{
address: '0x01',
Expand All @@ -1697,7 +1744,10 @@ describe('TokensController', () => {
name: undefined,
},
]);
preferences.setSelectedAddress('0x2');
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
selectedAddress: '0x2',
});
expect(tokensController.state.tokens).toStrictEqual([
{
address: '0x03',
Expand Down

0 comments on commit afdbf0f

Please sign in to comment.