nifty-wallet/test/unit/pending-balance-test.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-09-06 14:27:21 -07:00
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 = {}
2017-09-07 12:43:10 -07:00
const zeroBn = new BN(0)
2017-09-07 12:30:25 -07:00
const etherBn = new BN(String(1e18))
const ether = '0x' + etherBn.toString(16)
2017-09-06 14:27:21 -07:00
describe('PendingBalanceCalculator', function () {
2017-09-07 11:59:15 -07:00
let balanceCalculator
2017-09-06 14:27:21 -07:00
2017-09-25 14:36:49 -07:00
describe('#calculateMaxCost(tx)', function () {
2017-09-07 12:30:25 -07:00
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 })
2017-09-07 12:43:10 -07:00
const balanceCalculator = generateBalanceCalcWith([], zeroBn)
2017-09-25 14:36:49 -07:00
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
2017-09-07 12:30:25 -07:00
assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
})
2017-09-07 12:52:25 -07:00
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)
2017-09-25 14:36:49 -07:00
const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
assert.equal(result.toString(), '6', 'computes 6 wei of gas')
2017-09-07 12:52:25 -07:00
})
2017-09-07 12:30:25 -07:00
})
2017-09-06 14:27:21 -07:00
describe('if you have no pending txs and one ether', function () {
beforeEach(function () {
2017-09-07 12:47:27 -07:00
balanceCalculator = generateBalanceCalcWith([], etherBn)
2017-09-06 14:27:21 -07:00
})
2017-09-06 14:36:15 -07:00
it('returns the network balance', async function () {
2017-09-07 11:59:15 -07:00
const result = await balanceCalculator.getBalance()
2017-09-06 14:36:15 -07:00
assert.equal(result, ether, `gave ${result} needed ${ether}`)
2017-09-06 14:27:21 -07:00
})
})
2017-09-07 11:59:15 -07:00
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 })
2017-09-07 12:43:10 -07:00
balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
2017-09-07 11:59:15 -07:00
})
2017-09-07 12:53:30 -07:00
it('returns the subtracted result', async function () {
2017-09-07 11:59:15 -07:00
const result = await balanceCalculator.getBalance()
assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
return true
})
})
2017-09-06 14:27:21 -07:00
})
2017-09-07 12:43:10 -07:00
function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
2017-09-25 14:36:49 -07:00
const getPendingTransactions = async () => transactions
const getBalance = async () => providerStub
2017-09-06 14:27:21 -07:00
return new PendingBalanceCalculator({
2017-09-06 14:36:15 -07:00
getBalance,
2017-09-06 14:27:21 -07:00
getPendingTransactions,
})
}
2017-09-12 15:06:19 -07:00