diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-09-27 02:46:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-27 02:46:28 +0800 |
commit | b1daa5ae260be0aa91516bde0cbdb8820099587b (patch) | |
tree | 21814a5909ee4988178378d54501a4bc9a6081a7 /test | |
parent | d135c3c9ca1dcbb25fae9ab39d92e923115ae2a1 (diff) | |
parent | 9e3648c668aed1f3e632efe1693d6a2e0aa76617 (diff) | |
download | tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar.gz tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar.bz2 tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar.lz tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar.xz tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.tar.zst tangerine-wallet-browser-b1daa5ae260be0aa91516bde0cbdb8820099587b.zip |
Merge pull request #2094 from MetaMask/AddBalanceController
Calculate balance based on pending txs
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/mock-encryptor.js | 4 | ||||
-rw-r--r-- | test/unit/components/pending-tx-test.js | 5 | ||||
-rw-r--r-- | test/unit/keyring-controller-test.js | 2 | ||||
-rw-r--r-- | test/unit/pending-balance-test.js | 93 | ||||
-rw-r--r-- | test/unit/tx-controller-test.js | 4 |
5 files changed, 105 insertions, 3 deletions
diff --git a/test/lib/mock-encryptor.js b/test/lib/mock-encryptor.js index cdf13c507..ef229a82f 100644 --- a/test/lib/mock-encryptor.js +++ b/test/lib/mock-encryptor.js @@ -29,4 +29,8 @@ module.exports = { return 'WHADDASALT!' }, + getRandomValues () { + return 'SOO RANDO!!!1' + } + } diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 22a98bc93..fdade1042 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -34,10 +34,15 @@ describe('PendingTx', function () { const renderer = ReactTestUtils.createRenderer() const newGasPrice = '0x77359400' + const computedBalances = {} + computedBalances[Object.keys(identities)[0]] = { + ethBalance: '0x00000000000000056bc75e2d63100000', + } const props = { identities, accounts: identities, txData, + computedBalances, sendTransaction: (txMeta, event) => { // Assert changes: const result = ethUtil.addHexPrefix(txMeta.txParams.gasPrice) diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js index 8d0d75f12..135edf365 100644 --- a/test/unit/keyring-controller-test.js +++ b/test/unit/keyring-controller-test.js @@ -24,7 +24,7 @@ describe('KeyringController', function () { getTxList: () => [], getUnapprovedTxList: () => [], }, - ethStore: { + accountTracker: { addAccount (acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, }, encryptor: mockEncryptor, diff --git a/test/unit/pending-balance-test.js b/test/unit/pending-balance-test.js new file mode 100644 index 000000000..5048d487b --- /dev/null +++ b/test/unit/pending-balance-test.js @@ -0,0 +1,93 @@ +const assert = require('assert') +const PendingBalanceCalculator = require('../../app/scripts/lib/pending-balance-calculator') +const MockTxGen = require('../lib/mock-tx-gen') +const BN = require('ethereumjs-util').BN +let providerResultStub = {} + +const zeroBn = new BN(0) +const etherBn = new BN(String(1e18)) +const ether = '0x' + etherBn.toString(16) + +describe('PendingBalanceCalculator', function () { + let balanceCalculator + + describe('#calculateMaxCost(tx)', function () { + it('returns a BN for a given tx value', function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: ether, + gasPrice: '0x0', + gas: '0x0', + } + }, { count: 1 }) + + const balanceCalculator = generateBalanceCalcWith([], zeroBn) + const result = balanceCalculator.calculateMaxCost(pendingTxs[0]) + assert.equal(result.toString(), etherBn.toString(), 'computes one ether') + }) + + it('calculates gas costs as well', function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: '0x0', + gasPrice: '0x2', + gas: '0x3', + } + }, { count: 1 }) + + const balanceCalculator = generateBalanceCalcWith([], zeroBn) + const result = balanceCalculator.calculateMaxCost(pendingTxs[0]) + assert.equal(result.toString(), '6', 'computes 6 wei of gas') + }) + }) + + describe('if you have no pending txs and one ether', function () { + + beforeEach(function () { + balanceCalculator = generateBalanceCalcWith([], etherBn) + }) + + it('returns the network balance', async function () { + const result = await balanceCalculator.getBalance() + assert.equal(result, ether, `gave ${result} needed ${ether}`) + }) + }) + + describe('if you have a one ether pending tx and one ether', function () { + beforeEach(function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: ether, + gasPrice: '0x0', + gas: '0x0', + } + }, { count: 1 }) + + balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn) + }) + + it('returns the subtracted result', async function () { + const result = await balanceCalculator.getBalance() + assert.equal(result, '0x0', `gave ${result} needed '0x0'`) + return true + }) + + }) +}) + +function generateBalanceCalcWith (transactions, providerStub = zeroBn) { + const getPendingTransactions = async () => transactions + const getBalance = async () => providerStub + + return new PendingBalanceCalculator({ + getBalance, + getPendingTransactions, + }) +} + diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 7bb193242..7b875db66 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -27,7 +27,7 @@ describe('Transaction Controller', function () { networkStore: new ObservableStore(currentNetworkId), txHistoryLimit: 10, blockTracker: { getCurrentBlock: noop, on: noop, once: noop }, - ethStore: { getState: noop }, + accountTracker: { getState: noop }, signTransaction: (ethTx) => new Promise((resolve) => { ethTx.sign(privKey) resolve() @@ -431,4 +431,4 @@ describe('Transaction Controller', function () { }).catch(done) }) }) -})
\ No newline at end of file +}) |