1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/controllers/cached-balances.test.js

152 lines
3.9 KiB
JavaScript
Raw Normal View History

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