1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 04:37:13 +01:00
metamask-extension/test/unit/app/controllers/network/network-controller.test.js

127 lines
3.6 KiB
JavaScript
Raw Normal View History

import { strict as assert } from 'assert';
import sinon from 'sinon';
import NetworkController from '../../../../../app/scripts/controllers/network';
import { getNetworkDisplayName } from '../../../../../app/scripts/controllers/network/util';
2017-05-23 08:12:28 +02:00
describe('NetworkController', function () {
describe('controller', function () {
let networkController;
const noop = () => undefined;
const networkControllerProviderConfig = {
getAccounts: noop,
};
2017-05-23 08:12:28 +02:00
beforeEach(function () {
networkController = new NetworkController();
networkController.setInfuraProjectId('foo');
});
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 () {
networkController.initializeProvider(networkControllerProviderConfig);
2020-11-03 00:41:28 +01: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 () {
const networkState = networkController.getNetworkState();
assert.equal(networkState, 'loading', 'network is loading');
});
});
2017-05-23 08:12:28 +02:00
describe('#setNetworkState', function () {
it('should update the network', function () {
networkController.setNetworkState('1');
const networkState = networkController.getNetworkState();
assert.equal(networkState, '1', 'network is 1');
});
});
2017-05-23 08:12:28 +02:00
describe('#setProviderType', function () {
it('should update provider.type', function () {
networkController.initializeProvider(networkControllerProviderConfig);
networkController.setProviderType('mainnet');
const { type } = networkController.getProviderConfig();
assert.equal(type, 'mainnet', 'provider type is updated');
});
2017-05-23 08:12:28 +02:00
it('should set the network to loading', function () {
networkController.initializeProvider(networkControllerProviderConfig);
const spy = sinon.spy(networkController, 'setNetworkState');
networkController.setProviderType('mainnet');
assert.equal(
2020-11-03 00:41:28 +01:00
spy.callCount,
1,
'should have called setNetworkState 2 times',
);
assert.ok(
spy.calledOnceWithExactly('loading'),
'should have called with "loading" first',
);
});
});
});
2018-04-12 23:17:36 +02:00
describe('utils', function () {
it('getNetworkDisplayName should return the correct network name', function () {
const tests = [
{
input: '3',
expected: 'Ropsten',
2020-11-03 00:41:28 +01:00
},
{
input: '4',
expected: 'Rinkeby',
2020-11-03 00:41:28 +01:00
},
{
input: '42',
expected: 'Kovan',
2020-11-03 00:41:28 +01:00
},
{
input: '0x3',
expected: 'Ropsten',
2020-11-03 00:41:28 +01:00
},
{
input: '0x4',
expected: 'Rinkeby',
2020-11-03 00:41:28 +01:00
},
{
2020-07-03 19:14:43 +02:00
input: '0x2a',
expected: 'Kovan',
2020-11-03 00:41:28 +01:00
},
{
input: 'ropsten',
expected: 'Ropsten',
2020-11-03 00:41:28 +01:00
},
{
input: 'rinkeby',
expected: 'Rinkeby',
2020-11-03 00:41:28 +01:00
},
{
input: 'kovan',
expected: 'Kovan',
2020-11-03 00:41:28 +01:00
},
{
input: 'mainnet',
expected: 'Ethereum Mainnet',
2020-11-03 00:41:28 +01:00
},
{
input: 'goerli',
expected: 'Goerli',
},
];
2018-04-12 23:17:36 +02:00
2020-11-03 00:41:28 +01:00
tests.forEach(({ input, expected }) =>
assert.equal(getNetworkDisplayName(input), expected),
);
});
});
});