diff --git a/src/utils/__tests__/formatUtils.test.js b/src/utils/__tests__/formatUtils.test.js new file mode 100644 index 00000000..b1c01d0a --- /dev/null +++ b/src/utils/__tests__/formatUtils.test.js @@ -0,0 +1,48 @@ +import * as formatUtils from '../formatUtils'; + +describe('format utils', () => { + it('formatAmount', () => { + const input = [ + { amount: 0, coinInfo: { isBitcoin: true, currencyUnits: 'mbtc', shortcut: 'btc' } }, + { amount: 0.5, coinInfo: { isBitcoin: true, currencyUnits: 'mbtc', shortcut: 'btc' } }, + { amount: 1, coinInfo: { isBitcoin: false, shortcut: 'eth' } }, + { amount: 99999, coinInfo: { isBitcoin: false, currencyUnits: 'tau' } }, + ]; + + input.forEach((entry) => { + expect(formatUtils.formatAmount(entry.amount, entry.coinInfo)).toMatchSnapshot(); + }); + }); + + it('formatTime', () => { + const input = [0, 1, 2, 100, 999]; + + input.forEach((entry) => { + expect(formatUtils.formatTime(entry)).toMatchSnapshot(); + }); + }); + + it('btckb2satoshib', () => { + const input = [0, 1, 2, 100, 999]; + + input.forEach((entry) => { + expect(formatUtils.btckb2satoshib(entry)).toMatchSnapshot(); + }); + }); + + it('stringToHex', () => { + const input = ['test', '0001', 'test99999']; + + input.forEach((entry) => { + expect(formatUtils.stringToHex(entry)).toMatchSnapshot(); + }); + }); + + it('hexToString', () => { + const input = [0, 'xxxtestSringtestStringaaaaaa', 30303031, 2746573743939393939, -9]; + + input.forEach((entry) => { + expect(formatUtils.hexToString(entry)).toMatchSnapshot(); + }); + }); +}); diff --git a/src/utils/formatUtils.js b/src/utils/formatUtils.js index 982c9afb..986b0ee9 100644 --- a/src/utils/formatUtils.js +++ b/src/utils/formatUtils.js @@ -1,11 +1,9 @@ /* @flow */ - -const currencyUnits: string = 'mbtc2'; - // TODO: chagne currency units +const currencyUnitsConstant: string = 'mbtc2'; -export const formatAmount = (n: number, coinInfo: any): string => { +export const formatAmount = (n: number, coinInfo: any, currencyUnits = currencyUnitsConstant): string => { const amount = (n / 1e8); if (coinInfo.isBitcoin && currencyUnits === 'mbtc' && amount <= 0.1 && n !== 0) { const s = (n / 1e5).toString();