Skip to content

Commit

Permalink
feat: some unit tests for some more utility functions. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesudeep authored Jul 6, 2023
1 parent 76a5e49 commit 1ccc019
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 22 deletions.
2 changes: 1 addition & 1 deletion radialgauge_v2.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Maps a number between a certain range offset by minAllowed.
*
* @param {number} n The number to map to a range.
* @param {number} minAllowed The minimum allowed for the range.
* @param {number} maxAllowed The maximum allowed for the range.
* @param {number} min The minimum value for the number.
* @param {number} max The maximum value for the number.
* @return {number} A number within the projected range.
*/
export function mapBetween(n, minAllowed, maxAllowed, min, max) {
// TODO: This function has been refactored and moved into this file as is
// It has yet to be checked for accuracy and validation has to be added.
const numerator = ((maxAllowed - minAllowed) * (n - min));
const denominator = (max - min);
return (
numerator / denominator + minAllowed
);
}
26 changes: 26 additions & 0 deletions src/math.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { mapBetween } from './math';

describe('mapBetween', () => {
test('should map the number within the allowable range', () => {
expect(mapBetween(4, 0, 5, 1, 3)).toEqual(7.5);

expect(mapBetween(4, 2, 5, 1, 3)).toEqual(6.5);
expect(mapBetween(4, 0, 4, 1, 3)).toEqual(6);
expect(mapBetween(4, 0, 5, 4, 3)).toEqual(0);
});

test('should map the number within the allowable range', () => {
expect(mapBetween(1, 0, 5, 1, 3)).toEqual(0);
expect(mapBetween(2, 0, 5, 1, 3)).toEqual(2.5);
expect(mapBetween(3, 0, 5, 1, 3)).toEqual(5);
expect(mapBetween(4, 0, 5, 1, 3)).toEqual(7.5);
expect(mapBetween(5, 0, 5, 1, 3)).toEqual(10);
});

// FIXME: Are we sure this function maps a number within an allowable range correctly?
test('should map the number within the allowable range (bad cases?)', () => {
expect(mapBetween(-2, 0, 5, 1, 3)).toEqual(-7.5);
expect(mapBetween(-1, 0, 5, 1, 3)).toEqual(-5);
expect(mapBetween(0, 0, 5, 1, 3)).toEqual(-2.5);
});
});
22 changes: 2 additions & 20 deletions src/radial_gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, {useEffect} from 'react';
import * as d3 from 'd3';
import SSF from 'ssf';
// import { configs } from 'eslint-plugin-prettier';
import {mapBetween} from './math';
import {getLabel} from './string';

const RadialGauge = props => {
useEffect(() => {
Expand All @@ -10,12 +12,6 @@ const RadialGauge = props => {
return <div className="viz" />;
};

function mapBetween(currentNum, minAllowed, maxAllowed, min, max) {
return (
((maxAllowed - minAllowed) * (currentNum - min)) / (max - min) + minAllowed
);
}

function wrap(text, width) {
text.each(function () {
var text = d3.select(this),
Expand Down Expand Up @@ -51,20 +47,6 @@ function wrap(text, width) {
});
}

function getLabel(rule, value, label, override) {
// console.log(rule, value, label, override)
label = override === '' ? label : override;
if (rule === 'value') {
return `${value}`;
} else if (rule === 'label') {
return `${label}`;
} else if (rule === 'both') {
return `${value} ${label}`;
} else if (rule === 'none') {
return ``;
}
}

const drawRadial = props => {
let limiting_aspect = props.w < props.h ? 'vw' : 'vh';
let radius = 0.4 * Math.min(props.w, props.h);
Expand Down
24 changes: 24 additions & 0 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ export function trimSpecialCharacters(
) {
return str.replace(pattern, '$1$2');
}

/**
* Determines the label given a rule, a value, an existing label and an override.
*
* @param {string} rule The rule to match.
* @param {string} value The value to be used if the rule is mean to use a value.
* @param {string} label The label if the rule is meant to use a label.
* @param {string} override An override for the label.
* @return {string} The label based on the rule.
*/
export function getLabel(rule, value, label, override) {
label = override || label;
switch(rule) {
case 'value':
return `${value}`;
case 'label':
return `${label}`;
case 'both':
return `${value} ${label}`;
case 'none':
default:
return ``;
}
}
24 changes: 23 additions & 1 deletion src/string.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {trimSpecialCharacters} from './string';
import {getLabel, trimSpecialCharacters} from './string';

describe('trimSpecialCharacters', () => {
test('should trim leading special characters', () => {
Expand All @@ -14,3 +14,25 @@ describe('trimSpecialCharacters', () => {
expect(trimSpecialCharacters('')).toBe('');
});
});

describe('getLabel', () => {
test('should return the label if the rule specifies so', () => {
expect(getLabel('label', 'foo', 'labelle', '')).toEqual('labelle');
});
test('should return the label override if the rule specifies so and one has been provided', () => {
expect(getLabel('label', 'foo', 'labelle', null)).toEqual('labelle');
expect(getLabel('label', 'foo', 'labelle', undefined)).toEqual('labelle');
expect(getLabel('label', 'foo', 'labelle', '')).toEqual('labelle');
expect(getLabel('label', 'foo', 'labelle', 'foobar')).toEqual('foobar');
});
test('should return the value if the rule specifies so', () => {
expect(getLabel('value', 'foo', 'labelle', '')).toEqual('foo');
});
test('should return the both value and label if the rule specifies so', () => {
expect(getLabel('both', 'foo', 'labelle', '')).toEqual('foo labelle');
});
test('should return empty string otherwise', () => {
expect(getLabel('none', 'foo', 'labelle', '')).toEqual('');
expect(getLabel('', 'foo', 'labelle', '')).toEqual('');
});
});

0 comments on commit 1ccc019

Please # to comment.