mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Refactor: Use network mocks for network unit tests (#17126)
The network controller unit tests now use network mocks rather than mocking controller methods. This makes the tests less brittle, as they will no longer break when internal changes to the `_getLatestBlock` method are made.
This commit is contained in:
parent
4c3c4eebac
commit
61d8fbb1d4
@ -1,5 +1,5 @@
|
|||||||
import sinon from 'sinon';
|
|
||||||
import nock from 'nock';
|
import nock from 'nock';
|
||||||
|
import { deferredPromise } from '../../lib/util';
|
||||||
import { NETWORK_TO_NAME_MAP } from '../../../../shared/constants/network';
|
import { NETWORK_TO_NAME_MAP } from '../../../../shared/constants/network';
|
||||||
import NetworkController, { NETWORK_EVENTS } from './network-controller';
|
import NetworkController, { NETWORK_EVENTS } from './network-controller';
|
||||||
|
|
||||||
@ -19,9 +19,9 @@ function constructSuccessfulRpcResponse(result) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example block taken from Ethereum Mainnet
|
// Example block taken from Ethereum Mainnet that has been updated to match the
|
||||||
const BLOCK = {
|
// pre-EIP-1559 format (i.e. it doesn't have the `baseFeePerGas` property).
|
||||||
baseFeePerGas: '0x63c498a46',
|
const PRE_1559_BLOCK = {
|
||||||
difficulty: '0x0',
|
difficulty: '0x0',
|
||||||
extraData: '0x',
|
extraData: '0x',
|
||||||
gasLimit: '0x1c9c380',
|
gasLimit: '0x1c9c380',
|
||||||
@ -52,18 +52,21 @@ const BLOCK = {
|
|||||||
'0x98bbdfbe1074bc3aa72a77a281f16d6ba7e723d68f15937d80954fb34d323369',
|
'0x98bbdfbe1074bc3aa72a77a281f16d6ba7e723d68f15937d80954fb34d323369',
|
||||||
uncles: [],
|
uncles: [],
|
||||||
};
|
};
|
||||||
|
// Example block taken from Ethereum Mainnet post-EIP-1559
|
||||||
|
const BLOCK = {
|
||||||
|
...PRE_1559_BLOCK,
|
||||||
|
baseFeePerGas: '0x63c498a46',
|
||||||
|
};
|
||||||
|
|
||||||
describe('NetworkController', () => {
|
describe('NetworkController', () => {
|
||||||
describe('controller', () => {
|
describe('controller', () => {
|
||||||
let networkController;
|
let networkController;
|
||||||
let getLatestBlockStub;
|
|
||||||
let setProviderTypeAndWait;
|
let setProviderTypeAndWait;
|
||||||
|
let latestBlock;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
latestBlock = BLOCK;
|
||||||
networkController = new NetworkController({ infuraProjectId: 'foo' });
|
networkController = new NetworkController({ infuraProjectId: 'foo' });
|
||||||
getLatestBlockStub = sinon
|
|
||||||
.stub(networkController, '_getLatestBlock')
|
|
||||||
.callsFake(() => Promise.resolve({}));
|
|
||||||
setProviderTypeAndWait = () =>
|
setProviderTypeAndWait = () =>
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
networkController.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
networkController.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
||||||
@ -71,10 +74,21 @@ describe('NetworkController', () => {
|
|||||||
});
|
});
|
||||||
networkController.setProviderType('mainnet');
|
networkController.setProviderType('mainnet');
|
||||||
});
|
});
|
||||||
|
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(() => {
|
afterEach(() => {
|
||||||
getLatestBlockStub.reset();
|
|
||||||
networkController.destroy();
|
networkController.destroy();
|
||||||
nock.cleanAll();
|
nock.cleanAll();
|
||||||
});
|
});
|
||||||
@ -98,12 +112,6 @@ describe('NetworkController', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should stop the block tracker for the current selected network', async () => {
|
it('should stop the block tracker for the current selected network', async () => {
|
||||||
nock('http://localhost:8545')
|
|
||||||
.persist()
|
|
||||||
.post(/.*/u)
|
|
||||||
.reply(200, () =>
|
|
||||||
JSON.stringify(constructSuccessfulRpcResponse(BLOCK)),
|
|
||||||
);
|
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
const { blockTracker } = networkController.getProviderAndBlockTracker();
|
const { blockTracker } = networkController.getProviderAndBlockTracker();
|
||||||
// The block tracker starts running after a listener is attached
|
// The block tracker starts running after a listener is attached
|
||||||
@ -136,16 +144,19 @@ describe('NetworkController', () => {
|
|||||||
it('should set the network to loading', async () => {
|
it('should set the network to loading', async () => {
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
|
|
||||||
const spy = sinon.spy(networkController, '_setNetworkState');
|
|
||||||
networkController.setProviderType('mainnet');
|
networkController.setProviderType('mainnet');
|
||||||
|
const { promise: networkIdChanged, resolve } = deferredPromise();
|
||||||
|
networkController.networkStore.subscribe(resolve);
|
||||||
|
|
||||||
expect(spy.callCount).toStrictEqual(1);
|
expect(networkController.networkStore.getState()).toBe('loading');
|
||||||
expect(spy.calledOnceWithExactly('loading')).toStrictEqual(true);
|
await networkIdChanged;
|
||||||
|
expect(networkController.networkStore.getState()).toBe('1');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#getEIP1559Compatibility', () => {
|
describe('#getEIP1559Compatibility', () => {
|
||||||
it('should return false when baseFeePerGas is not in the block header', async () => {
|
it('should return false when baseFeePerGas is not in the block header', async () => {
|
||||||
|
latestBlock = PRE_1559_BLOCK;
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
const supportsEIP1559 =
|
const supportsEIP1559 =
|
||||||
await networkController.getEIP1559Compatibility();
|
await networkController.getEIP1559Compatibility();
|
||||||
@ -154,9 +165,6 @@ describe('NetworkController', () => {
|
|||||||
|
|
||||||
it('should return true when baseFeePerGas is in block header', async () => {
|
it('should return true when baseFeePerGas is in block header', async () => {
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
getLatestBlockStub.callsFake(() =>
|
|
||||||
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
||||||
);
|
|
||||||
const supportsEIP1559 =
|
const supportsEIP1559 =
|
||||||
await networkController.getEIP1559Compatibility();
|
await networkController.getEIP1559Compatibility();
|
||||||
expect(supportsEIP1559).toStrictEqual(true);
|
expect(supportsEIP1559).toStrictEqual(true);
|
||||||
@ -164,27 +172,22 @@ describe('NetworkController', () => {
|
|||||||
|
|
||||||
it('should store EIP1559 support in state to reduce calls to _getLatestBlock', async () => {
|
it('should store EIP1559 support in state to reduce calls to _getLatestBlock', async () => {
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
getLatestBlockStub.callsFake(() =>
|
|
||||||
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
||||||
);
|
|
||||||
await networkController.getEIP1559Compatibility();
|
await networkController.getEIP1559Compatibility();
|
||||||
const supportsEIP1559 =
|
const supportsEIP1559 =
|
||||||
await networkController.getEIP1559Compatibility();
|
await networkController.getEIP1559Compatibility();
|
||||||
expect(getLatestBlockStub.calledOnce).toStrictEqual(true);
|
|
||||||
expect(supportsEIP1559).toStrictEqual(true);
|
expect(supportsEIP1559).toStrictEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should clear stored EIP1559 support when changing networks', async () => {
|
it('should clear stored EIP1559 support when changing networks', async () => {
|
||||||
await networkController.initializeProvider();
|
await networkController.initializeProvider();
|
||||||
getLatestBlockStub.callsFake(() =>
|
|
||||||
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
||||||
);
|
|
||||||
await networkController.getEIP1559Compatibility();
|
await networkController.getEIP1559Compatibility();
|
||||||
expect(
|
expect(
|
||||||
networkController.networkDetails.getState().EIPS[1559],
|
networkController.networkDetails.getState().EIPS[1559],
|
||||||
).toStrictEqual(true);
|
).toStrictEqual(true);
|
||||||
getLatestBlockStub.callsFake(() => Promise.resolve({}));
|
latestBlock = PRE_1559_BLOCK;
|
||||||
|
|
||||||
await setProviderTypeAndWait('mainnet');
|
await setProviderTypeAndWait('mainnet');
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
networkController.networkDetails.getState().EIPS[1559],
|
networkController.networkDetails.getState().EIPS[1559],
|
||||||
).toBeUndefined();
|
).toBeUndefined();
|
||||||
@ -192,7 +195,6 @@ describe('NetworkController', () => {
|
|||||||
expect(
|
expect(
|
||||||
networkController.networkDetails.getState().EIPS[1559],
|
networkController.networkDetails.getState().EIPS[1559],
|
||||||
).toStrictEqual(false);
|
).toStrictEqual(false);
|
||||||
expect(getLatestBlockStub.calledTwice).toStrictEqual(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user