diff options
author | Dan J Miller <danjm.com@gmail.com> | 2018-12-01 06:51:24 +0800 |
---|---|---|
committer | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2018-12-01 06:51:24 +0800 |
commit | 4c2455554540b9d0dd979bad329892279fddd8b9 (patch) | |
tree | e3b101ec86a65500023d8b7e99dd5973a9473626 /test/unit | |
parent | 45a9f40aa614753e274275eaeb4dd6c0251dcf45 (diff) | |
download | tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar.gz tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar.bz2 tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar.lz tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar.xz tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.tar.zst tangerine-wallet-browser-4c2455554540b9d0dd979bad329892279fddd8b9.zip |
Save recent network balances in local storage (#5843)
* Use selector for state.metamask.accounts in all cases.
* Default to cached balance when selecting metamask accounts
* Adds the cached-balances controller
* Documentation and small codes fixes for #5843
Co-Authored-By: danjm <danjm.com@gmail.com>
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/app/controllers/cached-balances-test.js | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/test/unit/app/controllers/cached-balances-test.js b/test/unit/app/controllers/cached-balances-test.js new file mode 100644 index 000000000..27aeabba2 --- /dev/null +++ b/test/unit/app/controllers/cached-balances-test.js @@ -0,0 +1,137 @@ +const assert = require('assert') +const sinon = require('sinon') +const CachedBalancesController = require('../../../../app/scripts/controllers/cached-balances') + +describe('CachedBalancesController', () => { + describe('updateCachedBalances', () => { + it('should update the cached balances', async () => { + const controller = new CachedBalancesController({ + getNetwork: () => Promise.resolve(17), + accountTracker: { + store: { + subscribe: () => {}, + }, + }, + initState: { + cachedBalances: 'mockCachedBalances', + }, + }) + + controller._generateBalancesToCache = sinon.stub().callsFake(() => Promise.resolve('mockNewCachedBalances')) + + await controller.updateCachedBalances({ accounts: 'mockAccounts' }) + + assert.equal(controller._generateBalancesToCache.callCount, 1) + assert.deepEqual(controller._generateBalancesToCache.args[0], ['mockAccounts', 17]) + assert.equal(controller.store.getState().cachedBalances, 'mockNewCachedBalances') + }) + }) + + describe('_generateBalancesToCache', () => { + it('should generate updated account balances where the current network was updated', () => { + const controller = new CachedBalancesController({ + accountTracker: { + store: { + subscribe: () => {}, + }, + }, + initState: { + cachedBalances: { + 17: { + a: '0x1', + b: '0x2', + c: '0x3', + }, + 16: { + a: '0xa', + b: '0xb', + c: '0xc', + }, + }, + }, + }) + + const result = controller._generateBalancesToCache({ + a: { balance: '0x4' }, + b: { balance: null }, + c: { balance: '0x5' }, + }, 17) + + assert.deepEqual(result, { + 17: { + 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', () => { + const controller = new CachedBalancesController({ + accountTracker: { + store: { + subscribe: () => {}, + }, + }, + initState: { + cachedBalances: { + 17: { + a: '0x1', + b: '0x2', + c: '0x3', + }, + }, + }, + }) + + const result = controller._generateBalancesToCache({ + a: { balance: '0x4' }, + b: { balance: null }, + c: { balance: '0x5' }, + }, 16) + + assert.deepEqual(result, { + 17: { + a: '0x1', + b: '0x2', + c: '0x3', + }, + 16: { + a: '0x4', + c: '0x5', + }, + }) + }) + }) + + describe('_registerUpdates', () => { + it('should subscribe to the account tracker with the updateCachedBalances method', async () => { + const subscribeSpy = sinon.spy() + const controller = new CachedBalancesController({ + getNetwork: () => Promise.resolve(17), + 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) + }) + }) + +}) |