2017-05-23 08:12:28 +02:00
|
|
|
const assert = require('assert')
|
2018-02-15 17:34:31 +01:00
|
|
|
const nock = require('nock')
|
2019-10-30 23:15:54 +01:00
|
|
|
const NetworkController = require('../../../../../app/scripts/controllers/network')
|
2018-04-12 23:17:36 +02:00
|
|
|
const {
|
|
|
|
getNetworkDisplayName,
|
2019-10-30 23:15:54 +01:00
|
|
|
} = require('../../../../../app/scripts/controllers/network/util')
|
2017-05-23 08:12:28 +02:00
|
|
|
|
|
|
|
describe('# Network Controller', function () {
|
|
|
|
let networkController
|
2018-02-15 18:54:22 +01:00
|
|
|
const noop = () => {}
|
2018-05-03 03:07:29 +02:00
|
|
|
const networkControllerProviderConfig = {
|
2018-02-28 00:51:14 +01:00
|
|
|
getAccounts: noop,
|
2017-07-25 23:36:19 +02:00
|
|
|
}
|
2017-05-23 08:12:28 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2018-02-15 18:54:22 +01:00
|
|
|
|
|
|
|
nock('https://rinkeby.infura.io')
|
2018-03-09 20:20:18 +01:00
|
|
|
.persist()
|
2018-02-15 18:54:22 +01:00
|
|
|
.post('/metamask')
|
|
|
|
.reply(200)
|
|
|
|
|
2018-05-03 03:07:29 +02:00
|
|
|
networkController = new NetworkController()
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2018-05-03 03:07:29 +02:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig)
|
2017-05-23 08:12:28 +02:00
|
|
|
})
|
2018-03-09 20:20:18 +01:00
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
nock.cleanAll()
|
|
|
|
})
|
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
describe('network', function () {
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('#getNetworkState', function () {
|
|
|
|
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 () {
|
2018-10-26 10:26:43 +02:00
|
|
|
networkController.setNetworkState(1, 'rpc')
|
2017-05-28 20:18:07 +02:00
|
|
|
const networkState = networkController.getNetworkState()
|
2017-05-23 08:12:28 +02:00
|
|
|
assert.equal(networkState, 1, 'network is 1')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#setProviderType', function () {
|
|
|
|
it('should update provider.type', function () {
|
|
|
|
networkController.setProviderType('mainnet')
|
|
|
|
const type = networkController.getProviderConfig().type
|
|
|
|
assert.equal(type, 'mainnet', 'provider type is updated')
|
|
|
|
})
|
|
|
|
it('should set the network to loading', function () {
|
|
|
|
networkController.setProviderType('mainnet')
|
|
|
|
const loading = networkController.isNetworkLoading()
|
|
|
|
assert.ok(loading, 'network is loading')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-04-12 23:17:36 +02:00
|
|
|
})
|
|
|
|
|
2018-05-03 03:07:29 +02:00
|
|
|
describe('Network utils', () => {
|
2018-04-12 23:17:36 +02:00
|
|
|
it('getNetworkDisplayName should return the correct network name', () => {
|
|
|
|
const tests = [
|
|
|
|
{
|
|
|
|
input: 3,
|
|
|
|
expected: 'Ropsten',
|
|
|
|
}, {
|
|
|
|
input: 4,
|
|
|
|
expected: 'Rinkeby',
|
|
|
|
}, {
|
|
|
|
input: 42,
|
|
|
|
expected: 'Kovan',
|
|
|
|
}, {
|
|
|
|
input: 'ropsten',
|
|
|
|
expected: 'Ropsten',
|
|
|
|
}, {
|
|
|
|
input: 'rinkeby',
|
|
|
|
expected: 'Rinkeby',
|
|
|
|
}, {
|
|
|
|
input: 'kovan',
|
|
|
|
expected: 'Kovan',
|
|
|
|
}, {
|
|
|
|
input: 'mainnet',
|
|
|
|
expected: 'Main Ethereum Network',
|
2019-04-17 19:34:49 +02:00
|
|
|
}, {
|
|
|
|
input: 'goerli',
|
|
|
|
expected: 'Goerli',
|
2018-04-12 23:17:36 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
tests.forEach(({ input, expected }) => assert.equal(getNetworkDisplayName(input), expected))
|
|
|
|
})
|
|
|
|
})
|