refactored and added unit test

feature/default_network_editable
Simon Liang 7 years ago
parent e8ade42f2a
commit 0db627d979
  1. 32
      test/unit/components/balance-component-test.js
  2. 45
      ui/app/components/balance-component.js

@ -0,0 +1,32 @@
var assert = require('assert')
var BalanceComponent = require('../../../ui/app/components/balance-component')
describe('BalanceComponent', function () {
let balanceComponent
beforeEach(function () {
balanceComponent = new BalanceComponent()
})
it('shows token balance and convert to fiat value based on conversion rate', function () {
const formattedBalance = '1.23 ETH'
const tokenBalance = balanceComponent.getTokenBalance(formattedBalance, false)
const fiatDisplayNumber = balanceComponent.getFiatDisplayNumber(formattedBalance, 2)
assert.equal('1.23 ETH', tokenBalance)
assert.equal(2.46, fiatDisplayNumber)
})
it('shows only the token balance when conversion rate is not available', function () {
const formattedBalance = '1.23 ETH'
const tokenBalance = balanceComponent.getTokenBalance(formattedBalance, false)
const fiatDisplayNumber = balanceComponent.getFiatDisplayNumber(formattedBalance, 0)
assert.equal('1.23 ETH', tokenBalance)
assert.equal('N/A', fiatDisplayNumber)
})
})

@ -44,17 +44,12 @@ BalanceComponent.prototype.renderBalance = function (formattedBalance) {
]) ])
} }
var balanceObj = generateBalanceObject(formattedBalance, shorten ? 1 : 3)
var balanceValue = shorten ? balanceObj.shortBalance : balanceObj.balance
var label = balanceObj.label
// laptop: 5vw? // laptop: 5vw?
// phone: 50vw? // phone: 50vw?
return h('div.flex-column.balance-display', {}, [ return h('div.flex-column.balance-display', {}, [
h('div.token-amount', { h('div.token-amount', {
style: {}, style: {},
}, `${balanceValue} ${label}`), }, this.getTokenBalance(formattedBalance, shorten)),
showFiat ? this.renderFiatValue(formattedBalance) : null, showFiat ? this.renderFiatValue(formattedBalance) : null,
]) ])
@ -65,25 +60,33 @@ BalanceComponent.prototype.renderFiatValue = function (formattedBalance) {
const props = this.props const props = this.props
const { conversionRate, currentCurrency } = props const { conversionRate, currentCurrency } = props
if (formattedBalance === 'None') return formattedBalance const fiatDisplayNumber = this.getFiatDisplayNumber(formattedBalance, conversionRate)
var fiatDisplayNumber
var splitBalance = formattedBalance.split(' ')
if (conversionRate !== 0) {
fiatDisplayNumber = (Number(splitBalance[0]) * conversionRate).toFixed(2)
} else {
fiatDisplayNumber = 'N/A'
}
return fiatDisplay(fiatDisplayNumber, currentCurrency) return this.renderFiatAmount(fiatDisplayNumber, currentCurrency)
} }
function fiatDisplay (fiatDisplayNumber, fiatSuffix) { BalanceComponent.prototype.renderFiatAmount = function (fiatDisplayNumber, fiatSuffix) {
if (fiatDisplayNumber !== 'N/A') { if (fiatDisplayNumber === 'N/A') return null
return h('div.fiat-amount', { return h('div.fiat-amount', {
style: {}, style: {},
}, `${fiatDisplayNumber} ${fiatSuffix}`) }, `${fiatDisplayNumber} ${fiatSuffix}`)
} else { }
return h('div')
} BalanceComponent.prototype.getTokenBalance = function (formattedBalance, shorten) {
const balanceObj = generateBalanceObject(formattedBalance, shorten ? 1 : 3)
const balanceValue = shorten ? balanceObj.shortBalance : balanceObj.balance
const label = balanceObj.label
return `${balanceValue} ${label}`
}
BalanceComponent.prototype.getFiatDisplayNumber = function (formattedBalance, conversionRate) {
if (formattedBalance === 'None') return formattedBalance
if (conversionRate === 0) return 'N/A'
const splitBalance = formattedBalance.split(' ')
return (Number(splitBalance[0]) * conversionRate).toFixed(2)
} }

Loading…
Cancel
Save