From 25b7016f06fafa9f49e83d262523a4afb18284d8 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Thu, 8 Jun 2023 18:00:18 -0600 Subject: [PATCH] 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. --- .../network/network-controller.test.ts | 161 +++++++++++++++--- 1 file changed, 137 insertions(+), 24 deletions(-) diff --git a/app/scripts/controllers/network/network-controller.test.ts b/app/scripts/controllers/network/network-controller.test.ts index a74a38fc2..724f36771 100644 --- a/app/scripts/controllers/network/network-controller.test.ts +++ b/app/scripts/controllers/network/network-controller.test.ts @@ -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 () => {