Skip to content

Commit

Permalink
Merge pull request #2542 from ashley-o0o/utilities/valueUnitsUT
Browse files Browse the repository at this point in the history
Unit Test Update for utilities/valueUnits
  • Loading branch information
openshift-merge-bot[bot] authored Mar 1, 2024
2 parents 2e7e9d5 + 13de7f2 commit bab4396
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
87 changes: 87 additions & 0 deletions frontend/src/utilities/__tests__/valueUnits.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,97 @@
import {
UnitOption,
splitValueUnit,
isCpuLimitEqual,
isMemoryLimitEqual,
isCpuLimitLarger,
isMemoryLimitLarger,
isLarger,
} from '~/utilities/valueUnits';

describe('splitValueUnit', () => {
const options: UnitOption[] = [
{
name: 'name',
unit: 'unit',
weight: 1,
},
];
it('should default back to base if unable to match a legit value', () => {
expect(splitValueUnit('', options)).toEqual([1, options[0]]);
});
it('should return the value and unit', () => {
expect(splitValueUnit('1unit', options)).toEqual([1, options[0]]);
expect(splitValueUnit('1name', options)).toEqual([1, options[0]]);
expect(splitValueUnit('1', options)).toEqual([1, options[0]]);
expect(splitValueUnit('1.5unit', options)).toEqual([1.5, options[0]]);
expect(splitValueUnit('1.5name', options)).toEqual([1.5, options[0]]);
expect(splitValueUnit('1.5', options)).toEqual([1.5, options[0]]);
});
});

describe('isLarger', () => {
const unit: UnitOption[] = [
{
name: 'name',
unit: 'lg',
weight: 100,
},
{
name: 'name',
unit: 'sm',
weight: 1,
},
];
it('should return true if when value1 is larger than value2', () => {
expect(isLarger('1000lg', '10sm', unit)).toBe(true);
});
it('should return false if when value1 is smaller than value2', () => {
expect(isLarger('10sm', '1000lg', unit)).toBe(false);
});
it('should return false if not equal, but of the same numeric value', () => {
expect(isLarger('10sm', '10lg', unit)).toBe(false);
});
it('should return false if when value1 is equal to value2', () => {
expect(isLarger('10sm', '10sm', unit)).toBe(false);
});
it('should return false if when value1 is undefined', () => {
expect(isLarger('', '10lg', unit)).toBe(false);
});
it('should return false if when value2 is undefined', () => {
expect(isLarger('10sm', '', unit)).toBe(false);
});
it('should return false if when value1 and value2 are undefined', () => {
expect(isLarger('', '', unit)).toBe(false);
});
});

describe('isCpuLimitLarger', () => {
it('should return false if both values are undefined', () => {
expect(isCpuLimitLarger(undefined, undefined)).toBe(false);
});
it('should return false if the first value is undefined', () => {
expect(isCpuLimitLarger(undefined, '1000m')).toBe(false);
});
it('should return false if the second value is undefined', () => {
expect(isCpuLimitLarger('1000m', undefined)).toBe(false);
});
it('should return false if the first value is not larger', () => {
expect(isCpuLimitLarger('1000m', '1000m')).toBe(false);
});
it('should return false if the first value is larger', () => {
expect(isCpuLimitLarger('1001m', '1000m')).toBe(false);
});
it('should return false if the first value is larger and the second value is a number', () => {
expect(isCpuLimitLarger('2', '1000m')).toBe(false);
});
it('should return false if the first value is larger and the second value is a number', () => {
expect(isCpuLimitLarger('2', '1000m')).toBe(false);
});
it('should return true if the first value is larger and the second value is a number', () => {
expect(isCpuLimitLarger('2', 1000)).toBe(true);
});
});

describe('isCpuLimitEqual', () => {
test('correctly compares non-undefined values', () => {
expect(isCpuLimitEqual('1', '1')).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utilities/valueUnits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export const splitValueUnit = (
options: UnitOption[],
): [value: number, unit: UnitOption] => {
const match = value.match(/^(\d*\.?\d*)(.*)$/);
if (!match) {
if (!(match && match[1])) {
// Unable to match a legit value -- default back to base
return [1, options[0]];
}
const newValue = Number(match[1]) || 1; // avoid NaN
const newValue = Number(match[1]);
const foundUnit = options.find((o) => o.unit === match[2]);
const newUnit = foundUnit || options[0]; // escape hatch -- unsure what the unit can be
return [newValue, newUnit];
Expand Down

0 comments on commit bab4396

Please sign in to comment.