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

Sync NetworkController setActiveNetwork tests w/ core (#19405)

This makes it easier to visually compare differences in the
NetworkController unit tests between core and this repo.
This commit is contained in:
Elliot Winkler 2023-06-08 18:00:18 -06:00 committed by GitHub
parent 84969c1b0e
commit 25b7016f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2238,6 +2238,7 @@ describe('NetworkController', () => {
chainId: toHex(111),
ticker: 'TEST',
nickname: 'something existing',
id: 'testNetworkConfigurationId',
rpcPrefs: undefined,
type: NETWORK_TYPES.RPC,
},
@ -2258,34 +2259,146 @@ describe('NetworkController', () => {
},
});
it('throws if the given networkConfigurationId does not match one in networkConfigurations', async () => {
await withController(
{
state: {
networkConfigurations: {
testNetworkConfigurationId: {
id: 'testNetworkConfigurationId',
rpcUrl: 'https://mock-rpc-url',
chainId: '0xtest',
ticker: 'TEST',
describe('if the given ID does not match a network configuration in networkConfigurations', () => {
it('throws', async () => {
await withController(
{
state: {
networkConfigurations: {
testNetworkConfigurationId: {
rpcUrl: 'https://mock-rpc-url',
chainId: toHex(111),
ticker: 'TEST',
id: 'testNetworkConfigurationId',
},
},
},
},
},
async ({ controller }) => {
const fakeProvider = buildFakeProvider();
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
async ({ controller }) => {
const fakeProvider = buildFakeProvider();
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await expect(() =>
controller.setActiveNetwork('invalid-network-configuration-id'),
).rejects.toThrow(
new Error(
'networkConfigurationId invalid-network-configuration-id does not match a configured networkConfiguration',
),
);
},
);
await expect(() =>
controller.setActiveNetwork('invalidNetworkConfigurationId'),
).rejects.toThrow(
new Error(
'networkConfigurationId invalidNetworkConfigurationId does not match a configured networkConfiguration',
),
);
},
);
});
});
describe('if the network config does not contain an RPC URL', () => {
it('throws', async () => {
await withController(
// @ts-expect-error RPC URL intentionally omitted
{
state: {
providerConfig: {
type: NETWORK_TYPES.RPC,
rpcUrl: 'https://mock-rpc-url',
chainId: toHex(111),
ticker: 'TEST',
nickname: 'something existing',
rpcPrefs: undefined,
},
networkConfigurations: {
testNetworkConfigurationId1: {
rpcUrl: 'https://mock-rpc-url',
chainId: toHex(111),
ticker: 'TEST',
nickname: 'something existing',
id: 'testNetworkConfigurationId1',
rpcPrefs: undefined,
},
testNetworkConfigurationId2: {
rpcUrl: undefined,
chainId: toHex(222),
ticker: 'something existing',
nickname: 'something existing',
id: 'testNetworkConfigurationId2',
rpcPrefs: undefined,
},
},
},
},
async ({ controller }) => {
const fakeProvider = buildFakeProvider();
const fakeNetworkClient = buildFakeClient(fakeProvider);
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
await expect(() =>
controller.setActiveNetwork('testNetworkConfigurationId2'),
).rejects.toThrow(
'rpcUrl must be provided for custom RPC endpoints',
);
expect(createNetworkClientMock).not.toHaveBeenCalled();
const { provider, blockTracker } =
controller.getProviderAndBlockTracker();
expect(provider).toBeNull();
expect(blockTracker).toBeNull();
},
);
});
});
describe('if the network config does not contain a chain ID', () => {
it('throws', async () => {
await withController(
// @ts-expect-error chain ID intentionally omitted
{
state: {
providerConfig: {
type: NETWORK_TYPES.RPC,
rpcUrl: 'https://mock-rpc-url',
chainId: toHex(111),
ticker: 'TEST',
nickname: 'something existing',
rpcPrefs: undefined,
},
networkConfigurations: {
testNetworkConfigurationId1: {
rpcUrl: 'https://mock-rpc-url',
chainId: toHex(111),
ticker: 'TEST',
nickname: 'something existing',
id: 'testNetworkConfigurationId1',
rpcPrefs: undefined,
},
testNetworkConfigurationId2: {
rpcUrl: 'http://somethingexisting.com',
chainId: undefined,
ticker: 'something existing',
nickname: 'something existing',
id: 'testNetworkConfigurationId2',
rpcPrefs: undefined,
},
},
},
},
async ({ controller }) => {
const fakeProvider = buildFakeProvider();
const fakeNetworkClient = buildFakeClient(fakeProvider);
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
await expect(() =>
controller.setActiveNetwork('testNetworkConfigurationId2'),
).rejects.toThrow(
'chainId must be provided for custom RPC endpoints',
);
expect(createNetworkClientMock).not.toHaveBeenCalled();
const { provider, blockTracker } =
controller.getProviderAndBlockTracker();
expect(provider).toBeNull();
expect(blockTracker).toBeNull();
},
);
});
});
it('overwrites the provider configuration given a networkConfigurationId that matches a configured networkConfiguration', async () => {