Skip to content

Commit

Permalink
Setup network controller mocks per-test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Gudahtt committed Jan 17, 2023
1 parent 714402c commit ebd7537
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions app/scripts/controllers/network/network-controller.test.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -83,33 +86,40 @@ 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();
});

describe('#provider', () => {
it('provider should be updatable without reassignment', async () => {
await withController(async ({ controller }) => {
setupMockRpcBlockResponses();
await controller.initializeProvider();
const providerProxy =
controller.getProviderAndBlockTracker().provider;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit ebd7537

Please sign in to comment.