2017-11-12 19:45:52 +00:00
|
|
|
import { Wei } from 'libs/units';
|
2017-08-08 03:45:08 +00:00
|
|
|
import {
|
|
|
|
toFixedIfLarger,
|
|
|
|
formatNumber,
|
|
|
|
formatGasLimit
|
|
|
|
} from '../../common/utils/formatters';
|
2017-06-24 18:17:09 +00:00
|
|
|
|
|
|
|
describe('toFixedIfLarger', () => {
|
|
|
|
it('should return same value if decimal isnt longer than default', () => {
|
|
|
|
const numExample = 7.002;
|
|
|
|
expect(toFixedIfLarger(numExample)).toEqual(String(numExample));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return shortened value rounded up if decimal is longer than default', () => {
|
|
|
|
const numExample = 7.1234567;
|
|
|
|
expect(toFixedIfLarger(numExample)).toEqual(String(7.123457));
|
|
|
|
});
|
|
|
|
it('should return shortened value if decimal is longer than passed fixedSize', () => {
|
|
|
|
const numExample = 7.12345678;
|
|
|
|
expect(toFixedIfLarger(numExample, 2)).toEqual(String(7.12));
|
|
|
|
});
|
|
|
|
});
|
2017-07-27 23:21:50 +00:00
|
|
|
|
|
|
|
describe('formatNumber', () => {
|
|
|
|
const pairs = [
|
|
|
|
{
|
2017-11-12 19:45:52 +00:00
|
|
|
input: '0.0127491',
|
2017-10-25 02:17:26 +00:00
|
|
|
output: '0.013',
|
|
|
|
digits: undefined
|
2017-07-27 23:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
2017-11-12 19:45:52 +00:00
|
|
|
input: '21.87468421',
|
2017-10-25 02:17:26 +00:00
|
|
|
output: '21.875',
|
|
|
|
digits: undefined
|
2017-07-27 23:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
2017-11-12 19:45:52 +00:00
|
|
|
input: '0',
|
2017-10-25 02:17:26 +00:00
|
|
|
output: '0',
|
|
|
|
digits: undefined
|
2017-07-27 23:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
2017-11-12 19:45:52 +00:00
|
|
|
input: '354.4728173',
|
2017-07-27 23:21:50 +00:00
|
|
|
output: '354.4728',
|
|
|
|
digits: 4
|
|
|
|
},
|
|
|
|
{
|
2017-11-12 19:45:52 +00:00
|
|
|
input: '100.48391',
|
2017-07-27 23:21:50 +00:00
|
|
|
output: '100',
|
|
|
|
digits: 0
|
2017-11-12 19:45:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: '239.999632',
|
|
|
|
output: '240',
|
|
|
|
digits: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: '999.999',
|
|
|
|
output: '1,000',
|
|
|
|
digits: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: '0.9',
|
|
|
|
output: '1',
|
|
|
|
digits: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: '0.09',
|
|
|
|
output: '0.1',
|
|
|
|
digits: 1
|
2017-07-27 23:21:50 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
pairs.forEach(pair => {
|
2017-10-25 02:17:26 +00:00
|
|
|
const digits = pair.digits;
|
2017-11-12 19:45:52 +00:00
|
|
|
it(`should convert ${pair.input.toString()} to ${pair.output} when using ${
|
|
|
|
digits
|
|
|
|
} digits`, () => {
|
2017-07-27 23:21:50 +00:00
|
|
|
expect(formatNumber(pair.input, pair.digits)).toEqual(pair.output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
describe('formatGasLimit', () => {
|
|
|
|
it('should fix transaction gas limit off-by-one errors', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(formatGasLimit(Wei('21001'), 'ether')).toEqual('21000');
|
2017-08-08 03:45:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should mark the gas limit `-1` if you exceed the limit per block', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(formatGasLimit(Wei('999999999999999'), 'ether')).toEqual('-1');
|
2017-08-08 03:45:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not alter a valid gas limit', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(formatGasLimit(Wei('1234'))).toEqual('1234');
|
2017-08-08 03:45:08 +00:00
|
|
|
});
|
|
|
|
});
|