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