From fd31fbde174edb48e7c2f74c44db456babb8dd71 Mon Sep 17 00:00:00 2001 From: naz_dou Date: Thu, 13 Feb 2020 16:26:47 +0200 Subject: [PATCH] test(Formatter): Add test's for Error cases --- es/utils/amount-formatter.js | 6 ++---- test/unit/amount-formatter.js | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/es/utils/amount-formatter.js b/es/utils/amount-formatter.js index 9e137fbed7..ca7649c672 100644 --- a/es/utils/amount-formatter.js +++ b/es/utils/amount-formatter.js @@ -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 @@ -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]) @@ -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 } ] diff --git a/test/unit/amount-formatter.js b/test/unit/amount-formatter.js index 705e714e9c..92f920b6ef 100644 --- a/test/unit/amount-formatter.js +++ b/test/unit/amount-formatter.js @@ -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 () { @@ -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', () => { @@ -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', () => { @@ -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) + } + }) + }) })