1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 22:24:27 +01:00
metamask-extension/test/unit/network-contoller-test.js

82 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-05-23 08:12:28 +02:00
const assert = require('assert')
2018-02-15 17:34:31 +01:00
const nock = require('nock')
2017-05-23 08:12:28 +02:00
const NetworkController = require('../../app/scripts/controllers/network')
2018-02-15 18:54:22 +01:00
const { createTestProviderTools } = require('../stub/provider')
const providerResultStub = {}
const provider = createTestProviderTools({ scaffold: providerResultStub }).provider
2017-05-23 08:12:28 +02:00
describe('# Network Controller', function () {
let networkController
2018-02-15 18:54:22 +01:00
const noop = () => {}
const networkControllerProviderInit = {
2018-02-28 00:51:14 +01:00
getAccounts: noop,
}
2017-05-23 08:12:28 +02:00
beforeEach(function () {
2018-02-15 18:54:22 +01:00
nock('https://rinkeby.infura.io')
.persist()
2018-02-15 18:54:22 +01:00
.post('/metamask')
.reply(200)
2017-05-23 08:12:28 +02:00
networkController = new NetworkController({
2018-02-15 18:54:22 +01:00
provider,
2017-05-23 08:12:28 +02:00
})
2018-02-15 18:54:22 +01:00
networkController.initializeProvider(networkControllerProviderInit, provider)
2017-05-23 08:12:28 +02: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 () {
2017-05-23 08:12:28 +02:00
it('provider should be updatable without reassignment', function () {
2018-02-15 18:54:22 +01:00
networkController.initializeProvider(networkControllerProviderInit, provider)
const proxy = networkController._proxy
proxy.setTarget({ test: true, on: () => {} })
assert.ok(proxy.test)
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 () {
networkController.setNetworkState(1)
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('#getRpcAddressForType', function () {
it('should return the right rpc address', function () {
2017-05-28 20:18:07 +02:00
const rpcTarget = networkController.getRpcAddressForType('mainnet')
2017-05-23 08:12:28 +02:00
assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
})
})
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')
})
it('should set the right rpcTarget', function () {
networkController.setProviderType('mainnet')
const rpcTarget = networkController.getProviderConfig().rpcTarget
assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
})
})
})
2018-02-15 18:54:22 +01:00
})