2020-10-06 19:57:02 +02:00
|
|
|
import { strict as assert } from 'assert'
|
|
|
|
import sinon from 'sinon'
|
2020-01-09 04:34:58 +01:00
|
|
|
import NetworkController from '../../../../../app/scripts/controllers/network'
|
|
|
|
import { getNetworkDisplayName } from '../../../../../app/scripts/controllers/network/util'
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('NetworkController', function () {
|
|
|
|
describe('controller', function () {
|
|
|
|
let networkController
|
2020-08-14 13:47:02 +02:00
|
|
|
const noop = () => undefined
|
2020-02-11 17:51:13 +01:00
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: noop,
|
|
|
|
}
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
networkController = new NetworkController()
|
2020-09-10 18:16:00 +02:00
|
|
|
networkController.setInfuraProjectId('foo')
|
2020-02-11 17:51:13 +01:00
|
|
|
})
|
2018-03-09 20:20:18 +01:00
|
|
|
|
2017-05-28 20:18:07 +02:00
|
|
|
describe('#provider', function () {
|
2018-05-25 00:53:06 +02:00
|
|
|
it('provider should be updatable without reassignment', function () {
|
2018-05-03 03:07:29 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig)
|
2018-05-17 08:54:17 +02:00
|
|
|
const providerProxy = networkController.getProviderAndBlockTracker().provider
|
|
|
|
assert.equal(providerProxy.test, undefined)
|
|
|
|
providerProxy.setTarget({ test: true })
|
|
|
|
assert.equal(providerProxy.test, true)
|
2017-05-23 08:12:28 +02:00
|
|
|
})
|
|
|
|
})
|
2020-07-25 20:25:34 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
describe('#getNetworkState', function () {
|
2020-07-28 00:31:10 +02:00
|
|
|
it('should return "loading" when new', function () {
|
2017-05-28 20:18:07 +02:00
|
|
|
const networkState = networkController.getNetworkState()
|
2017-05-23 08:12:28 +02:00
|
|
|
assert.equal(networkState, 'loading', 'network is loading')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#setNetworkState', function () {
|
|
|
|
it('should update the network', function () {
|
2020-10-06 19:57:02 +02:00
|
|
|
networkController.setNetworkState('1')
|
2017-05-28 20:18:07 +02:00
|
|
|
const networkState = networkController.getNetworkState()
|
2020-10-06 19:57:02 +02:00
|
|
|
assert.equal(networkState, '1', 'network is 1')
|
2017-05-23 08:12:28 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#setProviderType', function () {
|
|
|
|
it('should update provider.type', function () {
|
2020-07-28 00:28:58 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig)
|
2017-05-23 08:12:28 +02:00
|
|
|
networkController.setProviderType('mainnet')
|
2020-08-18 22:06:58 +02:00
|
|
|
const { type } = networkController.getProviderConfig()
|
2017-05-23 08:12:28 +02:00
|
|
|
assert.equal(type, 'mainnet', 'provider type is updated')
|
|
|
|
})
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
it('should set the network to loading', function () {
|
2020-07-28 00:28:58 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig)
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
const spy = sinon.spy(networkController, 'setNetworkState')
|
2017-05-23 08:12:28 +02:00
|
|
|
networkController.setProviderType('mainnet')
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
spy.callCount, 1,
|
|
|
|
'should have called setNetworkState 2 times',
|
|
|
|
)
|
|
|
|
assert.ok(
|
|
|
|
spy.calledOnceWithExactly('loading'),
|
|
|
|
'should have called with "loading" first',
|
|
|
|
)
|
2017-05-23 08:12:28 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('utils', function () {
|
|
|
|
it('getNetworkDisplayName should return the correct network name', function () {
|
|
|
|
const tests = [
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '3',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Ropsten',
|
|
|
|
}, {
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '4',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Rinkeby',
|
|
|
|
}, {
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '42',
|
|
|
|
expected: 'Kovan',
|
|
|
|
}, {
|
|
|
|
input: '0x3',
|
|
|
|
expected: 'Ropsten',
|
|
|
|
}, {
|
|
|
|
input: '0x4',
|
|
|
|
expected: 'Rinkeby',
|
|
|
|
}, {
|
2020-07-03 19:14:43 +02:00
|
|
|
input: '0x2a',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Kovan',
|
|
|
|
}, {
|
|
|
|
input: 'ropsten',
|
|
|
|
expected: 'Ropsten',
|
|
|
|
}, {
|
|
|
|
input: 'rinkeby',
|
|
|
|
expected: 'Rinkeby',
|
|
|
|
}, {
|
|
|
|
input: 'kovan',
|
|
|
|
expected: 'Kovan',
|
|
|
|
}, {
|
|
|
|
input: 'mainnet',
|
2020-09-15 21:34:16 +02:00
|
|
|
expected: 'Ethereum Mainnet',
|
2020-02-11 17:51:13 +01:00
|
|
|
}, {
|
|
|
|
input: 'goerli',
|
|
|
|
expected: 'Goerli',
|
|
|
|
},
|
|
|
|
]
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
tests.forEach(({ input, expected }) => assert.equal(getNetworkDisplayName(input), expected))
|
|
|
|
})
|
2018-04-12 23:17:36 +02:00
|
|
|
})
|
|
|
|
})
|