Skip to content

Commit

Permalink
test(utils): increase test coverage chunk 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Arturo Riveron Borodovisina committed Mar 28, 2022
1 parent 679d32e commit 9960a13
Show file tree
Hide file tree
Showing 15 changed files with 512 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export function objFilter<T, R>(obj : { [string] : T }, filter? : (T, ?string) =
const result = {};
for (const key in obj) {
if (!obj.hasOwnProperty(key) || !filter(obj[key], key)) {
if (!obj.hasOwnProperty(key) || (filter && !filter(obj[key], key))) {
continue;
}
Expand Down
28 changes: 28 additions & 0 deletions test/tests/util/awaitKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @flow */

import { awaitKey } from '../../../src';

describe('awaitKey cases', () => {
it('awaitKey should return the value when existing', () => {
const obj = {
custom: true
};
const result = awaitKey(obj, 'custom');

if (!result) {
throw new Error(`should return "true", but got: ${ result }`);
}
});

it('awaitKey should return the configured value when does not exists', () => {
const obj = {};

awaitKey(obj, 'custom');
obj.custom = 'result';
const result = obj.custom;

if (result !== 'result') {
throw new Error(`should return "result", but got: ${ result }`);
}
});
});
130 changes: 130 additions & 0 deletions test/tests/util/commons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* @flow */

import {
perc, min, max, roundUp, regexMap, svgToBase64,
objFilter, regexTokenize, camelToDasherize, dasherizeToCamel,
capitalizeFirstLetter, arrayFrom, isObject, isObjectObject
} from '../../../src/util';

describe('util cases', () => {
const sourceValues = [ 7, 30, 1 ];

it('perc', () => {
const result = perc(1000, 50);

if (result !== 500) {
throw new Error(`should return the value "500", but got: ${ result }`);
}
});

it('min', () => {
const result = min(...sourceValues);

if (result !== 1) {
throw new Error(`should return the minimum value "1", but got: ${ result }`);
}
});

it('max', () => {
const result = max(...sourceValues);

if (result !== 30) {
throw new Error(`should return the maximum value "30", but got: ${ result }`);
}
});

it('roundUp', () => {
const result = roundUp(10, 5);

if (result !== 10) {
throw new Error(`should return the roundUp value "10", but got: ${ result }`);
}
});

it('roundUp', () => {
const result = roundUp(10, 6);

if (result !== 12) {
throw new Error(`should return the roundUp value "12", but got: ${ result }`);
}
});

it('regexMap', () => {
const expectedResult = 'test';
// $FlowFixMe incompatible-call
const result = regexMap(expectedResult, /[a-z]*/);

if (result[0] !== expectedResult) {
throw new Error(`should get the value "${ expectedResult }", but got: ${ String(result) }`);
}
});

it('svgToBase64', () => {
const expectedResult = 'data:image/svg+xml;base64,YQ';
// $FlowFixMe incompatible-call
const result = svgToBase64('a');

if (result !== expectedResult) {
throw new Error(`should get the value "${ expectedResult }", but got: ${ String(result) }`);
}
});

it('objFilter', () => {
const result = objFilter({ value: true, value1: false }, value => value);

if (!result.value) {
throw new Error(`should get the value "true" from key, but got: ${ String(result) }`);
}
});

it('regexTokenize', () => {
const expectedResult = 'test';
const result = regexTokenize(expectedResult, /[a-z]+/);

if (result[0] !== expectedResult) {
throw new Error(`should get the value "${ expectedResult }" from key, but got: ${ String(result) }`);
}
});

it('camelToDasherize and dasherizeToCamel', () => {
const dasherize = camelToDasherize('TestCase');
const undasherize = dasherizeToCamel(dasherize);

if (dasherize !== '-test-case' || undasherize !== 'TestCase') {
throw new Error(`should dasherize and undasherize values, but got dasherize: ${ String(dasherize) } and undasherize: ${ undasherize }`);
}
});

it('capitalizeFirstLetter', () => {
const expectedResult = 'Test';
const result = capitalizeFirstLetter('test');

if (result !== expectedResult) {
throw new Error(`should return the value "${ expectedResult }", but got ${ String(result) }`);
}
});

it('arrayFrom', () => {
const result = arrayFrom([ 1, 2, 3 ]);

if (result.length !== 3) {
throw new Error(`should return an array with length "3", but got ${ String(result) }`);
}
});

it('isObject', () => {
const result = isObject({});

if (!result) {
throw new Error(`should return the value "true", but got ${ String(result) }`);
}
});

it('isObjectObject', () => {
const result = isObjectObject({});

if (!result) {
throw new Error(`should return the value "true", but got ${ String(result) }`);
}
});
});
38 changes: 38 additions & 0 deletions test/tests/util/deserializePrimitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* @flow */

import { deserializePrimitive } from '../../../src';

describe('deserializePrimitive cases', () => {
it('deserializePrimitive should return true', () => {
const result = deserializePrimitive('true');

if (result !== true) {
throw new Error(`should return "true", but got: ${ String(result) }`);
}
});

it('deserializePrimitive should return false', () => {
const result = deserializePrimitive('false');

if (result !== false) {
throw new Error(`should return "true", but got: ${ String(result) }`);
}
});

it('deserializePrimitive should return numeric value', () => {
const result = deserializePrimitive('10');

if (result !== 10) {
throw new Error(`should return "true", but got: ${ String(result) }`);
}
});


it('deserializePrimitive should return float value', () => {
const result = deserializePrimitive('10.57');

if (result !== 10.57) {
throw new Error(`should return "true", but got: ${ String(result) }`);
}
});
});
21 changes: 19 additions & 2 deletions test/tests/util/dotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { dotify, undotify } from '../../../src';
describe('dotify cases', () => {

it('should dotify and undotify to give the same result', () => {

const data = {
foo: 'bar',
baz: [ 1, 2, 3 ],
Expand All @@ -18,7 +17,8 @@ describe('dotify cases', () => {
},
zorg: 'zerg',
berk: 'me,erk'
}
},
func: () => true
};

const dotified = dotify(data);
Expand All @@ -28,4 +28,21 @@ describe('dotify cases', () => {
throw new Error(`Does not match. Original data:\n\n${ JSON.stringify(data, null, 4) }\n\nDotified:\n\n${ JSON.stringify(dotified, null, 4) }\n\nUndotified:\n\n${ JSON.stringify(undotified, null, 4) }`);
}
});

it('undotify should throw an error', () => {
const expectedErrorMessage = 'Disallowed key: constructor';
const data = {
'test': Object.prototype,
'constructor.part': 'error'
};

try {
// $FlowFixMe incompatible-type
undotify(data);
} catch (err) {
if (err.message !== expectedErrorMessage) {
throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ err.message }`);
}
}
});
});
22 changes: 22 additions & 0 deletions test/tests/util/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { extend } from '../../../src';

describe('extend cases', () => {

it('should return same object when second argument is empty', () => {
const result = extend({ a: true });
const arrayResult = Object.entries(result).flat();

if (arrayResult[0] !== 'a' || !arrayResult[1]) {
throw new Error(`should return the exact same first argument object, but got: ${ String(result) }`);
}
});

it('should add keys from one object to another', () => {
const obj1 : Object = {
'foo': 1,
Expand Down Expand Up @@ -31,4 +40,17 @@ describe('extend cases', () => {
throw new Error(`Expected obj1.bloop to equal 6, got ${ obj1.bloop }`);
}
});

it('should return the extend object when Object.assign is not valid', () => {
const originalFunc = Object.assign;
Reflect.deleteProperty(Object, 'assign');
const result = extend({ a: true }, { b: false });
const arrayResult = Object.entries(result).flat();

if (arrayResult[0] !== 'a' || !arrayResult[1] ||
arrayResult[2] !== 'b' || arrayResult[3]) {
throw new Error(`should return the extended object, but got: ${ String(result) }`);
}
Reflect.defineProperty(Object, 'assign', originalFunc);
});
});
38 changes: 38 additions & 0 deletions test/tests/util/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* @flow */

import { get } from '../../../src';

describe('get cases', () => {
const expectedResult = 10;
it('get should return default value', () => {
const result = get({}, '', expectedResult);

if (result !== expectedResult) {
throw new Error(`should return value "10", but got: ${ String(result) }`);
}
});

it('get should get deep keys', () => {
const result = get({ value: { result: expectedResult } }, 'value.result');

if (result !== expectedResult) {
throw new Error(`should return value "true", but got: ${ String(result) }`);
}
});

it('get should get deep keys', () => {
const result = get({ value: { result: expectedResult } }, 'value.result');

if (result !== expectedResult) {
throw new Error(`should return value "true", but got: ${ String(result) }`);
}
});

it('get should get deep keys with default value', () => {
const result = get({}, 'value.result', expectedResult);

if (result !== expectedResult) {
throw new Error(`should return value "true", but got: ${ String(result) }`);
}
});
});
11 changes: 11 additions & 0 deletions test/tests/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ import './extend';
import './serialize';
import './domainMatches';
import './identity';
import './stringify';
import './stringifyError';
import './stringifyErrorMessage';
import './isRegex';
import './isDefined';
import './base64encode';
import './patch';
import './match';
import './awayKey';
import './values';
import './commons';
import './promiseDebounce';
import './safeInterval';
import './deserializePrimitive';
import './get';
13 changes: 13 additions & 0 deletions test/tests/util/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @flow */

import { match } from '../../../src';

describe('match cases', () => {
it('match should return original function', () => {
const result = match('letters', /(t[a-z]*)/i);

if (result !== 'tters') {
throw new Error(`should return "tters", but got: ${ String(result) }`);
}
});
});
23 changes: 23 additions & 0 deletions test/tests/util/patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* @flow */

import { patchMethod } from '../../../src';

describe('patchMethod cases', () => {
it('patchMethod should return original function', () => {
const obj = {
custom() : string {
return 'first';
}
};
const handler = ({ callOriginal }) => {
return callOriginal();
};

patchMethod(obj, 'custom', handler);
const result = obj.custom();

if (result !== 'first') {
throw new Error(`should return "first", but got: ${ result }`);
}
});
});
Loading

0 comments on commit 9960a13

Please sign in to comment.