You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.9 KiB
152 lines
3.9 KiB
4 years ago
|
import assert from 'assert';
|
||
|
import sinon from 'sinon';
|
||
4 years ago
|
import { KOVAN_CHAIN_ID } from '../../../shared/constants/network';
|
||
|
import CachedBalancesController from './cached-balances';
|
||
6 years ago
|
|
||
5 years ago
|
describe('CachedBalancesController', function () {
|
||
|
describe('updateCachedBalances', function () {
|
||
|
it('should update the cached balances', async function () {
|
||
6 years ago
|
const controller = new CachedBalancesController({
|
||
4 years ago
|
getCurrentChainId: () => KOVAN_CHAIN_ID,
|
||
6 years ago
|
accountTracker: {
|
||
|
store: {
|
||
4 years ago
|
subscribe: () => undefined,
|
||
6 years ago
|
},
|
||
|
},
|
||
|
initState: {
|
||
|
cachedBalances: 'mockCachedBalances',
|
||
|
},
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
controller._generateBalancesToCache = sinon
|
||
|
.stub()
|
||
4 years ago
|
.callsFake(() => Promise.resolve('mockNewCachedBalances'));
|
||
6 years ago
|
|
||
4 years ago
|
await controller.updateCachedBalances({ accounts: 'mockAccounts' });
|
||
6 years ago
|
|
||
4 years ago
|
assert.equal(controller._generateBalancesToCache.callCount, 1);
|
||
4 years ago
|
assert.deepEqual(controller._generateBalancesToCache.args[0], [
|
||
|
'mockAccounts',
|
||
4 years ago
|
KOVAN_CHAIN_ID,
|
||
4 years ago
|
]);
|
||
4 years ago
|
assert.equal(
|
||
|
controller.store.getState().cachedBalances,
|
||
|
'mockNewCachedBalances',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
describe('_generateBalancesToCache', function () {
|
||
|
it('should generate updated account balances where the current network was updated', function () {
|
||
6 years ago
|
const controller = new CachedBalancesController({
|
||
|
accountTracker: {
|
||
|
store: {
|
||
4 years ago
|
subscribe: () => undefined,
|
||
6 years ago
|
},
|
||
|
},
|
||
|
initState: {
|
||
|
cachedBalances: {
|
||
4 years ago
|
[KOVAN_CHAIN_ID]: {
|
||
6 years ago
|
a: '0x1',
|
||
|
b: '0x2',
|
||
|
c: '0x3',
|
||
|
},
|
||
|
16: {
|
||
|
a: '0xa',
|
||
|
b: '0xb',
|
||
|
c: '0xc',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
const result = controller._generateBalancesToCache(
|
||
|
{
|
||
|
a: { balance: '0x4' },
|
||
|
b: { balance: null },
|
||
|
c: { balance: '0x5' },
|
||
|
},
|
||
4 years ago
|
KOVAN_CHAIN_ID,
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
|
assert.deepEqual(result, {
|
||
4 years ago
|
[KOVAN_CHAIN_ID]: {
|
||
6 years ago
|
a: '0x4',
|
||
|
b: '0x2',
|
||
|
c: '0x5',
|
||
|
},
|
||
|
16: {
|
||
|
a: '0xa',
|
||
|
b: '0xb',
|
||
|
c: '0xc',
|
||
|
},
|
||
4 years ago
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
it('should generate updated account balances where the a new network was selected', function () {
|
||
6 years ago
|
const controller = new CachedBalancesController({
|
||
|
accountTracker: {
|
||
|
store: {
|
||
4 years ago
|
subscribe: () => undefined,
|
||
6 years ago
|
},
|
||
|
},
|
||
|
initState: {
|
||
|
cachedBalances: {
|
||
4 years ago
|
[KOVAN_CHAIN_ID]: {
|
||
6 years ago
|
a: '0x1',
|
||
|
b: '0x2',
|
||
|
c: '0x3',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
const result = controller._generateBalancesToCache(
|
||
|
{
|
||
|
a: { balance: '0x4' },
|
||
|
b: { balance: null },
|
||
|
c: { balance: '0x5' },
|
||
|
},
|
||
|
16,
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
|
assert.deepEqual(result, {
|
||
4 years ago
|
[KOVAN_CHAIN_ID]: {
|
||
6 years ago
|
a: '0x1',
|
||
|
b: '0x2',
|
||
|
c: '0x3',
|
||
|
},
|
||
|
16: {
|
||
|
a: '0x4',
|
||
|
c: '0x5',
|
||
|
},
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
describe('_registerUpdates', function () {
|
||
|
it('should subscribe to the account tracker with the updateCachedBalances method', async function () {
|
||
4 years ago
|
const subscribeSpy = sinon.spy();
|
||
6 years ago
|
const controller = new CachedBalancesController({
|
||
4 years ago
|
getCurrentChainId: () => KOVAN_CHAIN_ID,
|
||
6 years ago
|
accountTracker: {
|
||
|
store: {
|
||
|
subscribe: subscribeSpy,
|
||
|
},
|
||
|
},
|
||
4 years ago
|
});
|
||
|
subscribeSpy.resetHistory();
|
||
6 years ago
|
|
||
4 years ago
|
const updateCachedBalancesSpy = sinon.spy();
|
||
|
controller.updateCachedBalances = updateCachedBalancesSpy;
|
||
|
controller._registerUpdates({ accounts: 'mockAccounts' });
|
||
6 years ago
|
|
||
4 years ago
|
assert.equal(subscribeSpy.callCount, 1);
|
||
6 years ago
|
|
||
4 years ago
|
subscribeSpy.args[0][0]();
|
||
6 years ago
|
|
||
4 years ago
|
assert.equal(updateCachedBalancesSpy.callCount, 1);
|
||
|
});
|
||
|
});
|
||
|
});
|