Skip to content

Commit

Permalink
feat(preferences-controller): Convert to BaseControllerV2
Browse files Browse the repository at this point in the history
The `PreferencesController` has been migrated to `BaseControllerV2`. As
part of this migration, the unused `name` state property has also been
removed.

Closes #3708
  • Loading branch information
Gudahtt committed Jan 9, 2024
1 parent 845fcf9 commit 24e158f
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 130 deletions.
98 changes: 64 additions & 34 deletions packages/preferences-controller/src/PreferencesController.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ControllerMessenger } from '@metamask/base-controller';

import { ETHERSCAN_SUPPORTED_CHAIN_IDS } from './constants';
import type { EtherscanSupportedHexChainId } from './PreferencesController';
import { PreferencesController } from './PreferencesController';

describe('PreferencesController', () => {
it('should set default state', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
expect(controller.state).toStrictEqual({
featureFlags: {},
identities: {},
Expand All @@ -31,7 +33,7 @@ describe('PreferencesController', () => {
});

it('should add identities', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.addIdentities(['0x00']);
controller.addIdentities(['0x00']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
Expand All @@ -42,9 +44,16 @@ describe('PreferencesController', () => {
});

it('should remove identity', () => {
const controller = new PreferencesController();
controller.addIdentities(['0x00', '0x01', '0x02']);
controller.update({ selectedAddress: '0x00' });
const controller = setupPreferencesController({
state: {
identities: {
'0x00': { address: '0x00', name: 'Account 1' },
'0x01': { address: '0x01', name: 'Account 2' },
'0x02': { address: '0x02', name: 'Account 3' },
},
selectedAddress: '0x00',
},
});
controller.removeIdentity('0x00');
controller.removeIdentity('0x02');
controller.removeIdentity('0x00');
Expand All @@ -53,7 +62,7 @@ describe('PreferencesController', () => {
});

it('should set identity label', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.addIdentities(['0x00']);
controller.setAccountLabel('0x00', 'bar');
controller.setAccountLabel('0x01', 'qux');
Expand All @@ -62,7 +71,7 @@ describe('PreferencesController', () => {
});

it('should sync identities', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.addIdentities(['0x00', '0x01']);
controller.syncIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
Expand All @@ -82,7 +91,7 @@ describe('PreferencesController', () => {
});

it('should add new identities', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
Expand All @@ -97,10 +106,11 @@ describe('PreferencesController', () => {
});

it('should not update existing identities', () => {
const controller = new PreferencesController(
{},
{ identities: { '0x01': { address: '0x01', name: 'Custom name' } } },
);
const controller = setupPreferencesController({
state: {
identities: { '0x01': { address: '0x01', name: 'Custom name' } },
},
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
Expand All @@ -113,113 +123,133 @@ describe('PreferencesController', () => {
});

it('should remove identities', () => {
const controller = new PreferencesController(
{},
{
const controller = setupPreferencesController({
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x00': { address: '0x00', name: 'Account 1' },
},
},
);
});
controller.updateIdentities(['0x00']);
expect(controller.state.identities).toStrictEqual({
'0x00': { address: '0x00', name: 'Account 1' },
});
});

it('should not update selected address if it is still among identities', () => {
const controller = new PreferencesController(
{},
{
const controller = setupPreferencesController({
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x00': { address: '0x00', name: 'Account 1' },
},
selectedAddress: '0x01',
},
);
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.selectedAddress).toBe('0x01');
});

it('should update selected address to first identity if it was removed from identities', () => {
const controller = new PreferencesController(
{},
{
const controller = setupPreferencesController({
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x02': { address: '0x02', name: 'Account 3' },
'0x00': { address: '0x00', name: 'Account 1' },
},
selectedAddress: '0x02',
},
);
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.selectedAddress).toBe('0x00');
});

it('should set IPFS gateway', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setIpfsGateway('https://ipfs.infura.io/ipfs/');
expect(controller.state.ipfsGateway).toBe('https://ipfs.infura.io/ipfs/');
});

it('should update selected address as checksummed', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setSelectedAddress('0x95d2bc047b0ddec1e4a178eeb64d59f5e735cd0a');
expect(controller.state.selectedAddress).toBe(
'0x95D2bC047B0dDEc1E4A178EeB64d59F5E735cd0A',
);
});

it('should set useTokenDetection', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setUseTokenDetection(true);
expect(controller.state.useTokenDetection).toBe(true);
});

it('should set useNftDetection', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setOpenSeaEnabled(true);
controller.setUseNftDetection(true);
expect(controller.state.useNftDetection).toBe(true);
});

it('should set securityAlertsEnabled', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setSecurityAlertsEnabled(true);
expect(controller.state.securityAlertsEnabled).toBe(true);
});

it('should set disabledRpcMethodPreferences', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setDisabledRpcMethodPreference('eth_sign', true);
expect(controller.state.disabledRpcMethodPreferences.eth_sign).toBe(true);
});

it('should set isMultiAccountBalancesEnabled', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setIsMultiAccountBalancesEnabled(true);
expect(controller.state.isMultiAccountBalancesEnabled).toBe(true);
});

it('should set showTestNetworks', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setShowTestNetworks(true);
expect(controller.state.showTestNetworks).toBe(true);
});

it('should set isIpfsGatewayEnabled', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();
controller.setIsIpfsGatewayEnabled(true);
expect(controller.state.isIpfsGatewayEnabled).toBe(true);
});

it('should set showIncomingTransactions to false on ethereum network', () => {
const controller = new PreferencesController();
const controller = setupPreferencesController();

controller.setEnableNetworkIncomingTransactions('0x1', false);
expect(controller.state.showIncomingTransactions['0x1']).toBe(false);
});
});

/**
* Setup a PreferencesController instance for testing.
*
* @param options - PreferencesController options.
* @returns A PreferencesController instance.
*/
function setupPreferencesController(
options: Partial<ConstructorParameters<typeof PreferencesController>[0]> = {},
) {
const controllerMessenger = new ControllerMessenger();
const preferencesControllerMessenger = controllerMessenger.getRestricted<
'PreferencesController',
never,
never
>({
name: 'PreferencesController',
});
return new PreferencesController({
messenger: preferencesControllerMessenger,
...options,
});
}
Loading

0 comments on commit 24e158f

Please sign in to comment.