Skip to content

Commit

Permalink
test(Formatter): Add test's for Error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Feb 13, 2020
1 parent 994b779 commit fd31fbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 2 additions & 4 deletions es/utils/amount-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const DENOMINATION_MAGNITUDE = {
* @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos']
* @return {BigNumber}
*/
export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS }) => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE })
export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE })

/**
* Convert amount to aettos
Expand All @@ -72,11 +72,10 @@ export const toAettos = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}
* @param {String} [options.targetDenomination='aettos'] target denomination, can be ['ae', 'aettos']
* @return {BigNumber}
*/
export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS }) => {
export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => {
if (!Object.values(AE_AMOUNT_FORMATS).includes(denomination)) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!Object.values(AE_AMOUNT_FORMATS).includes(targetDenomination)) throw new Error(`Invalid target denomination. Current: ${targetDenomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!isBigNumber(value)) throw new Error(`Value ${value} is not type of number`)
if (asBigNumber(value).lt(0)) throw Error('Value is less then 0')

return asBigNumber(value)
.shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
Expand All @@ -87,7 +86,6 @@ const prefixes = [
{ name: 'exa', magnitude: 18 },
{ name: 'giga', magnitude: 9 },
{ name: '', magnitude: 0 },
{ name: 'micro', magnitude: -6 },
{ name: 'pico', magnitude: -12 }
]

Expand Down
19 changes: 16 additions & 3 deletions test/unit/amount-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import '../'
import { describe, it } from 'mocha'
import { AE_AMOUNT_FORMATS, formatAmount } from '../../es/utils/amount-formatter'
import { AE_AMOUNT_FORMATS, formatAmount, toAe, toAettos } from '../../es/utils/amount-formatter'
import { asBigNumber, parseBigNumber } from '../../es/utils/bignumber'

describe('Amount Formatter', function () {
Expand All @@ -29,7 +29,7 @@ describe('Amount Formatter', function () {
[10012312, AE_AMOUNT_FORMATS.AE, 10012312e18],
[1, AE_AMOUNT_FORMATS.AETTOS, 1]
].forEach(
([v, d, e]) => parseBigNumber(e).should.be.equal(formatAmount(v, { denomination: d }).toString(10))
([v, d, e]) => parseBigNumber(e).should.be.equal(toAettos(v, { denomination: d }).toString(10))
)
})
it('to Ae', () => {
Expand All @@ -40,7 +40,7 @@ describe('Amount Formatter', function () {
[10012312, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AE, 1]
].forEach(
([v, d, e]) => parseBigNumber(e).should.be.equal(formatAmount(v, { denomination: d, targetDenomination: AE_AMOUNT_FORMATS.AE }).toString(10))
([v, d, e]) => parseBigNumber(e).should.be.equal(toAe(v, { denomination: d }).toString(10))
)
})
it('format', () => {
Expand All @@ -60,4 +60,17 @@ describe('Amount Formatter', function () {
([v, dF, dT, e]) => parseBigNumber(e).should.be.equal(formatAmount(v, { denomination: dF, targetDenomination: dT }).toString(10))
)
})
it('Invalid value', () => {
[
[true, [AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AE], `Value ${true} is not type of number`],
[1, [AE_AMOUNT_FORMATS.AE, 'ASD'], `Invalid target denomination. Current: ASD, available [${Object.keys(AE_AMOUNT_FORMATS)}]`],
[1, ['ASD', AE_AMOUNT_FORMATS.AE], `Invalid denomination. Current: ASD, available [${Object.keys(AE_AMOUNT_FORMATS)}]`]
].forEach(([v, [dF, dT], error]) => {
try {
formatAmount(v, { denomination: dF, targetDenomination: dT })
} catch (e) {
e.message.should.be.equal(error)
}
})
})
})

0 comments on commit fd31fbd

Please sign in to comment.