1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/app/scripts/controllers/preferences.test.js

344 lines
10 KiB
JavaScript
Raw Normal View History

/**
* @jest-environment node
*/
import { ControllerMessenger } from '@metamask/base-controller';
import { TokenListController } from '@metamask/assets-controllers';
import { CHAIN_IDS } from '../../../shared/constants/network';
import PreferencesController from './preferences';
const NETWORK_CONFIGURATION_DATA = {
'test-networkConfigurationId-1': {
rpcUrl: 'https://testrpc.com',
chainId: CHAIN_IDS.GOERLI,
nickname: '0X5',
rpcPrefs: { blockExplorerUrl: 'https://etherscan.io' },
},
'test-networkConfigurationId-2': {
rpcUrl: 'http://localhost:8545',
chainId: '0x539',
ticker: 'ETH',
nickname: 'Localhost 8545',
rpcPrefs: {},
},
};
describe('preferences controller', () => {
let preferencesController;
let tokenListController;
beforeEach(() => {
const tokenListMessenger = new ControllerMessenger().getRestricted({
name: 'TokenListController',
});
tokenListController = new TokenListController({
chainId: '1',
preventPollingOnNetworkRestart: false,
onNetworkStateChange: jest.fn(),
onPreferencesStateChange: jest.fn(),
messenger: tokenListMessenger,
});
2020-11-03 00:41:28 +01:00
preferencesController = new PreferencesController({
initLangCode: 'en_US',
tokenListController,
onInfuraIsBlocked: jest.fn(),
onInfuraIsUnblocked: jest.fn(),
networkConfigurations: NETWORK_CONFIGURATION_DATA,
});
});
Update address book state upon custom RPC chainId edit (#9493) When the `chainId` for a custom RPC endpoint is edited, we now migrate the corresponding address book entries to ensure they are not orphaned. The address book entries are grouped by the `metamask.network` state, which unfortunately was sometimes the `chainId`, and sometimes the `networkId`. It was always the `networkId` for built-in Infura networks, but for custom RPC endpoints it would be set to the user-set `chainId` field, with a fallback to the `networkId` of the network. A recent change will force users to enter valid `chainId`s on all custom networks, which will be normalized to be hex-prefixed. As a result, address book contacts will now be keyed by a different string. The contact entries are now migrated when this edit takes place. There are some edge cases where two separate entries share the same set of contacts. For example, if two entries have the same `chainId`, or if they had the same `networkId` and had no `chainId` set. When the `chainId` is edited in such cases, the contacts are duplicated on both networks. This is the best we can do, as we don't have any way to know which network the contacts _should_ be on. The `typed-message-manager` unit tests have also been updated as part of this commit because the addition of `sinon.restore()` to the preferences controller tests ended up clearing a test object in-between individual tests in that file. The test object is now re-constructed before each individual test.
2020-10-07 19:32:17 +02:00
describe('useBlockie', () => {
it('defaults useBlockie to false', () => {
expect(preferencesController.store.getState().useBlockie).toStrictEqual(
false,
);
});
it('setUseBlockie to true', () => {
preferencesController.setUseBlockie(true);
expect(preferencesController.store.getState().useBlockie).toStrictEqual(
true,
);
});
});
describe('setCurrentLocale', () => {
it('checks the default currentLocale', () => {
const { currentLocale } = preferencesController.store.getState();
expect(currentLocale).toStrictEqual('en_US');
});
it('sets current locale in preferences controller', () => {
preferencesController.setCurrentLocale('ja');
const { currentLocale } = preferencesController.store.getState();
expect(currentLocale).toStrictEqual('ja');
});
});
describe('setAddresses', () => {
it('should keep a map of addresses to names and addresses in the store', () => {
preferencesController.setAddresses(['0xda22le', '0x7e57e2']);
const { identities } = preferencesController.store.getState();
expect(identities).toStrictEqual({
'0xda22le': {
name: 'Account 1',
address: '0xda22le',
},
'0x7e57e2': {
name: 'Account 2',
address: '0x7e57e2',
},
});
});
it('should replace its list of addresses', () => {
preferencesController.setAddresses(['0xda22le', '0x7e57e2']);
preferencesController.setAddresses(['0xda22le77', '0x7e57e277']);
const { identities } = preferencesController.store.getState();
expect(identities).toStrictEqual({
'0xda22le77': {
name: 'Account 1',
address: '0xda22le77',
},
'0x7e57e277': {
name: 'Account 2',
address: '0x7e57e277',
},
});
});
});
describe('removeAddress', () => {
it('should remove an address from state', () => {
preferencesController.setAddresses(['0xda22le', '0x7e57e2']);
2018-07-16 22:08:19 +02:00
preferencesController.removeAddress('0xda22le');
2018-07-16 22:08:19 +02:00
expect(
2020-11-03 00:41:28 +01:00
preferencesController.store.getState().identities['0xda22le'],
).toStrictEqual(undefined);
});
2018-07-16 22:08:19 +02:00
it('should switch accounts if the selected address is removed', () => {
preferencesController.setAddresses(['0xda22le', '0x7e57e2']);
2018-07-16 22:08:19 +02:00
preferencesController.setSelectedAddress('0x7e57e2');
preferencesController.removeAddress('0x7e57e2');
expect(preferencesController.getSelectedAddress()).toStrictEqual(
'0xda22le',
);
});
});
2018-07-16 22:08:19 +02:00
describe('setAccountLabel', () => {
it('should update a label for the given account', () => {
preferencesController.setAddresses(['0xda22le', '0x7e57e2']);
2020-11-03 00:41:28 +01:00
expect(
2020-11-03 00:41:28 +01:00
preferencesController.store.getState().identities['0xda22le'],
).toStrictEqual({
name: 'Account 1',
address: '0xda22le',
});
preferencesController.setAccountLabel('0xda22le', 'Dazzle');
expect(
2020-11-03 00:41:28 +01:00
preferencesController.store.getState().identities['0xda22le'],
).toStrictEqual({
name: 'Dazzle',
address: '0xda22le',
});
});
});
describe('setPasswordForgotten', () => {
it('should default to false', () => {
expect(
2020-11-03 00:41:28 +01:00
preferencesController.store.getState().forgottenPassword,
).toStrictEqual(false);
});
it('should set the forgottenPassword property in state', () => {
preferencesController.setPasswordForgotten(true);
expect(
2020-11-03 00:41:28 +01:00
preferencesController.store.getState().forgottenPassword,
).toStrictEqual(true);
});
});
describe('setUsePhishDetect', () => {
it('should default to true', () => {
expect(
preferencesController.store.getState().usePhishDetect,
).toStrictEqual(true);
});
it('should set the usePhishDetect property in state', () => {
preferencesController.setUsePhishDetect(false);
expect(
preferencesController.store.getState().usePhishDetect,
).toStrictEqual(false);
});
});
describe('setUseMultiAccountBalanceChecker', () => {
it('should default to true', () => {
expect(
preferencesController.store.getState().useMultiAccountBalanceChecker,
).toStrictEqual(true);
});
it('should set the setUseMultiAccountBalanceChecker property in state', () => {
preferencesController.setUseMultiAccountBalanceChecker(false);
expect(
preferencesController.store.getState().useMultiAccountBalanceChecker,
).toStrictEqual(false);
});
});
describe('setUseTokenDetection', () => {
it('should default to false', () => {
expect(
preferencesController.store.getState().useTokenDetection,
).toStrictEqual(false);
});
it('should set the useTokenDetection property in state', () => {
preferencesController.setUseTokenDetection(true);
expect(
preferencesController.store.getState().useTokenDetection,
).toStrictEqual(true);
});
});
describe('setUseNftDetection', () => {
it('should default to false', () => {
expect(
preferencesController.store.getState().useNftDetection,
).toStrictEqual(false);
});
it('should set the useNftDetection property in state', () => {
preferencesController.setOpenSeaEnabled(true);
preferencesController.setUseNftDetection(true);
expect(
preferencesController.store.getState().useNftDetection,
).toStrictEqual(true);
});
});
describe('setUse4ByteResolution', function () {
it('should default to true', function () {
expect(
preferencesController.store.getState().use4ByteResolution,
).toStrictEqual(true);
});
it('should set the use4ByteResolution property in state', function () {
preferencesController.setUse4ByteResolution(false);
expect(
preferencesController.store.getState().use4ByteResolution,
).toStrictEqual(false);
});
});
describe('setOpenSeaEnabled', () => {
it('should default to false', () => {
expect(
preferencesController.store.getState().openSeaEnabled,
).toStrictEqual(false);
});
it('should set the openSeaEnabled property in state', () => {
preferencesController.setOpenSeaEnabled(true);
expect(
preferencesController.store.getState().openSeaEnabled,
).toStrictEqual(true);
});
});
describe('setAdvancedGasFee', () => {
it('should default to null', () => {
expect(
preferencesController.store.getState().advancedGasFee,
).toStrictEqual(null);
});
it('should set the setAdvancedGasFee property in state', () => {
preferencesController.setAdvancedGasFee({
maxBaseFee: '1.5',
priorityFee: '2',
});
expect(
preferencesController.store.getState().advancedGasFee.maxBaseFee,
).toStrictEqual('1.5');
expect(
preferencesController.store.getState().advancedGasFee.priorityFee,
).toStrictEqual('2');
});
});
describe('setTheme', () => {
it('should default to value "OS"', () => {
expect(preferencesController.store.getState().theme).toStrictEqual('os');
});
it('should set the setTheme property in state', () => {
preferencesController.setTheme('dark');
expect(preferencesController.store.getState().theme).toStrictEqual(
'dark',
);
});
});
describe('setUseCurrencyRateCheck', () => {
it('should default to false', () => {
expect(
preferencesController.store.getState().useCurrencyRateCheck,
).toStrictEqual(true);
});
it('should set the useCurrencyRateCheck property in state', () => {
preferencesController.setUseCurrencyRateCheck(false);
expect(
preferencesController.store.getState().useCurrencyRateCheck,
).toStrictEqual(false);
});
});
describe('setIncomingTransactionsPreferences', () => {
const addedNonTestNetworks = Object.keys(NETWORK_CONFIGURATION_DATA);
it('should have default value combined', () => {
const state = preferencesController.store.getState();
expect(state.incomingTransactionsPreferences).toStrictEqual({
[CHAIN_IDS.MAINNET]: true,
[CHAIN_IDS.LINEA_MAINNET]: true,
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[0]].chainId]: true,
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[1]].chainId]: true,
[CHAIN_IDS.GOERLI]: true,
[CHAIN_IDS.SEPOLIA]: true,
[CHAIN_IDS.LINEA_GOERLI]: true,
});
});
it('should update incomingTransactionsPreferences with given value set', () => {
preferencesController.setIncomingTransactionsPreferences(
[CHAIN_IDS.LINEA_MAINNET],
false,
);
const state = preferencesController.store.getState();
expect(state.incomingTransactionsPreferences).toStrictEqual({
[CHAIN_IDS.MAINNET]: true,
[CHAIN_IDS.LINEA_MAINNET]: false,
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[0]].chainId]: true,
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[1]].chainId]: true,
[CHAIN_IDS.GOERLI]: true,
[CHAIN_IDS.SEPOLIA]: true,
[CHAIN_IDS.LINEA_GOERLI]: true,
});
});
});
});