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';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS } from '../../../shared/constants/network';
|
2021-03-16 22:00:08 +01:00
|
|
|
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({
|
2022-09-29 05:26:01 +02:00
|
|
|
getCurrentChainId: () => CHAIN_IDS.GOERLI,
|
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',
|
2022-09-29 05:26:01 +02:00
|
|
|
CHAIN_IDS.GOERLI,
|
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: {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: {
|
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' },
|
|
|
|
},
|
2022-09-29 05:26:01 +02:00
|
|
|
CHAIN_IDS.GOERLI,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-11-30 23:51:24 +01:00
|
|
|
|
|
|
|
assert.deepEqual(result, {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: {
|
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: {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: {
|
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, {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: {
|
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({
|
2022-09-29 05:26:01 +02:00
|
|
|
getCurrentChainId: () => CHAIN_IDS.GOERLI,
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|