1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Fix #19950 - Use removable property to determine if network should be removable (#20220)

This commit is contained in:
David Walsh 2023-07-29 14:59:24 -05:00 committed by GitHub
parent 96e4b7bb9f
commit e6a4c63d51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 16 deletions

View File

@ -12,7 +12,7 @@ import {
setProviderType,
toggleNetworkMenu,
} from '../../../store/actions';
import { CHAIN_IDS, TEST_CHAINS } from '../../../../shared/constants/network';
import { TEST_CHAINS } from '../../../../shared/constants/network';
import {
getShowTestNetworks,
getCurrentChainId,
@ -52,12 +52,6 @@ import {
isLineaMainnetNetworkReleased,
} from '../../../ducks/metamask/metamask';
const UNREMOVABLE_CHAIN_IDS = [
CHAIN_IDS.MAINNET,
CHAIN_IDS.LINEA_MAINNET,
...TEST_CHAINS,
];
export const NetworkListMenu = ({ onClose }) => {
const t = useI18nContext();
@ -117,9 +111,7 @@ export const NetworkListMenu = ({ onClose }) => {
}
const isCurrentNetwork = currentNetwork.id === network.id;
const canDeleteNetwork =
!isCurrentNetwork && !UNREMOVABLE_CHAIN_IDS.includes(network.chainId);
const canDeleteNetwork = !isCurrentNetwork && network.removable;
return (
<NetworkListItem

View File

@ -1211,6 +1211,7 @@ export function getTestNetworks(state) {
providerType: NETWORK_TYPES.GOERLI,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.GOERLI],
id: NETWORK_TYPES.GOERLI,
removable: false,
},
{
chainId: CHAIN_IDS.SEPOLIA,
@ -1219,6 +1220,7 @@ export function getTestNetworks(state) {
providerType: NETWORK_TYPES.SEPOLIA,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.SEPOLIA],
id: NETWORK_TYPES.SEPOLIA,
removable: false,
},
{
chainId: CHAIN_IDS.LINEA_GOERLI,
@ -1230,11 +1232,12 @@ export function getTestNetworks(state) {
providerType: NETWORK_TYPES.LINEA_GOERLI,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_GOERLI],
id: NETWORK_TYPES.LINEA_GOERLI,
removable: false,
},
// Localhosts
...Object.values(networkConfigurations).filter(
({ chainId }) => chainId === CHAIN_IDS.LOCALHOST,
),
...Object.values(networkConfigurations)
.filter(({ chainId }) => chainId === CHAIN_IDS.LOCALHOST)
.map((network) => ({ ...network, removable: true })),
];
}
@ -1253,6 +1256,7 @@ export function getNonTestNetworks(state) {
providerType: NETWORK_TYPES.MAINNET,
ticker: CURRENCY_SYMBOLS.ETH,
id: NETWORK_TYPES.MAINNET,
removable: false,
},
{
chainId: CHAIN_IDS.LINEA_MAINNET,
@ -1264,11 +1268,12 @@ export function getNonTestNetworks(state) {
providerType: NETWORK_TYPES.LINEA_MAINNET,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_MAINNET],
id: NETWORK_TYPES.LINEA_MAINNET,
removable: false,
},
// Custom networks added by the user
...Object.values(networkConfigurations).filter(
({ chainId }) => ![CHAIN_IDS.LOCALHOST].includes(chainId),
),
...Object.values(networkConfigurations)
.filter(({ chainId }) => ![CHAIN_IDS.LOCALHOST].includes(chainId))
.map((network) => ({ ...network, removable: true })),
];
}

View File

@ -4,6 +4,7 @@ import { KeyringType } from '../../shared/constants/keyring';
import {
CHAIN_IDS,
LOCALHOST_DISPLAY_NAME,
NETWORK_TYPES,
} from '../../shared/constants/network';
import * as selectors from './selectors';
@ -314,6 +315,33 @@ describe('Selectors', () => {
const lastItem = networks.pop();
expect(lastItem.nickname.toLowerCase()).toContain('localhost');
});
it('properly assigns a network as removable', () => {
const networks = selectors.getAllNetworks({
metamask: {
preferences: {
showTestNetworks: true,
},
networkConfigurations: {
'some-config-name': {
chainId: CHAIN_IDS.LOCALHOST,
nickname: LOCALHOST_DISPLAY_NAME,
id: 'some-config-name',
},
},
},
});
const mainnet = networks.find(
(network) => network.id === NETWORK_TYPES.MAINNET,
);
expect(mainnet.removable).toBe(false);
const customNetwork = networks.find(
(network) => network.id === 'some-config-name',
);
expect(customNetwork.removable).toBe(true);
});
});
describe('#getCurrentNetwork', () => {