mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +01:00
removeNetworkConfiguration validates given ID (#18650)
In the `core` version of NetworkController, `removeNetworkConfiguration` throws an error if the given network configuration ID doesn't match an existing network configuration. This commits adds the same check for consistency. It also makes some minor changes to the implementation and tests for `removeNetworkConfiguration` for consistency as well.
This commit is contained in:
parent
d362dbfacb
commit
a876eaba23
@ -7308,14 +7308,14 @@ describe('NetworkController', () => {
|
||||
});
|
||||
|
||||
describe('removeNetworkConfigurations', () => {
|
||||
it('should remove a network configuration', async () => {
|
||||
const networkConfigurationId = 'networkConfigurationId';
|
||||
it('removes a network configuration', async () => {
|
||||
const networkConfigurationId = 'testNetworkConfigurationId';
|
||||
await withController(
|
||||
{
|
||||
state: {
|
||||
networkConfigurations: {
|
||||
[networkConfigurationId]: {
|
||||
id: 'aaaaaa',
|
||||
id: networkConfigurationId,
|
||||
rpcUrl: 'https://test-rpc-url',
|
||||
ticker: 'old_rpc_ticker',
|
||||
nickname: 'old_rpc_chainName',
|
||||
@ -7326,25 +7326,44 @@ describe('NetworkController', () => {
|
||||
},
|
||||
},
|
||||
async ({ controller }) => {
|
||||
expect(
|
||||
Object.values(controller.store.getState().networkConfigurations),
|
||||
).toStrictEqual([
|
||||
{
|
||||
id: 'aaaaaa',
|
||||
rpcUrl: 'https://test-rpc-url',
|
||||
ticker: 'old_rpc_ticker',
|
||||
nickname: 'old_rpc_chainName',
|
||||
rpcPrefs: { blockExplorerUrl: 'testchainscan.io' },
|
||||
chainId: '0x1',
|
||||
},
|
||||
]);
|
||||
controller.removeNetworkConfiguration(networkConfigurationId);
|
||||
|
||||
expect(
|
||||
controller.store.getState().networkConfigurations,
|
||||
).toStrictEqual({});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('throws if the networkConfigurationId it is passed does not correspond to a network configuration in state', async () => {
|
||||
const testNetworkConfigurationId = 'testNetworkConfigurationId';
|
||||
const invalidNetworkConfigurationId = 'invalidNetworkConfigurationId';
|
||||
await withController(
|
||||
{
|
||||
state: {
|
||||
networkConfigurations: {
|
||||
[testNetworkConfigurationId]: {
|
||||
rpcUrl: 'https://rpc-url.com',
|
||||
ticker: 'old_rpc_ticker',
|
||||
nickname: 'old_rpc_nickname',
|
||||
rpcPrefs: { blockExplorerUrl: 'testchainscan.io' },
|
||||
chainId: '0x1',
|
||||
id: testNetworkConfigurationId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
async ({ controller }) => {
|
||||
expect(() =>
|
||||
controller.removeNetworkConfiguration(
|
||||
invalidNetworkConfigurationId,
|
||||
),
|
||||
).toThrow(
|
||||
`networkConfigurationId ${invalidNetworkConfigurationId} does not match a configured networkConfiguration`,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1141,9 +1141,12 @@ export class NetworkController extends EventEmitter {
|
||||
* @param networkConfigurationId - The unique id for the network configuration
|
||||
* to remove.
|
||||
*/
|
||||
removeNetworkConfiguration(
|
||||
networkConfigurationId: NetworkConfigurationId,
|
||||
): void {
|
||||
removeNetworkConfiguration(networkConfigurationId: NetworkConfigurationId) {
|
||||
if (!this.store.getState().networkConfigurations[networkConfigurationId]) {
|
||||
throw new Error(
|
||||
`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`,
|
||||
);
|
||||
}
|
||||
const networkConfigurations = {
|
||||
...this.store.getState().networkConfigurations,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user