2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { KOVAN_CHAIN_ID } from '../../../shared/constants/network';
|
|
|
|
import CachedBalancesController from './cached-balances';
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('CachedBalancesController', function () {
|
|
|
|
describe('updateCachedBalances', function () {
|
|
|
|
it('should update the cached balances', async function () {
|
2018-11-30 23:51:24 +01:00
|
|
|
const controller = new CachedBalancesController({
|
2021-03-02 23:53:07 +01:00
|
|
|
getCurrentChainId: () => KOVAN_CHAIN_ID,
|
2018-11-30 23:51:24 +01:00
|
|
|
accountTracker: {
|
|
|
|
store: {
|
2020-08-14 13:47:02 +02:00
|
|
|
subscribe: () => undefined,
|
2018-11-30 23:51:24 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: {
|
|
|
|
cachedBalances: 'mockCachedBalances',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
controller._generateBalancesToCache = sinon
|
|
|
|
.stub()
|
2021-02-04 19:15:23 +01:00
|
|
|
.callsFake(() => Promise.resolve('mockNewCachedBalances'));
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await controller.updateCachedBalances({ accounts: 'mockAccounts' });
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(controller._generateBalancesToCache.callCount, 1);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.deepEqual(controller._generateBalancesToCache.args[0], [
|
|
|
|
'mockAccounts',
|
2021-03-02 23:53:07 +01:00
|
|
|
KOVAN_CHAIN_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
controller.store.getState().cachedBalances,
|
|
|
|
'mockNewCachedBalances',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('_generateBalancesToCache', function () {
|
|
|
|
it('should generate updated account balances where the current network was updated', function () {
|
2018-11-30 23:51:24 +01:00
|
|
|
const controller = new CachedBalancesController({
|
|
|
|
accountTracker: {
|
|
|
|
store: {
|
2020-08-14 13:47:02 +02:00
|
|
|
subscribe: () => undefined,
|
2018-11-30 23:51:24 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: {
|
|
|
|
cachedBalances: {
|
2021-03-02 23:53:07 +01:00
|
|
|
[KOVAN_CHAIN_ID]: {
|
2018-11-30 23:51:24 +01:00
|
|
|
a: '0x1',
|
|
|
|
b: '0x2',
|
|
|
|
c: '0x3',
|
|
|
|
},
|
|
|
|
16: {
|
|
|
|
a: '0xa',
|
|
|
|
b: '0xb',
|
|
|
|
c: '0xc',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = controller._generateBalancesToCache(
|
|
|
|
{
|
|
|
|
a: { balance: '0x4' },
|
|
|
|
b: { balance: null },
|
|
|
|
c: { balance: '0x5' },
|
|
|
|
},
|
2021-03-02 23:53:07 +01:00
|
|
|
KOVAN_CHAIN_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
assert.deepEqual(result, {
|
2021-03-02 23:53:07 +01:00
|
|
|
[KOVAN_CHAIN_ID]: {
|
2018-11-30 23:51:24 +01:00
|
|
|
a: '0x4',
|
|
|
|
b: '0x2',
|
|
|
|
c: '0x5',
|
|
|
|
},
|
|
|
|
16: {
|
|
|
|
a: '0xa',
|
|
|
|
b: '0xb',
|
|
|
|
c: '0xc',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should generate updated account balances where the a new network was selected', function () {
|
2018-11-30 23:51:24 +01:00
|
|
|
const controller = new CachedBalancesController({
|
|
|
|
accountTracker: {
|
|
|
|
store: {
|
2020-08-14 13:47:02 +02:00
|
|
|
subscribe: () => undefined,
|
2018-11-30 23:51:24 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: {
|
|
|
|
cachedBalances: {
|
2021-03-02 23:53:07 +01:00
|
|
|
[KOVAN_CHAIN_ID]: {
|
2018-11-30 23:51:24 +01:00
|
|
|
a: '0x1',
|
|
|
|
b: '0x2',
|
|
|
|
c: '0x3',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = controller._generateBalancesToCache(
|
|
|
|
{
|
|
|
|
a: { balance: '0x4' },
|
|
|
|
b: { balance: null },
|
|
|
|
c: { balance: '0x5' },
|
|
|
|
},
|
|
|
|
16,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
assert.deepEqual(result, {
|
2021-03-02 23:53:07 +01:00
|
|
|
[KOVAN_CHAIN_ID]: {
|
2018-11-30 23:51:24 +01:00
|
|
|
a: '0x1',
|
|
|
|
b: '0x2',
|
|
|
|
c: '0x3',
|
|
|
|
},
|
|
|
|
16: {
|
|
|
|
a: '0x4',
|
|
|
|
c: '0x5',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('_registerUpdates', function () {
|
|
|
|
it('should subscribe to the account tracker with the updateCachedBalances method', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const subscribeSpy = sinon.spy();
|
2018-11-30 23:51:24 +01:00
|
|
|
const controller = new CachedBalancesController({
|
2021-03-02 23:53:07 +01:00
|
|
|
getCurrentChainId: () => KOVAN_CHAIN_ID,
|
2018-11-30 23:51:24 +01:00
|
|
|
accountTracker: {
|
|
|
|
store: {
|
|
|
|
subscribe: subscribeSpy,
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
subscribeSpy.resetHistory();
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const updateCachedBalancesSpy = sinon.spy();
|
|
|
|
controller.updateCachedBalances = updateCachedBalancesSpy;
|
|
|
|
controller._registerUpdates({ accounts: 'mockAccounts' });
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(subscribeSpy.callCount, 1);
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
subscribeSpy.args[0][0]();
|
2018-11-30 23:51:24 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(updateCachedBalancesSpy.callCount, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|