Skip to content

Commit

Permalink
Merge pull request #1 from rawagner/fix_units_tests
Browse files Browse the repository at this point in the history
Fix units tests
  • Loading branch information
vojtechszocs authored and rawagner committed Aug 15, 2019
2 parents 7524302 + f385db6 commit 25dbfce
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 65 deletions.
116 changes: 59 additions & 57 deletions frontend/__tests__/units.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as _ from 'lodash-es';

import { units, validate, convertToBaseValue } from '../public/components/utils/units';
import { units, validate, convertToBaseValue, humanizePercentage } from '../public/components/utils/units';

xdescribe('units', () => {
describe('units', () => {
describe('round', () => {
const testRound = (n, expected) => {
it(`${n} into ${expected}`, () => {
Expand All @@ -28,48 +28,50 @@ xdescribe('units', () => {
});

describe('should humanize numeric values', () => {
const test_ = (value, expected) => {
it(`${value} into ${expected}`, () => {
expect(units.humanize(value, 'numeric', false).string).toEqual(expected);
const test_ = (value, expectedValue, expectedString) => {
it(`${value} into ${expectedValue}/${expectedString}`, () => {
const humanized = units.humanize(value, 'numeric', false);
expect(humanized.value).toEqual(expectedValue);
expect(humanized.string).toEqual(expectedString); //string is always rounded
});
};

test_('banana', '0');
test_(-1, '-1');
test_(-0, '0');
test_(1/0, '0');
test_(-1/0, '0');
test_('100$', '0');
test_(Number.MIN_VALUE, '5e-324');
test_(0, '0');
test_(0.1234, '0.1234');
test_(NaN, '0');
test_(1, '1');
test_(12, '12');
test_(123, '123');
test_(123.123, '123.123');
test_(999.999, '999.999');
test_(1000, '1k');
test_(1001, '1.001k');
test_(1011, '1.011k');
test_(5123, '5.123k');
test_(10000, '10k');
test_(10234, '10.234k');
test_(100000, '100k');
test_(1000000, '1m');
test_(10000000, '10m');
test_(100000000, '100m');
test_(1000000000, '1b');
test_(10000000000, '10b');
test_(100000000000, '100b');
test_(1000000000000, '1000b');
test_(1000000000001, '1000.000000001b');
test_('banana', 0, '0');
test_(-1, -1, '-1');
test_(-0, -0, '0');
test_(1/0, 0, '0');
test_(-1/0, 0, '0');
test_('100$', 0, '0');
test_(Number.MIN_VALUE, Number.MIN_VALUE, '0');
test_(0, 0, '0');
test_(0.1234, 0.1234, '0.123');
test_(NaN, 0, '0');
test_(1, 1, '1');
test_(12, 12, '12');
test_(123, 123, '123');
test_(123.123, 123.123, '123.1');
test_(999.999, 999.999, '1,000');
test_(1000, 1, '1k');
test_(1001, 1.001, '1k');
test_(1011, 1.011, '1.01k');
test_(5123, 5.123, '5.12k');
test_(10000, 10, '10k');
test_(10234, 10.234, '10.23k');
test_(100000, 100, '100k');
test_(1000000, 1, '1m');
test_(10000000, 10, '10m');
test_(100000000, 100, '100m');
test_(1000000000, 1, '1b');
test_(10000000000, 10, '10b');
test_(100000000000, 100, '100b');
test_(1000000000000, 1000, '1,000b');
test_(1000000000001, 1000.000000001, '1,000b');
});

describe('should humanize percentage values', () => {
const test_ = (value, expected) => {
it(`${value} into ${expected}`, () => {
expect(units.humanize(value, 'percentage', false).string).toEqual(expected);
expect(humanizePercentage(value).string).toEqual(expected);
});
};

Expand All @@ -79,30 +81,30 @@ xdescribe('units', () => {
test_(1/0, '0%');
test_(-1/0, '0%');
test_('100$', '0%');
test_(Number.MIN_VALUE, '5e-324%');
test_(Number.MIN_VALUE, '0%');
test_(0, '0%');
test_(0.1234, '0.1234%');
test_(0.1234, '0.1%');
test_(NaN, '0%');
test_(1, '1%');
test_(12, '12%');
test_(123, '123%');
test_(123.123, '123.123%');
test_(999.999, '999.999%');
test_(123.123, '123.1%');
test_(999.999, '1,000%');
test_(1.000, '1%');
test_(1.001, '1.001%');
test_(1.011, '1.011%');
test_(5.123, '5.123%');
test_(1.001, '1%');
test_(1.011, '1%');
test_(5.123, '5.1%');
test_(10.000, '10%');
test_(10.234, '10.234%');
test_(10.234, '10.2%');
test_(100, '100%');
test_(1000, '1000%');
test_(10000, '10000%');
test_(100000, '100000%');
test_(1000000, '1000000%');
test_(10000000, '10000000%');
test_(100000000, '100000000%');
test_(1000000000, '1000000000%');
test_(1000000001, '1000000001%');
test_(1000, '1,000%');
test_(10000, '10,000%');
test_(100000, '100,000%');
test_(1000000, '1,000,000%');
test_(10000000, '10,000,000%');
test_(100000000, '100,000,000%');
test_(1000000000, '1,000,000,000%');
test_(1000000001, '1,000,000,001%');
});

describe('should humanize decimalBytes values', () => {
Expand Down Expand Up @@ -140,7 +142,7 @@ xdescribe('units', () => {
test_(1000000000000, '1 TB');
test_(1000000000000000, '1 PB');
test_(1000000000000000000, '1 EB');
test_(1000000000000000000000, '1000 EB');
test_(1000000000000000000000, '1,000 EB');
});

describe('should humanize binaryBytes values', () => {
Expand All @@ -163,8 +165,8 @@ xdescribe('units', () => {
test_(12, '12 B');
test_(123, '123 B');
test_(123.123, '123.1 B');
test_(999.999, '1000 B');
test_(1023, '1023 B');
test_(999.999, '1,000 B');
test_(1023, '1,023 B');
test_(1023.999, '1 KiB');
test_(1024, '1 KiB');
test_(1025, '1 KiB');
Expand Down Expand Up @@ -201,8 +203,8 @@ xdescribe('units', () => {
test_(12, '12 i');
test_(123, '123 i');
test_(123.123, '123.1 i');
test_(999.999, '1000 i');
test_(1023, '1023 i');
test_(999.999, '1,000 i');
test_(1023, '1,023 i');
test_(1023.999, '1 Ki');
test_(1024, '1 Ki');
test_(1025, '1 Ki');
Expand Down
31 changes: 23 additions & 8 deletions frontend/public/components/utils/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const getDefaultFractionDigits = value => {
return 1;
};

const round = units.round = (value, options) => {
const formatValue = (value, options) => {
const fractionDigits = getDefaultFractionDigits(value);
const {locales, ...rest} = _.defaults(options, {
maximumFractionDigits: fractionDigits,
Expand All @@ -134,6 +134,14 @@ const round = units.round = (value, options) => {
return Intl.NumberFormat(locales, rest).format(value);
};

const round = units.round = (value, fractionDigits) => {
if (!isFinite(value)) {
return 0;
}
const multiplier = Math.pow(10, fractionDigits || getDefaultFractionDigits(value));
return Math.round(value * multiplier) / multiplier;
};

const humanize = units.humanize = (value, typeName, useRound = false, initialUnit, preferredUnit) => {
const type = getType(typeName);

Expand All @@ -148,8 +156,10 @@ const humanize = units.humanize = (value, typeName, useRound = false, initialUni
converted = convertBaseValueToUnits(converted.value, type.units, type.divisor, converted.unit, preferredUnit);
}

const formattedValue = formatValue(converted.value);

return {
string: type.space ? `${converted.value} ${converted.unit}`: converted.value + converted.unit,
string: type.space ? `${formattedValue} ${converted.unit}`: formattedValue + converted.unit,
unit: converted.unit,
value: converted.value,
};
Expand All @@ -175,16 +185,21 @@ export const humanizeCpuCores = v => {
const value = v < 1 ? round(v*1000) : v;
const unit = v < 1 ? 'm' : '';
return {
string: `${value}${unit}`,
string: `${formatValue(value)}${unit}`,
unit,
value,
};
};
export const humanizePercentage = value => ({
string: formatPercentage(value/100),
unit: '%',
value,
});
export const humanizePercentage = value => {
if (!isFinite(value)) {
value = 0;
}
return {
string: formatPercentage(value/100),
unit: '%',
value: round(value, 1),
};
};

units.dehumanize = (value, typeName) => {
const type = getType(typeName);
Expand Down

0 comments on commit 25dbfce

Please sign in to comment.