Jestify app/scripts/controller/network/**/*.test.js (#12985)

feature/default_network_editable
Thomas Huang 3 years ago committed by GitHub
parent bdc7e1df57
commit c9cb2ae1a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .eslintrc.js
  2. 1
      .mocharc.js
  3. 89
      app/scripts/controllers/network/network-controller.test.js
  4. 56
      app/scripts/controllers/network/pending-middleware.test.js
  5. 13
      app/scripts/controllers/network/util.test.js
  6. 1
      jest.config.js

@ -161,6 +161,7 @@ module.exports = {
'app/scripts/lib/**/*.test.js', 'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js', 'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js', 'app/scripts/platforms/*.test.js',
'app/scripts/controllers/network/**/*.test.js',
'app/scripts/controllers/permissions/*.test.js', 'app/scripts/controllers/permissions/*.test.js',
], ],
extends: ['@metamask/eslint-config-mocha'], extends: ['@metamask/eslint-config-mocha'],
@ -187,6 +188,7 @@ module.exports = {
'app/scripts/lib/**/*.test.js', 'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js', 'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js', 'app/scripts/platforms/*.test.js',
'app/scripts/controllers/network/**/*.test.js',
'app/scripts/controllers/permissions/*.test.js', 'app/scripts/controllers/permissions/*.test.js',
], ],
extends: ['@metamask/eslint-config-jest'], extends: ['@metamask/eslint-config-jest'],

@ -5,6 +5,7 @@ module.exports = {
'./app/scripts/lib/**/*.test.js', './app/scripts/lib/**/*.test.js',
'./app/scripts/migrations/*.test.js', './app/scripts/migrations/*.test.js',
'./app/scripts/platforms/*.test.js', './app/scripts/platforms/*.test.js',
'./app/scripts/controllers/network/**/*.test.js',
'./app/scripts/controllers/permissions/*.test.js', './app/scripts/controllers/permissions/*.test.js',
], ],
recursive: true, recursive: true,

@ -1,10 +1,9 @@
import { strict as assert } from 'assert';
import sinon from 'sinon'; import sinon from 'sinon';
import { getNetworkDisplayName } from './util'; import { getNetworkDisplayName } from './util';
import NetworkController, { NETWORK_EVENTS } from './network'; import NetworkController, { NETWORK_EVENTS } from './network';
describe('NetworkController', function () { describe('NetworkController', () => {
describe('controller', function () { describe('controller', () => {
let networkController; let networkController;
let getLatestBlockStub; let getLatestBlockStub;
let setProviderTypeAndWait; let setProviderTypeAndWait;
@ -13,7 +12,7 @@ describe('NetworkController', function () {
getAccounts: noop, getAccounts: noop,
}; };
beforeEach(function () { beforeEach(() => {
networkController = new NetworkController(); networkController = new NetworkController();
getLatestBlockStub = sinon getLatestBlockStub = sinon
.stub(networkController, 'getLatestBlock') .stub(networkController, 'getLatestBlock')
@ -28,118 +27,108 @@ describe('NetworkController', function () {
}); });
}); });
afterEach(function () { afterEach(() => {
getLatestBlockStub.reset(); getLatestBlockStub.reset();
}); });
describe('#provider', function () { describe('#provider', () => {
it('provider should be updatable without reassignment', function () { it('provider should be updatable without reassignment', () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
const providerProxy = networkController.getProviderAndBlockTracker() const providerProxy = networkController.getProviderAndBlockTracker()
.provider; .provider;
assert.equal(providerProxy.test, undefined); expect(providerProxy.test).toBeUndefined();
providerProxy.setTarget({ test: true }); providerProxy.setTarget({ test: true });
assert.equal(providerProxy.test, true); expect(providerProxy.test).toStrictEqual(true);
}); });
}); });
describe('#getNetworkState', function () { describe('#getNetworkState', () => {
it('should return "loading" when new', function () { it('should return "loading" when new', () => {
const networkState = networkController.getNetworkState(); const networkState = networkController.getNetworkState();
assert.equal(networkState, 'loading', 'network is loading'); expect(networkState).toStrictEqual('loading');
}); });
}); });
describe('#setNetworkState', function () { describe('#setNetworkState', () => {
it('should update the network', function () { it('should update the network', () => {
networkController.setNetworkState('1'); networkController.setNetworkState('1');
const networkState = networkController.getNetworkState(); const networkState = networkController.getNetworkState();
assert.equal(networkState, '1', 'network is 1'); expect(networkState).toStrictEqual('1');
}); });
}); });
describe('#setProviderType', function () { describe('#setProviderType', () => {
it('should update provider.type', function () { it('should update provider.type', () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
networkController.setProviderType('mainnet'); networkController.setProviderType('mainnet');
const { type } = networkController.getProviderConfig(); const { type } = networkController.getProviderConfig();
assert.equal(type, 'mainnet', 'provider type is updated'); expect(type).toStrictEqual('mainnet');
}); });
it('should set the network to loading', function () { it('should set the network to loading', () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
const spy = sinon.spy(networkController, 'setNetworkState'); const spy = sinon.spy(networkController, 'setNetworkState');
networkController.setProviderType('mainnet'); networkController.setProviderType('mainnet');
assert.equal( expect(spy.callCount).toStrictEqual(1);
spy.callCount, expect(spy.calledOnceWithExactly('loading')).toStrictEqual(true);
1,
'should have called setNetworkState 2 times',
);
assert.ok(
spy.calledOnceWithExactly('loading'),
'should have called with "loading" first',
);
}); });
}); });
describe('#getEIP1559Compatibility', function () { describe('#getEIP1559Compatibility', () => {
it('should return false when baseFeePerGas is not in the block header', async function () { it('should return false when baseFeePerGas is not in the block header', async () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
const supportsEIP1559 = await networkController.getEIP1559Compatibility(); const supportsEIP1559 = await networkController.getEIP1559Compatibility();
assert.equal(supportsEIP1559, false); expect(supportsEIP1559).toStrictEqual(false);
}); });
it('should return true when baseFeePerGas is in block header', async function () { it('should return true when baseFeePerGas is in block header', async () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
getLatestBlockStub.callsFake(() => getLatestBlockStub.callsFake(() =>
Promise.resolve({ baseFeePerGas: '0xa ' }), Promise.resolve({ baseFeePerGas: '0xa ' }),
); );
const supportsEIP1559 = await networkController.getEIP1559Compatibility(); const supportsEIP1559 = await networkController.getEIP1559Compatibility();
assert.equal(supportsEIP1559, true); expect(supportsEIP1559).toStrictEqual(true);
}); });
it('should store EIP1559 support in state to reduce calls to getLatestBlock', async function () { it('should store EIP1559 support in state to reduce calls to getLatestBlock', async () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
getLatestBlockStub.callsFake(() => getLatestBlockStub.callsFake(() =>
Promise.resolve({ baseFeePerGas: '0xa ' }), Promise.resolve({ baseFeePerGas: '0xa ' }),
); );
await networkController.getEIP1559Compatibility(); await networkController.getEIP1559Compatibility();
const supportsEIP1559 = await networkController.getEIP1559Compatibility(); const supportsEIP1559 = await networkController.getEIP1559Compatibility();
assert.equal(getLatestBlockStub.calledOnce, true); expect(getLatestBlockStub.calledOnce).toStrictEqual(true);
assert.equal(supportsEIP1559, true); expect(supportsEIP1559).toStrictEqual(true);
}); });
it('should clear stored EIP1559 support when changing networks', async function () { it('should clear stored EIP1559 support when changing networks', async () => {
networkController.initializeProvider(networkControllerProviderConfig); networkController.initializeProvider(networkControllerProviderConfig);
networkController.consoleThis = true; networkController.consoleThis = true;
getLatestBlockStub.callsFake(() => getLatestBlockStub.callsFake(() =>
Promise.resolve({ baseFeePerGas: '0xa ' }), Promise.resolve({ baseFeePerGas: '0xa ' }),
); );
await networkController.getEIP1559Compatibility(); await networkController.getEIP1559Compatibility();
assert.equal( expect(
networkController.networkDetails.getState().EIPS[1559], networkController.networkDetails.getState().EIPS[1559],
true, ).toStrictEqual(true);
);
getLatestBlockStub.callsFake(() => Promise.resolve({})); getLatestBlockStub.callsFake(() => Promise.resolve({}));
await setProviderTypeAndWait('mainnet'); await setProviderTypeAndWait('mainnet');
assert.equal( expect(
networkController.networkDetails.getState().EIPS[1559], networkController.networkDetails.getState().EIPS[1559],
undefined, ).toBeUndefined();
);
await networkController.getEIP1559Compatibility(); await networkController.getEIP1559Compatibility();
assert.equal( expect(
networkController.networkDetails.getState().EIPS[1559], networkController.networkDetails.getState().EIPS[1559],
false, ).toStrictEqual(false);
); expect(getLatestBlockStub.calledTwice).toStrictEqual(true);
assert.equal(getLatestBlockStub.calledTwice, true);
}); });
}); });
}); });
describe('utils', function () { describe('utils', () => {
it('getNetworkDisplayName should return the correct network name', function () { it('getNetworkDisplayName should return the correct network name', () => {
const tests = [ const tests = [
{ {
input: '3', input: '3',
@ -188,7 +177,7 @@ describe('NetworkController', function () {
]; ];
tests.forEach(({ input, expected }) => tests.forEach(({ input, expected }) =>
assert.equal(getNetworkDisplayName(input), expected), expect(getNetworkDisplayName(input)).toStrictEqual(expected),
); );
}); });
}); });

@ -1,4 +1,3 @@
import { strict as assert } from 'assert';
import { GAS_LIMITS } from '../../../../shared/constants/gas'; import { GAS_LIMITS } from '../../../../shared/constants/gas';
import { TRANSACTION_ENVELOPE_TYPES } from '../../../../shared/constants/transaction'; import { TRANSACTION_ENVELOPE_TYPES } from '../../../../shared/constants/transaction';
import { txMetaStub } from '../../../../test/stub/tx-meta-stub'; import { txMetaStub } from '../../../../test/stub/tx-meta-stub';
@ -7,25 +6,35 @@ import {
createPendingTxMiddleware, createPendingTxMiddleware,
} from './middleware/pending'; } from './middleware/pending';
describe('PendingNonceMiddleware', function () { describe('PendingNonceMiddleware', () => {
describe('#createPendingNonceMiddleware', function () { describe('#createPendingNonceMiddleware', () => {
const getPendingNonce = async () => '0x2'; const getPendingNonce = async () => '0x2';
const address = '0xF231D46dD78806E1DD93442cf33C7671f8538748'; const address = '0xF231D46dD78806E1DD93442cf33C7671f8538748';
const pendingNonceMiddleware = createPendingNonceMiddleware({ const pendingNonceMiddleware = createPendingNonceMiddleware({
getPendingNonce, getPendingNonce,
}); });
it('should call next if not a eth_getTransactionCount request', function (done) { it('should call next if not a eth_getTransactionCount request', () => {
const req = { method: 'eth_getBlockByNumber' }; const req = { method: 'eth_getBlockByNumber' };
const res = {}; const res = {};
pendingNonceMiddleware(req, res, () => done());
const next = jest.fn();
pendingNonceMiddleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
}); });
it('should call next if not a "pending" block request', function (done) {
it('should call next if not a "pending" block request', () => {
const req = { method: 'eth_getTransactionCount', params: [address] }; const req = { method: 'eth_getTransactionCount', params: [address] };
const res = {}; const res = {};
pendingNonceMiddleware(req, res, () => done());
const next = jest.fn();
pendingNonceMiddleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
}); });
it('should fill the result with a the "pending" nonce', function (done) {
it('should fill the result with a the "pending" nonce', () => {
const req = { const req = {
method: 'eth_getTransactionCount', method: 'eth_getTransactionCount',
params: [address, 'pending'], params: [address, 'pending'],
@ -35,17 +44,16 @@ describe('PendingNonceMiddleware', function () {
req, req,
res, res,
() => { () => {
done(new Error('should not have called next')); return new Error('should not have called next');
}, },
() => { () => {
assert(res.result === '0x2'); expect(res.result).toStrictEqual('0x2');
done();
}, },
); );
}); });
}); });
describe('#createPendingTxMiddleware', function () { describe('#createPendingTxMiddleware', () => {
let returnUndefined = true; let returnUndefined = true;
const getPendingTransactionByHash = () => const getPendingTransactionByHash = () =>
returnUndefined ? undefined : txMetaStub; returnUndefined ? undefined : txMetaStub;
@ -72,19 +80,24 @@ describe('PendingNonceMiddleware', function () {
r: '0x5f973e540f2d3c2f06d3725a626b75247593cb36477187ae07ecfe0a4db3cf57', r: '0x5f973e540f2d3c2f06d3725a626b75247593cb36477187ae07ecfe0a4db3cf57',
s: '0x0259b52ee8c58baaa385fb05c3f96116e58de89bcc165cb3bfdfc708672fed8a', s: '0x0259b52ee8c58baaa385fb05c3f96116e58de89bcc165cb3bfdfc708672fed8a',
}; };
it('should call next if not a eth_getTransactionByHash request', function (done) {
it('should call next if not a eth_getTransactionByHash request', () => {
const req = { method: 'eth_getBlockByNumber' }; const req = { method: 'eth_getBlockByNumber' };
const res = {}; const res = {};
pendingTxMiddleware(req, res, () => done()); const next = jest.fn();
pendingTxMiddleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
}); });
it('should call next if no pending txMeta is in history', function (done) { it('should call next if no pending txMeta is in history', () => {
const req = { method: 'eth_getTransactionByHash', params: [address] }; const req = { method: 'eth_getTransactionByHash', params: [address] };
const res = {}; const res = {};
pendingTxMiddleware(req, res, () => done()); const next = jest.fn();
pendingTxMiddleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
}); });
it('should fill the result with a the "pending" tx the result should match the rpc spec', function (done) { it('should fill the result with a the "pending" tx the result should match the rpc spec', () => {
returnUndefined = false; returnUndefined = false;
const req = { const req = {
method: 'eth_getTransactionByHash', method: 'eth_getTransactionByHash',
@ -95,15 +108,10 @@ describe('PendingNonceMiddleware', function () {
req, req,
res, res,
() => { () => {
done(new Error('should not have called next')); return new Error('should not have called next');
}, },
() => { () => {
assert.deepStrictEqual( expect(res.result).toStrictEqual(spec);
res.result,
spec,
new Error('result does not match the spec object'),
);
done();
}, },
); );
}); });

@ -1,4 +1,3 @@
import { strict as assert } from 'assert';
import { import {
TRANSACTION_STATUSES, TRANSACTION_STATUSES,
TRANSACTION_TYPES, TRANSACTION_TYPES,
@ -7,9 +6,9 @@ import {
import { formatTxMetaForRpcResult } from './util'; import { formatTxMetaForRpcResult } from './util';
describe('network utils', function () { describe('network utils', () => {
describe('formatTxMetaForRpcResult', function () { describe('formatTxMetaForRpcResult', () => {
it('should correctly format the tx meta object (EIP-1559)', function () { it('should correctly format the tx meta object (EIP-1559)', () => {
const txMeta = { const txMeta = {
id: 1, id: 1,
status: TRANSACTION_STATUSES.UNAPPROVED, status: TRANSACTION_STATUSES.UNAPPROVED,
@ -54,10 +53,10 @@ describe('network utils', function () {
value: '0x0', value: '0x0',
}; };
const result = formatTxMetaForRpcResult(txMeta); const result = formatTxMetaForRpcResult(txMeta);
assert.deepEqual(result, expectedResult); expect(result).toStrictEqual(expectedResult);
}); });
it('should correctly format the tx meta object (non EIP-1559)', function () { it('should correctly format the tx meta object (non EIP-1559)', () => {
const txMeta = { const txMeta = {
id: 1, id: 1,
status: TRANSACTION_STATUSES.UNAPPROVED, status: TRANSACTION_STATUSES.UNAPPROVED,
@ -99,7 +98,7 @@ describe('network utils', function () {
value: '0x0', value: '0x0',
}; };
const result = formatTxMetaForRpcResult(txMeta); const result = formatTxMetaForRpcResult(txMeta);
assert.deepEqual(result, expectedResult); expect(result).toStrictEqual(expectedResult);
}); });
}); });
}); });

@ -32,6 +32,7 @@ module.exports = {
'<rootDir>/app/scripts/lib/**/*.test.js', '<rootDir>/app/scripts/lib/**/*.test.js',
'<rootDir>/app/scripts/migrations/*.test.js', '<rootDir>/app/scripts/migrations/*.test.js',
'<rootDir>/app/scripts/platforms/*.test.js', '<rootDir>/app/scripts/platforms/*.test.js',
'<rootDir>app/scripts/controllers/network/**/*.test.js',
'<rootDir>/app/scripts/controllers/permissions/*.test.js', '<rootDir>/app/scripts/controllers/permissions/*.test.js',
], ],
testTimeout: 2500, testTimeout: 2500,

Loading…
Cancel
Save