From ebd7537ef25105ed33ee00773e5158c693c4e279 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 16 Jan 2023 16:03:57 -0330 Subject: [PATCH] Setup network controller mocks per-test The network controller unit test network mocks are now setup for each test. This makes modifying network behavior on a per-test basis easier, and makes it more clear which test relies upon which mocks. --- .../network/network-controller.test.js | 63 ++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/app/scripts/controllers/network/network-controller.test.js b/app/scripts/controllers/network/network-controller.test.js index 4a6dc70913bb..4c4256c46041 100644 --- a/app/scripts/controllers/network/network-controller.test.js +++ b/app/scripts/controllers/network/network-controller.test.js @@ -1,6 +1,9 @@ import nock from 'nock'; import { deferredPromise } from '../../lib/util'; -import { NETWORK_TO_NAME_MAP } from '../../../../shared/constants/network'; +import { + NETWORK_TO_NAME_MAP, + NETWORK_TYPES, +} from '../../../../shared/constants/network'; import NetworkController, { NETWORK_EVENTS } from './network-controller'; const getNetworkDisplayName = (key) => NETWORK_TO_NAME_MAP[key]; @@ -83,26 +86,32 @@ async function withController(...args) { } } +/** + * Setup Nock mocks for the block tracker. + * + * @param {object} options - Options. + * @param {NETWORK_TYPES} options.networkType - The network type to mock. + * @param {object} options.block - The mock block to return. + */ +function setupMockRpcBlockResponses({ + networkType = NETWORK_TYPES.RPC, + block = BLOCK, +} = {}) { + if (networkType === NETWORK_TYPES.RPC) { + nock('http://localhost:8545') + .persist() + .post(/.*/u) + .reply(200, () => JSON.stringify(constructSuccessfulRpcResponse(block))); + } else { + nock(`https://${networkType}.infura.io`) + .persist() + .post(`/v3/${defaultControllerOptions.infuraProjectId}`) + .reply(200, () => JSON.stringify(constructSuccessfulRpcResponse(block))); + } +} + describe('NetworkController', () => { describe('controller', () => { - let latestBlock; - - beforeEach(() => { - latestBlock = BLOCK; - nock('http://localhost:8545') - .persist() - .post(/.*/u) - .reply(200, () => - JSON.stringify(constructSuccessfulRpcResponse(latestBlock)), - ); - nock('https://mainnet.infura.io') - .persist() - .post('/v3/foo') - .reply(200, () => - JSON.stringify(constructSuccessfulRpcResponse(latestBlock)), - ); - }); - afterEach(() => { nock.cleanAll(); }); @@ -110,6 +119,7 @@ describe('NetworkController', () => { describe('#provider', () => { it('provider should be updatable without reassignment', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); await controller.initializeProvider(); const providerProxy = controller.getProviderAndBlockTracker().provider; @@ -130,6 +140,7 @@ describe('NetworkController', () => { it('should stop the block tracker for the current selected network', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); await controller.initializeProvider(); const { blockTracker } = controller.getProviderAndBlockTracker(); // The block tracker starts running after a listener is attached @@ -157,6 +168,8 @@ describe('NetworkController', () => { describe('#setProviderType', () => { it('should update provider.type', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); + setupMockRpcBlockResponses({ networkType: NETWORK_TYPES.MAINNET }); await controller.initializeProvider(); controller.setProviderType('mainnet'); const { type } = controller.getProviderConfig(); @@ -166,6 +179,8 @@ describe('NetworkController', () => { it('should set the network to loading', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); + setupMockRpcBlockResponses({ networkType: NETWORK_TYPES.MAINNET }); await controller.initializeProvider(); controller.setProviderType('mainnet'); @@ -182,7 +197,7 @@ describe('NetworkController', () => { describe('#getEIP1559Compatibility', () => { it('should return false when baseFeePerGas is not in the block header', async () => { await withController(async ({ controller }) => { - latestBlock = PRE_1559_BLOCK; + setupMockRpcBlockResponses({ block: PRE_1559_BLOCK }); await controller.initializeProvider(); const supportsEIP1559 = await controller.getEIP1559Compatibility(); expect(supportsEIP1559).toStrictEqual(false); @@ -191,6 +206,7 @@ describe('NetworkController', () => { it('should return true when baseFeePerGas is in block header', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); await controller.initializeProvider(); const supportsEIP1559 = await controller.getEIP1559Compatibility(); expect(supportsEIP1559).toStrictEqual(true); @@ -199,6 +215,7 @@ describe('NetworkController', () => { it('should store EIP1559 support in state to reduce calls to _getLatestBlock', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); await controller.initializeProvider(); await controller.getEIP1559Compatibility(); const supportsEIP1559 = await controller.getEIP1559Compatibility(); @@ -208,12 +225,16 @@ describe('NetworkController', () => { it('should clear stored EIP1559 support when changing networks', async () => { await withController(async ({ controller }) => { + setupMockRpcBlockResponses(); + setupMockRpcBlockResponses({ + networkType: NETWORK_TYPES.MAINNET, + block: PRE_1559_BLOCK, + }); await controller.initializeProvider(); await controller.getEIP1559Compatibility(); expect(controller.networkDetails.getState().EIPS[1559]).toStrictEqual( true, ); - latestBlock = PRE_1559_BLOCK; await new Promise((resolve) => { controller.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => { resolve();