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

Sync NetworkController setProviderType tests (#19406)

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-14 09:23:36 -06:00 committed by GitHub
parent 9c91db5d31
commit 312dadf91f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2455,7 +2455,7 @@ describe('NetworkController', () => {
ticker, ticker,
blockExplorerUrl, blockExplorerUrl,
} of INFURA_NETWORKS) { } of INFURA_NETWORKS) {
describe(`given a type of "${networkType}"`, () => { describe(`given a network type of "${networkType}"`, () => {
refreshNetworkTests({ refreshNetworkTests({
expectedProviderConfig: buildProviderConfig({ expectedProviderConfig: buildProviderConfig({
type: networkType, type: networkType,
@ -2464,53 +2464,113 @@ describe('NetworkController', () => {
await controller.setProviderType(networkType); await controller.setProviderType(networkType);
}, },
}); });
});
it(`overwrites the provider configuration using type: "${networkType}", chainId: "${chainId}", ticker "${ticker}", and blockExplorerUrl "${blockExplorerUrl}", clearing rpcUrl and nickname`, async () => { it(`overwrites the provider configuration using a predetermined chainId, ticker, and blockExplorerUrl for "${networkType}", clearing id, rpcUrl, and nickname`, async () => {
await withController( await withController(
{ {
state: { state: {
providerConfig: { providerConfig: {
type: 'rpc', type: 'rpc',
rpcUrl: 'https://mock-rpc-url', rpcUrl: 'https://mock-rpc-url',
chainId: '0x1337', chainId: '0x1337',
nickname: 'test-chain', nickname: 'test-chain',
ticker: 'TEST', ticker: 'TEST',
rpcPrefs: { rpcPrefs: {
blockExplorerUrl: 'https://test-block-explorer.com', blockExplorerUrl: 'https://test-block-explorer.com',
},
}, },
}, },
}, },
async ({ controller }) => { },
const fakeProvider = buildFakeProvider(); async ({ controller }) => {
const fakeNetworkClient = buildFakeClient(fakeProvider); const fakeProvider = buildFakeProvider();
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
controller.setProviderType(networkType); await controller.setProviderType(networkType);
expect(controller.store.getState().providerConfig).toStrictEqual({ expect(controller.store.getState().providerConfig).toStrictEqual({
type: networkType, type: networkType,
rpcUrl: undefined, rpcUrl: undefined,
chainId, chainId,
ticker, ticker,
nickname: undefined, nickname: undefined,
rpcPrefs: { blockExplorerUrl }, rpcPrefs: { blockExplorerUrl },
id: undefined, id: undefined,
}); });
}, },
); );
});
}); });
} }
describe('given a type of "rpc"', () => { describe('given a network type of "rpc"', () => {
it('throws', async () => { it('throws because there is no way to switch to a custom RPC endpoint using this method', async () => {
await withController(async ({ controller }) => { await withController(
await expect(() => controller.setProviderType('rpc')).rejects.toThrow( {
new Error( state: {
providerConfig: {
type: NETWORK_TYPES.RPC,
rpcUrl: 'http://somethingexisting.com',
chainId: toHex(99999),
ticker: 'something existing',
nickname: 'something existing',
},
},
},
async ({ controller }) => {
await expect(() =>
controller.setProviderType(NETWORK_TYPES.RPC),
).rejects.toThrow(
'NetworkController - cannot call "setProviderType" with type "rpc". Use "setActiveNetwork"', 'NetworkController - cannot call "setProviderType" with type "rpc". Use "setActiveNetwork"',
), );
); },
);
});
it("doesn't set a provider", async () => {
await withController(async ({ controller }) => {
const fakeProvider = buildFakeProvider();
const fakeNetworkClient = buildFakeClient(fakeProvider);
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
try {
await controller.setProviderType(NETWORK_TYPES.RPC);
} catch {
// catch the rejection (it is tested above)
}
expect(createNetworkClientMock).not.toHaveBeenCalled();
expect(controller.getProviderAndBlockTracker().provider).toBeNull();
});
});
it('does not update networkDetails.EIPS in state', async () => {
await withController(async ({ controller }) => {
const fakeProvider = buildFakeProvider([
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: {
baseFeePerGas: '0x1',
},
},
},
]);
const fakeNetworkClient = buildFakeClient(fakeProvider);
createNetworkClientMock.mockReturnValue(fakeNetworkClient);
try {
await controller.setProviderType(NETWORK_TYPES.RPC);
} catch {
// catch the rejection (it is tested above)
}
expect(
controller.store.getState().networkDetails.EIPS[1559],
).toBeUndefined();
}); });
}); });
}); });
@ -2519,9 +2579,9 @@ describe('NetworkController', () => {
it('throws', async () => { it('throws', async () => {
await withController(async ({ controller }) => { await withController(async ({ controller }) => {
await expect(() => await expect(() =>
controller.setProviderType('sadlflaksdj'), controller.setProviderType('invalid-infura-network'),
).rejects.toThrow( ).rejects.toThrow(
new Error('Unknown Infura provider type "sadlflaksdj".'), new Error('Unknown Infura provider type "invalid-infura-network".'),
); );
}); });
}); });