aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/pending-balance-test.js
diff options
context:
space:
mode:
authorChi Kei Chan <chikeichan@gmail.com>2017-09-28 13:55:44 +0800
committerGitHub <noreply@github.com>2017-09-28 13:55:44 +0800
commit42ff763674f72d11602048642a74651d89128193 (patch)
treefe76102102c875ee6eaaee8cb57829899936c519 /test/unit/pending-balance-test.js
parentdeee689426f0b6236093128b47be81faf56d6b75 (diff)
parente711a4193f34b532c78526d68a32baff93eb061d (diff)
downloadtangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar.gz
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar.bz2
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar.lz
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar.xz
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.tar.zst
tangerine-wallet-browser-42ff763674f72d11602048642a74651d89128193.zip
Merge pull request #2201 from chikeichan/merge
Merge master into NewUI-flat
Diffstat (limited to 'test/unit/pending-balance-test.js')
-rw-r--r--test/unit/pending-balance-test.js93
1 files changed, 93 insertions, 0 deletions
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,
+ })
+}
+