UNPKG

crypto-units-convert

Version:
428 lines (405 loc) 17.8 kB
/* eslint-disable no-undef */ const assert = require('assert'); const Units = require('../index.js'); describe('Units', () => { describe('convertBTC', () => { it('should convert BTC to smaller units', () => { assert.equal(Units.convertBTC('1', 'btc', 'mbtc'), '1000'); assert.equal(Units.convertBTC('1', 'btc', 'ubtc'), '1000000'); assert.equal(Units.convertBTC('1', 'btc', 'bit'), '1000000'); assert.equal(Units.convertBTC('1', 'btc', 'satoshi'), '100000000'); }); it('should convert mbtc to other units', () => { assert.equal(Units.convertBTC('1', 'mbtc', 'btc'), '0.001'); assert.equal(Units.convertBTC('1', 'mbtc', 'ubtc'), '1000'); assert.equal(Units.convertBTC('1', 'mbtc', 'satoshi'), '100000'); }); it('should convert ubtc to other units', function() { assert.equal(Units.convertBTC('1', 'ubtc', 'btc'), '0.000001'); assert.equal(Units.convertBTC('1', 'ubtc', 'mbtc'), '0.001'); assert.equal(Units.convertBTC('1', 'ubtc', 'satoshi'), '100'); }); it('should convert Satoshi to bigger units', () => { assert.equal(Units.convertBTC('1', 'satoshi', 'bit'), '0.01'); assert.equal(Units.convertBTC('1', 'satoshi', 'ubtc'), '0.01'); assert.equal(Units.convertBTC('1', 'satoshi', 'mbtc'), '0.00001'); assert.equal(Units.convertBTC('1', 'satoshi', 'btc'), '0.00000001'); }); it('should fail on invalid input satoshi', () => { assert.throws(() => { Units.convertBTC('1', 'random', 'satoshi'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid input btc', () => { assert.throws(() => { Units.convertBTC('1', 'random', 'btc'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output btc', () => { assert.throws(() => { Units.convertBTC('1', 'satoshi', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertBTC('1,00', 'btc', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertBTC('test', 'btc', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertBTC('0.000000001', 'btc', 'satoshi'); }, /^Error: Unsupported decimal points. Satoshis must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertBTC('.1', 'btc', 'satoshi'), '10000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertBTC('1', 'bTc', 'satOSHi'), '100000000'); }); }); describe('convertBCH', () => { it('should convert BCH to smaller units', () => { assert.equal(Units.convertBCH('1', 'bch', 'mbch'), '1000'); assert.equal(Units.convertBCH('1', 'bch', 'ubch'), '1000000'); assert.equal(Units.convertBCH('1', 'bch', 'bit'), '1000000'); assert.equal(Units.convertBCH('1', 'bch', 'satoshi'), '100000000'); }); it('should convert mbch to other units', () => { assert.equal(Units.convertBCH('1', 'mbch', 'bch'), '0.001'); assert.equal(Units.convertBCH('1', 'mbch', 'ubch'), '1000'); assert.equal(Units.convertBCH('1', 'mbch', 'satoshi'), '100000'); }); it('should convert ubch to other units', function() { assert.equal(Units.convertBCH('1', 'ubch', 'bch'), '0.000001'); assert.equal(Units.convertBCH('1', 'ubch', 'mbch'), '0.001'); assert.equal(Units.convertBCH('1', 'ubch', 'satoshi'), '100'); }); it('should convert Satoshi to bigger units', () => { assert.equal(Units.convertBCH('1', 'satoshi', 'bit'), '0.01'); assert.equal(Units.convertBCH('1', 'satoshi', 'ubch'), '0.01'); assert.equal(Units.convertBCH('1', 'satoshi', 'mbch'), '0.00001'); assert.equal(Units.convertBCH('1', 'satoshi', 'bch'), '0.00000001'); }); it('should fail on invalid input satoshi', () => { assert.throws(() => { Units.convertBCH('1', 'random', 'satoshi'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid input bch', () => { assert.throws(() => { Units.convertBCH('1', 'random', 'bch'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output bch', () => { assert.throws(() => { Units.convertBCH('1', 'satoshi', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertBCH('1,00', 'bch', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertBCH('test', 'bch', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertBCH('0.000000001', 'bch', 'satoshi'); }, /^Error: Unsupported decimal points. Satoshis must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertBCH('.1', 'bch', 'satoshi'), '10000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertBCH('1', 'bch', 'satOSHi'), '100000000'); }); }); describe('convertETH', () => { it('should convert eth big unit to small unit', () => { assert.equal(Units.convertETH('1', 'eth', 'wei'), '1000000000000000000'); assert.equal(Units.convertETH('20', 'gwei', 'wei'), '20000000000'); assert.equal(Units.convertETH('20.05', 'gwei', 'wei'), '20050000000'); assert.equal(Units.convertETH('20.005', 'kwei', 'wei'), '20005'); }); it('should convert wei to bigger unit', function() { assert.equal(Units.convertETH('1', 'wei', 'eth'), '0.000000000000000001'); assert.equal(Units.convertETH('1', 'wei', 'finney'), '0.000000000000001'); assert.equal(Units.convertETH('1', 'wei', 'gwei'), '0.000000001'); assert.equal(Units.convertETH('1', 'wei', 'mwei'), '0.000001'); assert.equal(Units.convertETH('1', 'wei', 'kwei'), '0.001'); }); it('should convert kwei to bigger unit', function() { assert.equal(Units.convertETH('1', 'kwei', 'eth'), '0.000000000000001'); assert.equal(Units.convertETH('1', 'kwei', 'finney'), '0.000000000001'); assert.equal(Units.convertETH('1', 'kwei', 'gwei'), '0.000001'); assert.equal(Units.convertETH('1', 'kwei', 'mwei'), '0.001'); assert.equal(Units.convertETH('1', 'kwei', 'wei'), '1000'); }); it('should fail on invalid input wei', () => { assert.throws(() => { Units.convertETH('1', 'random', 'wei'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output wei', () => { assert.throws(() => { Units.convertETH('1', 'wei', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid input eth', () => { assert.throws(() => { Units.convertETH('1', 'random', 'eth'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertETH('1,00', 'eth', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertETH('test', 'eth', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertETH('0.0000000000000000001', 'eth', 'wei'); }, /^Error: Unsupported decimal points. Wei must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertETH('.1', 'eth', 'wei'), '100000000000000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertETH('1', 'eTh', 'WEi'), '1000000000000000000'); }); }); describe('convertXRP', () => { it('should convert Ripple big unit to small unit', () => { assert.equal(Units.convertXRP('1', 'xrp', 'drop'), '1000000'); assert.equal(Units.convertXRP('1.5', 'xrp', 'drop'), '1500000'); assert.equal(Units.convertXRP('1.05', 'xrp', 'drop'), '1050000'); }); it('should convert drop to bigger units', function() { assert.equal(Units.convertXRP('1', 'drop', 'xrp'), '0.000001'); }); it('should fail on invalid input drop', () => { assert.throws(() => { Units.convertXRP('1', 'random', 'drop'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output xrp', () => { assert.throws(() => { Units.convertXRP('1', 'xrp', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertXRP('1,00', 'xrp', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertXRP('test', 'xrp', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertXRP('0.0000001', 'xrp', 'drop'); }, /^Error: Unsupported decimal points. Drop must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertXRP('.1', 'xrp', 'drop'), '100000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertXRP('1', 'XrP', 'drOp'), '1000000'); }); }); describe('convertLTC', () => { it('should convert Litecoin big unit to small unit', () => { assert.equal(Units.convertLTC('1', 'ltc', 'litoshi'), '100000000'); assert.equal(Units.convertLTC('1', 'lite', 'litoshi'), '100000'); assert.equal(Units.convertLTC('1', 'photon', 'litoshi'), '100'); assert.equal(Units.convertLTC('1.5', 'photon', 'litoshi'), '150'); assert.equal(Units.convertLTC('1.05', 'photon', 'litoshi'), '105'); }); it('should convert litoshi to bigger unit', function() { assert.equal(Units.convertLTC('1', 'litoshi', 'ltc'), '0.00000001'); }); it('should fail on invalid input litoshi', () => { assert.throws(() => { Units.convertLTC('1', 'random', 'litoshi'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output ltc', () => { assert.throws(() => { Units.convertLTC('1', 'ltc', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertLTC('1,00', 'ltc', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertLTC('test', 'ltc', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertLTC('0.000000001', 'ltc', 'litoshi'); }, /^Error: Unsupported decimal points. Litoshi must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertLTC('.1', 'ltc', 'litoshi'), '10000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertLTC('1', 'lTc', 'LitoSHi'), '100000000'); }); }); describe('convertDASH', () => { it('should convert Dash big unit to small unit', () => { assert.equal(Units.convertDASH('1', 'dash', 'duff'), '100000000'); }); it('should convert duff to bigger unit', function() { assert.equal(Units.convertDASH('1', 'duff', 'dash'), '0.00000001'); }); it('should fail on invalid input duff', () => { assert.throws(() => { Units.convertDASH('1', 'random', 'duff'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output dash', () => { assert.throws(() => { Units.convertDASH('1', 'dash', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertDASH('1,00', 'dash', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertDASH('test', 'dash', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertDASH('0.000000001', 'dash', 'duff'); }, /^Error: Unsupported decimal points. Duff must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertDASH('.1', 'dash', 'duff'), '10000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertDASH('1', 'DAsh', 'dUff'), '100000000'); }); }); describe('convertXMR', () => { it('should convert Monero big unit to small unit', () => { assert.equal(Units.convertXMR('1', 'xmr', 'pxmr'), '1000000000000'); }); it('should convert pXMR to bigger unit', function() { assert.equal(Units.convertXMR('1', 'pxmr', 'xmr'), '0.000000000001'); }); it('should fail on invalid input pxmr', () => { assert.throws(() => { Units.convertXMR('1', 'random', 'pxmr'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output xmr', () => { assert.throws(() => { Units.convertXMR('1', 'xmr', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertXMR('1,00', 'xmr', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertXMR('test', 'xmr', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertXMR('0.0000000000001', 'xmr', 'pxmr'); }, /^Error: Unsupported decimal points. pXmr must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertXMR('.1', 'xmr', 'pxmr'), '100000000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertXMR('1', 'XmR', 'pXMR'), '1000000000000'); }); }); describe('convertDOT', () => { it('should convert DOT big unit to small unit', () => { assert.equal(Units.convertDOT('1', 'dot', 'planck'), '10000000000'); }); it('should convert planck to bigger unit', function() { assert.equal(Units.convertDOT('1', 'planck', 'dot'), '0.0000000001'); }); it('should fail on invalid input planck', () => { assert.throws(() => { Units.convertDOT('1', 'random', 'planck'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output DOT', () => { assert.throws(() => { Units.convertDOT('1', 'dot', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertDOT('1,00', 'dot', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertDOT('test', 'dot', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertDOT('0.00000000001', 'dot', 'planck'); }, /^Error: Unsupported decimal points. Planck must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertDOT('.1', 'dot', 'planck'), '1000000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertDOT('1', 'DoT', 'Planck'), '10000000000'); }); }); describe('convertZEC', () => { it('should convert Zcash big unit to small unit', () => { assert.equal(Units.convertZEC('1', 'zec', 'zatoshi'), '100000000'); }); it('should convert duff to bigger unit', function() { assert.equal(Units.convertZEC('1', 'zatoshi', 'zec'), '0.00000001'); }); it('should fail on invalid input zatoshi', () => { assert.throws(() => { Units.convertZEC('1', 'random', 'zatoshi'); }, /^Error: Unsupported input unit$/); }); it('should fail on invalid output zec', () => { assert.throws(() => { Units.convertZEC('1', 'zec', 'random'); }, /^Error: Unsupported input unit$/); }); it('should fail on non-decimal input', function () { assert.throws(function () { Units.convertZEC('1,00', 'zec', 'random'); }, /^Error: Unsupported value$/); assert.throws(function () { Units.convertZEC('test', 'zec', 'random'); }, /^Error: Unsupported value$/); }); it('should fail on invalid input resulting in a decimal error', () => { assert.throws(() => { Units.convertZEC('0.000000001', 'zec', 'zatoshi'); }, /^Error: Unsupported decimal points. Zatoshi must be an integer.$/); }); it('should work with decimal first numbers', () => { assert.equal(Units.convertZEC('.1', 'zec', 'zatoshi'), '10000000'); }); it('should work with any capitalization', () => { assert.equal(Units.convertZEC('1', 'ZeC', 'ZAtoShI'), '100000000'); }); }); });