diff --git a/app/scripts/controllers/network/network-controller.test.ts b/app/scripts/controllers/network/network-controller.test.ts index c3360c140..37f8ac579 100644 --- a/app/scripts/controllers/network/network-controller.test.ts +++ b/app/scripts/controllers/network/network-controller.test.ts @@ -227,11 +227,9 @@ describe('NetworkController', () => { describe('destroy', () => { it('does not throw if called before the provider is initialized', async () => { - const controller = new NetworkController( - buildDefaultNetworkControllerOptions(), - ); - - expect(await controller.destroy()).toBeUndefined(); + await withController(async ({ controller }) => { + expect(await controller.destroy()).toBeUndefined(); + }); }); it('stops the block tracker for the currently selected network as long as the provider has been initialized', async () => { @@ -328,12 +326,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked or infuraIsUnblocked, depending on whether Infura is blocking requests', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -343,13 +337,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.initializeProvider(); @@ -505,12 +499,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -528,13 +518,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.initializeProvider(); @@ -547,12 +537,8 @@ describe('NetworkController', () => { }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -570,13 +556,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, count: 0, operation: async () => { @@ -1130,53 +1116,41 @@ describe('NetworkController', () => { }); it('does not emit infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider(); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await withController( - { messenger: restrictedMessenger }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider(); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.InfuraIsUnblocked, + count: 0, + operation: async () => { + await controller.lookupNetwork(); + }, + }); - const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.InfuraIsUnblocked, - count: 0, - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(await promiseForNoInfuraIsUnblockedEvents).toBeTruthy(); - }, - ); + expect(await promiseForNoInfuraIsUnblockedEvents).toBeTruthy(); + }); }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider(); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await withController( - { messenger: restrictedMessenger }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider(); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.InfuraIsBlocked, + count: 0, + operation: async () => { + await controller.lookupNetwork(); + }, + }); - const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.InfuraIsBlocked, - count: 0, - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(await promiseForNoInfuraIsBlockedEvents).toBeTruthy(); - }, - ); + expect(await promiseForNoInfuraIsBlockedEvents).toBeTruthy(); + }); }); }); @@ -1214,13 +1188,9 @@ describe('NetworkController', () => { }); it('does not emit infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( /* @ts-expect-error We are intentionally not including a chainId in the provider config. */ { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -1228,14 +1198,14 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); await controller.initializeProvider(); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, operation: async () => { @@ -1249,13 +1219,9 @@ describe('NetworkController', () => { }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( /* @ts-expect-error We are intentionally not including a chainId in the provider config. */ { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -1263,14 +1229,14 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); await controller.initializeProvider(); const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, count: 0, operation: async () => { @@ -1482,12 +1448,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -1497,7 +1459,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -1518,7 +1480,7 @@ describe('NetworkController', () => { }); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.lookupNetwork(); @@ -1698,12 +1660,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -1713,7 +1671,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -1732,7 +1690,7 @@ describe('NetworkController', () => { }); const infuraIsBlocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, operation: async () => { await controller.lookupNetwork(); @@ -1745,12 +1703,8 @@ describe('NetworkController', () => { }); it('does not emit infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -1760,7 +1714,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -1780,7 +1734,7 @@ describe('NetworkController', () => { const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, operation: async () => { @@ -2067,61 +2021,40 @@ describe('NetworkController', () => { }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - - await withController( - { - messenger: restrictedMessenger, - state: { - provider: { - type: networkType, - // NOTE: This doesn't need to match the logical chain ID of - // the network selected, it just needs to exist - chainId: '0x9999999', + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider([ + { + request: { + method: 'eth_getBlockByNumber', }, + error: GENERIC_JSON_RPC_ERROR, }, - }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider([ - { - request: { - method: 'eth_getBlockByNumber', - }, - error: GENERIC_JSON_RPC_ERROR, - }, - ]); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await withoutCallingLookupNetwork({ - controller, - operation: async () => { - await controller.initializeProvider(); - }, - }); + ]); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + await withoutCallingLookupNetwork({ + controller, + operation: async () => { + await controller.initializeProvider(); + }, + }); - const promiseForNoInfuraIsBlockedEvents = - waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.InfuraIsBlocked, - count: 0, - operation: async () => { - await controller.lookupNetwork(); - }, - }); + const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.InfuraIsBlocked, + count: 0, + operation: async () => { + await controller.lookupNetwork(); + }, + }); - expect(await promiseForNoInfuraIsBlockedEvents).toBeTruthy(); - }, - ); + expect(await promiseForNoInfuraIsBlockedEvents).toBeTruthy(); + }); }); it('does not emit infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -2131,7 +2064,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -2151,7 +2084,7 @@ describe('NetworkController', () => { const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, operation: async () => { @@ -2471,12 +2404,8 @@ describe('NetworkController', () => { ); } - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -2487,7 +2416,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [ buildFakeProvider([ { @@ -2536,12 +2465,12 @@ describe('NetworkController', () => { }); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, }); const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, }); @@ -2767,12 +2696,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -2781,7 +2706,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -2808,7 +2733,7 @@ describe('NetworkController', () => { }); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.lookupNetwork(); @@ -3098,12 +3023,8 @@ describe('NetworkController', () => { }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -3112,7 +3033,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -3137,7 +3058,7 @@ describe('NetworkController', () => { }); const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, count: 0, operation: async () => { @@ -3151,12 +3072,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -3165,7 +3082,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -3190,7 +3107,7 @@ describe('NetworkController', () => { }); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.lookupNetwork(); @@ -3410,12 +3327,8 @@ describe('NetworkController', () => { }); it('does not emit infuraIsBlocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -3424,7 +3337,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -3449,7 +3362,7 @@ describe('NetworkController', () => { }); const promiseForNoInfuraIsBlockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, count: 0, operation: async () => { @@ -3463,12 +3376,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -3477,7 +3386,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -3502,7 +3411,7 @@ describe('NetworkController', () => { }); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.lookupNetwork(); @@ -3833,12 +3742,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked, not infuraIsUnblocked, if the second network is blocked, even if the first one is not', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -3849,7 +3754,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [ buildFakeProvider([ { @@ -3908,12 +3813,12 @@ describe('NetworkController', () => { }); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, }); const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, }); @@ -4247,12 +4152,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked, not infuraIsUnblocked, if the second network is blocked, even if the first one is not', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -4263,7 +4164,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [ buildFakeProvider([ { @@ -4322,12 +4223,12 @@ describe('NetworkController', () => { }); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, }); const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, }); @@ -4430,12 +4331,8 @@ describe('NetworkController', () => { }); it('emits networkWillChange before making any changes to the network status', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -4453,7 +4350,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [ buildFakeProvider([ { @@ -4500,7 +4397,7 @@ describe('NetworkController', () => { expect(initialNetworkStatus).toBe('available'); const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: () => { // Intentionally not awaited because we're checking state @@ -4795,12 +4692,8 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { networkConfigurations: { testNetworkConfiguration: { @@ -4812,7 +4705,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient() @@ -4824,7 +4717,7 @@ describe('NetworkController', () => { .mockReturnValue(fakeNetworkClient); const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkDidChange, operation: async () => { await controller.setActiveNetwork('testNetworkConfiguration'); @@ -4837,12 +4730,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { networkConfigurations: { testNetworkConfiguration: { @@ -4854,7 +4743,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient() @@ -4866,7 +4755,7 @@ describe('NetworkController', () => { .mockReturnValue(fakeNetworkClient); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.setActiveNetwork('testNetworkConfiguration'); @@ -5017,29 +4906,23 @@ describe('NetworkController', () => { }); it('emits networkWillChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider(); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await withController( - { messenger: restrictedMessenger }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider(); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + const networkWillChange = await waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.NetworkWillChange, + operation: () => { + // Intentionally not awaited because we're capturing an event + // emitted partway through the operation + controller.setProviderType(networkType); + }, + }); - const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.NetworkWillChange, - operation: () => { - // Intentionally not awaited because we're capturing an event - // emitted partway through the operation - controller.setProviderType(networkType); - }, - }); - - expect(networkWillChange).toBeTruthy(); - }, - ); + expect(networkWillChange).toBeTruthy(); + }); }); it('resets the network status to "unknown" before emitting networkDidChange', async () => { @@ -5286,63 +5169,50 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider(); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await withController( - { messenger: restrictedMessenger }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider(); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + const networkDidChange = await waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.NetworkDidChange, + operation: async () => { + await controller.setProviderType(networkType); + }, + }); - const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.NetworkDidChange, - operation: async () => { - await controller.setProviderType(networkType); - }, - }); - - expect(networkDidChange).toBeTruthy(); - }, - ); + expect(networkDidChange).toBeTruthy(); + }); }); it('emits infuraIsBlocked or infuraIsUnblocked, depending on whether Infura is blocking requests', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - - await withController( - { messenger: restrictedMessenger }, - async ({ controller }) => { - const fakeProvider = buildFakeProvider([ - { - request: { - method: 'eth_getBlockByNumber', - }, - error: BLOCKED_INFURA_JSON_RPC_ERROR, + await withController(async ({ controller, messenger }) => { + const fakeProvider = buildFakeProvider([ + { + request: { + method: 'eth_getBlockByNumber', }, - ]); - const fakeNetworkClient = buildFakeClient(fakeProvider); - mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - const promiseForNoInfuraIsUnblockedEvents = - waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.InfuraIsUnblocked, - count: 0, - }); - const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, - eventType: NetworkControllerEventType.InfuraIsBlocked, - }); + error: BLOCKED_INFURA_JSON_RPC_ERROR, + }, + ]); + const fakeNetworkClient = buildFakeClient(fakeProvider); + mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); + const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.InfuraIsUnblocked, + count: 0, + }); + const promiseForInfuraIsBlocked = waitForPublishedEvents({ + messenger, + eventType: NetworkControllerEventType.InfuraIsBlocked, + }); - await controller.setProviderType(networkType); + await controller.setProviderType(networkType); - expect(await promiseForNoInfuraIsUnblockedEvents).toBeTruthy(); - expect(await promiseForInfuraIsBlocked).toBeTruthy(); - }, - ); + expect(await promiseForNoInfuraIsUnblockedEvents).toBeTruthy(); + expect(await promiseForInfuraIsBlocked).toBeTruthy(); + }); }); it('determines the status of the network, storing it in state', async () => { @@ -5436,12 +5306,8 @@ describe('NetworkController', () => { for (const { networkType } of INFURA_NETWORKS) { describe(`when the type in the provider configuration is "${networkType}"`, () => { it('emits networkWillChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -5451,13 +5317,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: () => { // Intentionally not awaited because we want to capture an @@ -5644,12 +5510,8 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -5659,13 +5521,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkDidChange, operation: async () => { await controller.resetConnection(); @@ -5678,12 +5540,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked or infuraIsUnblocked, depending on whether Infura is blocking requests', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -5693,7 +5551,7 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider([ { request: { @@ -5706,12 +5564,12 @@ describe('NetworkController', () => { mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, }); const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, }); @@ -5807,12 +5665,8 @@ describe('NetworkController', () => { describe(`when the type in the provider configuration is "rpc"`, () => { it('emits networkWillChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -5821,13 +5675,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: () => { // Intentionally not awaited because we're capturing an event @@ -6012,12 +5866,8 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -6026,13 +5876,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkDidChange, operation: async () => { await controller.resetConnection(); @@ -6045,12 +5895,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -6059,13 +5905,13 @@ describe('NetworkController', () => { }, }, }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.resetConnection(); @@ -6245,12 +6091,8 @@ describe('NetworkController', () => { }); it('emits networkWillChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -6269,7 +6111,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [buildFakeProvider(), buildFakeProvider()]; const fakeNetworkClients = [ buildFakeClient(fakeProviders[0]), @@ -6294,7 +6136,7 @@ describe('NetworkController', () => { controller, operation: async () => { const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: async () => { await controller.rollbackToPreviousProvider(); @@ -6623,12 +6465,8 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -6647,7 +6485,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [buildFakeProvider(), buildFakeProvider()]; const fakeNetworkClients = [ buildFakeClient(fakeProviders[0]), @@ -6673,7 +6511,7 @@ describe('NetworkController', () => { controller, operation: async () => { const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkDidChange, operation: async () => { await controller.rollbackToPreviousProvider(); @@ -6688,12 +6526,8 @@ describe('NetworkController', () => { }); it('emits infuraIsBlocked or infuraIsUnblocked, depending on whether Infura is blocking requests for the previous network', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: networkType, @@ -6712,7 +6546,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [ buildFakeProvider(), buildFakeProvider([ @@ -6744,12 +6578,12 @@ describe('NetworkController', () => { await controller.setActiveNetwork('testNetworkConfiguration'); const promiseForNoInfuraIsUnblockedEvents = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, count: 0, }); const promiseForInfuraIsBlocked = waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsBlocked, }); @@ -7005,12 +6839,8 @@ describe('NetworkController', () => { }); it('emits networkWillChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -7020,7 +6850,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [buildFakeProvider(), buildFakeProvider()]; const fakeNetworkClients = [ buildFakeClient(fakeProviders[0]), @@ -7045,7 +6875,7 @@ describe('NetworkController', () => { controller, operation: async () => { const networkWillChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: async () => { await controller.rollbackToPreviousProvider(); @@ -7332,12 +7162,8 @@ describe('NetworkController', () => { }); it('emits networkDidChange', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -7347,7 +7173,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [buildFakeProvider(), buildFakeProvider()]; const fakeNetworkClients = [ buildFakeClient(fakeProviders[0]), @@ -7372,7 +7198,7 @@ describe('NetworkController', () => { controller, operation: async () => { const networkDidChange = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.NetworkDidChange, operation: async () => { await controller.rollbackToPreviousProvider(); @@ -7387,12 +7213,8 @@ describe('NetworkController', () => { }); it('emits infuraIsUnblocked', async () => { - const { unrestrictedMessenger, restrictedMessenger } = - buildMessengerGroup(); - await withController( { - messenger: restrictedMessenger, state: { provider: { type: 'rpc', @@ -7402,7 +7224,7 @@ describe('NetworkController', () => { }, infuraProjectId: 'some-infura-project-id', }, - async ({ controller }) => { + async ({ controller, messenger }) => { const fakeProviders = [buildFakeProvider(), buildFakeProvider()]; const fakeNetworkClients = [ buildFakeClient(fakeProviders[0]), @@ -7427,7 +7249,7 @@ describe('NetworkController', () => { controller, operation: async () => { const infuraIsUnblocked = await waitForPublishedEvents({ - messenger: unrestrictedMessenger, + messenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, operation: async () => { await controller.rollbackToPreviousProvider(); @@ -8239,18 +8061,22 @@ function mockCreateNetworkClient() { } /** - * Builds the set of controller messengers that recognizes the events that - * NetworkController emits: one designed to be used directly by - * NetworkController, and one designed to be used in tests. + * Builds a controller messenger. * * @returns The controller messenger. */ -function buildMessengerGroup() { - const unrestrictedMessenger = new ControllerMessenger< - never, - NetworkControllerEvent - >(); - const restrictedMessenger = unrestrictedMessenger.getRestricted< +function buildMessenger() { + return new ControllerMessenger(); +} + +/** + * Build a restricted controller messenger for the network controller. + * + * @param messenger - A controller messenger. + * @returns The network controller restricted messenger. + */ +function buildNetworkControllerMessenger(messenger = buildMessenger()) { + return messenger.getRestricted< 'NetworkController', never, NetworkControllerEventType @@ -8263,23 +8089,6 @@ function buildMessengerGroup() { NetworkControllerEventType.InfuraIsUnblocked, ], }); - return { unrestrictedMessenger, restrictedMessenger }; -} - -/** - * Despite the signature of its constructor, NetworkController must take an - * Infura project ID. The object that this function returns is mixed into the - * options first when a NetworkController is instantiated in tests. - * - * @returns The controller options. - */ -function buildDefaultNetworkControllerOptions() { - const { restrictedMessenger } = buildMessengerGroup(); - return { - messenger: restrictedMessenger, - infuraProjectId: DEFAULT_INFURA_PROJECT_ID, - trackMetaMetricsEvent: jest.fn(), - }; } /** @@ -8289,6 +8098,7 @@ type WithControllerCallback = ({ controller, }: { controller: NetworkController; + messenger: ControllerMessenger; }) => Promise | ReturnValue; /** @@ -8301,7 +8111,10 @@ type WithControllerOptions = Partial; */ type WithControllerArgs = | [WithControllerCallback] - | [WithControllerOptions, WithControllerCallback]; + | [ + Omit, + WithControllerCallback, + ]; /** * Builds a controller based on the given options, and calls the given function @@ -8318,13 +8131,16 @@ async function withController( ): Promise { const [givenNetworkControllerOptions, fn] = args.length === 2 ? args : [{}, args[0]]; - const networkControllerOptions = { - ...buildDefaultNetworkControllerOptions(), + const messenger = buildMessenger(); + const restrictedMessenger = buildNetworkControllerMessenger(messenger); + const controller = new NetworkController({ + infuraProjectId: DEFAULT_INFURA_PROJECT_ID, + messenger: restrictedMessenger, + trackMetaMetricsEvent: jest.fn(), ...givenNetworkControllerOptions, - }; - const controller = new NetworkController(networkControllerOptions); + }); try { - return await fn({ controller }); + return await fn({ controller, messenger }); } finally { await controller.destroy(); }