testing library
by joplomacedo
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
JavaScript
function getPrecisionFromNum(num) {
const decimalPart = ("" + num).split(".")[1];
return decimalPart ? decimalPart.length : 0;
}
function forceOptionsToValidOptions({
max,
min,
precision,
step,
justControls
}) {
let internalOptions = {};
if (min >= max) {
console.warn('max cannot be equal or lower than min. max was converted to Infinity')
internalOptions.max = Infinity;
} else {
internalOptions.max = max;
}
// precision/max-min-step inconsistency
const precisionFromMax = getPrecisionFromNum(max);
const precisionFromMin = getPrecisionFromNum(min);
const precisionFromStep = getPrecisionFromNum(step);
const highestOfOptionsPrecision = Math.max(precisionFromMax, precisionFromMin);
if (highestOfOptionsPrecision > precision) {
console.warn('max, min or step have precision higher than defined precision. original precision value is ignoredd. new precision value is equal to the highest of these');
internalOptions.precision = highestOfOptionsPrecision;
} else {
internalOptions.precision = precision;
}
internalOptions = {
...internalOptions,
//keep em as they are
min,
step,
justControls
};
return internalOptions;
}
function multiplyText(text, times) {
let result = '';
for (let i = 0; i < times; i++) {
result += text;
}
return result;
}
function test(name, fn, tests) {
let title = `Testing: ${name}`;
let delimiter = multiplyText('#', title.length);
console.log(delimiter);
console.log('Test: ' + name);
tests.forEach((test, i) => {
const {
args,
expected
} = test;
const result = fn(...args);
const passed = _.isEqual(expected, result);
if (passed) {
console.log('passed: ', passed)
} else {
console.log(delimiter);
console.log('arguments: ', ...args);
console.log('result: ', result);
console.log('expected: ', expected);
console.error('passed: ', passed)
...