2018-10-30 16:28:31 -04:00
|
|
|
import {
|
|
|
|
formatRelevantDigits,
|
|
|
|
bytesToKiB,
|
|
|
|
bytesToMiB,
|
|
|
|
bytesToGiB,
|
|
|
|
numberToHumanSize,
|
2019-03-01 05:22:46 -05:00
|
|
|
sum,
|
2018-10-30 16:28:31 -04:00
|
|
|
} from '~/lib/utils/number_utils';
|
2017-04-03 11:37:08 -04:00
|
|
|
|
|
|
|
describe('Number Utils', () => {
|
|
|
|
describe('formatRelevantDigits', () => {
|
|
|
|
it('returns an empty string when the number is NaN', () => {
|
|
|
|
expect(formatRelevantDigits('fail')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 4 decimals when there is 4 plus digits to the left', () => {
|
|
|
|
const formattedNumber = formatRelevantDigits('1000.1234567');
|
|
|
|
const rightFromDecimal = formattedNumber.split('.')[1];
|
|
|
|
const leftFromDecimal = formattedNumber.split('.')[0];
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-04-03 11:37:08 -04:00
|
|
|
expect(rightFromDecimal.length).toBe(4);
|
|
|
|
expect(leftFromDecimal.length).toBe(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 3 decimals when there is 1 digit to the left', () => {
|
|
|
|
const formattedNumber = formatRelevantDigits('0.1234567');
|
|
|
|
const rightFromDecimal = formattedNumber.split('.')[1];
|
|
|
|
const leftFromDecimal = formattedNumber.split('.')[0];
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-04-03 11:37:08 -04:00
|
|
|
expect(rightFromDecimal.length).toBe(3);
|
|
|
|
expect(leftFromDecimal.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 2 decimals when there is 2 digits to the left', () => {
|
|
|
|
const formattedNumber = formatRelevantDigits('10.1234567');
|
|
|
|
const rightFromDecimal = formattedNumber.split('.')[1];
|
|
|
|
const leftFromDecimal = formattedNumber.split('.')[0];
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-04-03 11:37:08 -04:00
|
|
|
expect(rightFromDecimal.length).toBe(2);
|
|
|
|
expect(leftFromDecimal.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 1 decimal when there is 3 digits to the left', () => {
|
|
|
|
const formattedNumber = formatRelevantDigits('100.1234567');
|
|
|
|
const rightFromDecimal = formattedNumber.split('.')[1];
|
|
|
|
const leftFromDecimal = formattedNumber.split('.')[0];
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-04-03 11:37:08 -04:00
|
|
|
expect(rightFromDecimal.length).toBe(1);
|
|
|
|
expect(leftFromDecimal.length).toBe(3);
|
|
|
|
});
|
|
|
|
});
|
2017-04-18 08:27:10 -04:00
|
|
|
|
|
|
|
describe('bytesToKiB', () => {
|
|
|
|
it('calculates KiB for the given bytes', () => {
|
|
|
|
expect(bytesToKiB(1024)).toEqual(1);
|
|
|
|
expect(bytesToKiB(1000)).toEqual(0.9765625);
|
|
|
|
});
|
|
|
|
});
|
2017-05-29 11:04:47 -04:00
|
|
|
|
|
|
|
describe('bytesToMiB', () => {
|
|
|
|
it('calculates MiB for the given bytes', () => {
|
|
|
|
expect(bytesToMiB(1048576)).toEqual(1);
|
|
|
|
expect(bytesToMiB(1000000)).toEqual(0.95367431640625);
|
|
|
|
});
|
|
|
|
});
|
2017-11-13 14:51:14 -05:00
|
|
|
|
|
|
|
describe('bytesToGiB', () => {
|
|
|
|
it('calculates GiB for the given bytes', () => {
|
|
|
|
expect(bytesToGiB(1073741824)).toEqual(1);
|
|
|
|
expect(bytesToGiB(10737418240)).toEqual(10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('numberToHumanSize', () => {
|
|
|
|
it('should return bytes', () => {
|
|
|
|
expect(numberToHumanSize(654)).toEqual('654 bytes');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return KiB', () => {
|
|
|
|
expect(numberToHumanSize(1079)).toEqual('1.05 KiB');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return MiB', () => {
|
|
|
|
expect(numberToHumanSize(10485764)).toEqual('10.00 MiB');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return GiB', () => {
|
|
|
|
expect(numberToHumanSize(10737418240)).toEqual('10.00 GiB');
|
|
|
|
});
|
|
|
|
});
|
2019-03-01 05:22:46 -05:00
|
|
|
|
|
|
|
describe('sum', () => {
|
|
|
|
it('should add up two values', () => {
|
|
|
|
expect(sum(1, 2)).toEqual(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add up all the values in an array when passed to a reducer', () => {
|
|
|
|
expect([1, 2, 3, 4, 5].reduce(sum)).toEqual(15);
|
|
|
|
});
|
|
|
|
});
|
2017-04-03 11:37:08 -04:00
|
|
|
});
|