Skip to content

feat: Convert mmi controller to a non-controller #27983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 7, 2024
91 changes: 59 additions & 32 deletions app/scripts/controllers/mmi-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import {
NETWORK_TYPES,
TEST_NETWORK_TICKER_MAP,
} from '../../../shared/constants/network';
import MMIController from './mmi-controller';
import { MMIController, AllowedActions } from './mmi-controller';
import { AppStateController } from './app-state-controller';
import { ControllerMessenger } from '@metamask/base-controller';
import { mmiKeyringBuilderFactory } from '../mmi-keyring-builder-factory';
import MetaMetricsController from './metametrics';
import { ETH_EOA_METHODS } from '../../../shared/constants/eth-methods';
import { mockNetworkState } from '../../../test/stub/networks';
import { InfuraNetworkType } from '@metamask/controller-utils';
import { API_REQUEST_LOG_EVENT } from '@metamask-institutional/sdk';

jest.mock('@metamask-institutional/portfolio-dashboard', () => ({
Expand All @@ -39,6 +40,21 @@ jest.mock('./permissions', () => ({
}),
}));

export const createMockNetworkConfiguration = (
override?: Partial<NetworkConfiguration>,
): NetworkConfiguration => {
return {
chainId: CHAIN_IDS.SEPOLIA,
blockExplorerUrls: [],
defaultRpcEndpointIndex: 0,
name: 'Mock Network',
nativeCurrency: 'MOCK TOKEN',
rpcEndpoints: [],
defaultBlockExplorerUrlIndex: 0,
...override,
};
};

const mockAccount = {
address: '0x758b8178a9A4B7206d1f648c4a77C515Cbac7001',
id: 'mock-id',
Expand Down Expand Up @@ -73,10 +89,10 @@ describe('MMIController', function () {
mmiConfigurationController,
controllerMessenger,
accountsController,
networkController,
keyringController,
metaMetricsController,
custodyController;
custodyController,
mmiControllerMessenger;

beforeEach(async function () {
const mockMessenger = {
Expand All @@ -89,22 +105,10 @@ describe('MMIController', function () {
subscribe: jest.fn(),
};

networkController = new NetworkController({
messenger: new ControllerMessenger().getRestricted({
name: 'NetworkController',
allowedEvents: [
'NetworkController:stateChange',
'NetworkController:networkWillChange',
'NetworkController:networkDidChange',
'NetworkController:infuraIsBlocked',
'NetworkController:infuraIsUnblocked',
],
}),
state: mockNetworkState({ chainId: CHAIN_IDS.SEPOLIA }),
infuraProjectId: 'mock-infura-project-id',
});

controllerMessenger = new ControllerMessenger();
const controllerMessenger = new ControllerMessenger<
AllowedActions,
never
>();

accountsController = new AccountsController({
messenger: controllerMessenger.getRestricted({
Expand Down Expand Up @@ -212,14 +216,42 @@ describe('MMIController', function () {
onNetworkDidChange: jest.fn(),
});

const mmiControllerMessenger = controllerMessenger.getRestricted({
controllerMessenger.registerActionHandler(
'NetworkController:getState',
jest.fn().mockReturnValue(mockNetworkState({ chainId: CHAIN_IDS.SEPOLIA })),
);

controllerMessenger.registerActionHandler(
'NetworkController:setActiveNetwork',
InfuraNetworkType['sepolia'],
);

controllerMessenger.registerActionHandler(
'NetworkController:getNetworkClientById',
jest.fn().mockReturnValue({
configuration: {
chainId: CHAIN_IDS.SEPOLIA,
}
}),
);

controllerMessenger.registerActionHandler(
'NetworkController:getNetworkConfigurationByChainId',
jest.fn().mockReturnValue(createMockNetworkConfiguration()),
);

mmiControllerMessenger = controllerMessenger.getRestricted({
name: 'MMIController',
allowedActions: [
'AccountsController:getAccountByAddress',
'AccountsController:setAccountName',
'AccountsController:listAccounts',
'AccountsController:getSelectedAccount',
'AccountsController:setSelectedAccount',
'NetworkController:getState',
'NetworkController:setActiveNetwork',
'NetworkController:getNetworkClientById',
'NetworkController:getNetworkConfigurationByChainId'
],
});

Expand Down Expand Up @@ -253,7 +285,6 @@ describe('MMIController', function () {
})
}
}),
networkController,
permissionController,
custodyController,
metaMetricsController,
Expand Down Expand Up @@ -507,9 +538,7 @@ describe('MMIController', function () {
CUSTODIAN_TYPES['CUSTODIAN-TYPE'] = {
keyringClass: { type: 'mock-keyring-class' },
};
mmiController.messenger.call = jest
.fn()
.mockReturnValue({ address: '0x1' });
jest.spyOn(mmiControllerMessenger, 'call').mockReturnValue({ address: '0x1' });
mmiController.custodyController.getCustodyTypeByAddress = jest
.fn()
.mockReturnValue('custodian-type');
Expand Down Expand Up @@ -628,9 +657,7 @@ describe('MMIController', function () {
mmiController.custodyController.getAccountDetails = jest
.fn()
.mockReturnValue({});
mmiController.messenger.call = jest
.fn()
.mockReturnValue([mockAccount, mockAccount2]);
jest.spyOn(mmiControllerMessenger, 'call').mockReturnValue([mockAccount, mockAccount2]);
mmiController.mmiConfigurationController.store.getState = jest
.fn()
.mockReturnValue({
Expand Down Expand Up @@ -701,7 +728,7 @@ describe('MMIController', function () {

describe('handleMmiCheckIfTokenIsPresent', () => {
it('should check if a token is present', async () => {
mmiController.messenger.call = jest
mmiController.messagingSystem.call = jest
.fn()
.mockReturnValue({ address: '0x1' });
mmiController.custodyController.getCustodyTypeByAddress = jest
Expand Down Expand Up @@ -733,7 +760,7 @@ describe('MMIController', function () {

describe('handleMmiDashboardData', () => {
it('should return internalAccounts as identities', async () => {
const controllerMessengerSpy = jest.spyOn(controllerMessenger, 'call');
const controllerMessengerSpy = jest.spyOn(mmiControllerMessenger, 'call');
await mmiController.handleMmiDashboardData();

expect(controllerMessengerSpy).toHaveBeenCalledWith(
Expand Down Expand Up @@ -811,7 +838,7 @@ describe('MMIController', function () {

describe('setAccountAndNetwork', () => {
it('should set a new selected account if the selectedAddress and the address from the arguments is different', async () => {
const selectedAccountSpy = jest.spyOn(controllerMessenger, 'call');
const selectedAccountSpy = jest.spyOn(mmiControllerMessenger, 'call');
await mmiController.setAccountAndNetwork(
'mock-origin',
mockAccount2.address,
Expand All @@ -829,14 +856,14 @@ describe('MMIController', function () {
});

it('should not set a new selected account the accounts are the same', async () => {
const selectedAccountSpy = jest.spyOn(controllerMessenger, 'call');
const selectedAccountSpy = jest.spyOn(mmiControllerMessenger, 'call');
await mmiController.setAccountAndNetwork(
'mock-origin',
mockAccount.address,
'0x1',
);

expect(selectedAccountSpy).toHaveBeenCalledTimes(1);
expect(selectedAccountSpy).toHaveBeenCalledTimes(4);
const selectedAccount = accountsController.getSelectedAccount();
expect(selectedAccount.id).toBe(mockAccount.id);
});
Expand Down
Loading