Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase unit test coverage in the utils file 🧪 #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/tests/util/awayKey.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 }`);
}
});
});
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);
});
});
6 changes: 6 additions & 0 deletions test/tests/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ 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';
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 }`);
}
});
});
13 changes: 13 additions & 0 deletions test/tests/util/stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @flow */

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

describe('stringify cases', () => {
it('stringify should return the exact same value when is a string value', () => {
const result = stringify('1');

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

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

describe('stringifyError cases', () => {
it('stringifyError should return stack overflow error message', () => {
const expectedResult = 'stringifyError stack overflow';
const result = stringifyError('custom error', 4);

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

it('stringifyError should return unknown error message', () => {
const expectedResult = `<unknown error: [object Number]>`;
const result = stringifyError(0, 1);

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

it('stringifyError should return the exact same error message when is a string type', () => {
const expectedResult = `my error`;
const result = stringifyError(expectedResult, 1);

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

it('stringifyError should return only the stack when is an Error instance', () => {
const expectedResult = `custom`;
const error = new Error(expectedResult);
const result = stringifyError(error, 1);

if (!result.startsWith(`Error: ${ expectedResult }`)) {
throw new Error(`should throw the error message starting with "Error: ${ expectedResult }", but got: ${ result }`);
}
});

it('stringifyError should return the only the stack when is an Error instance with empty message', () => {
const expectedResult = `at Context.<anonymous>`;
const error = new Error('anything not important');

error.message = '';
const result = stringifyError(error, 1);

if (!result.includes(expectedResult)) {
throw new Error(`should throw the error message starting with "${ expectedResult }", but got: ${ result }`);
}
});

it('stringifyError should return the only the message when is an Error instance with empty stack', () => {
const expectedResult = `error instance`;
const error = new Error(expectedResult);

error.stack = '';
const result = stringifyError(error, 1);

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

it('stringifyError should return the message and stack when is and Error instance and message is not include in the stack', () => {
const expectedErrorMessage = 'Error: custom at line whatever';
const error = new Error('custom');

error.message = 'message';
error.stack = expectedErrorMessage;
const result = stringifyError(error, 1);

if (!result.endsWith(expectedErrorMessage)) {
throw new Error(`should throw the error message ending with "${ expectedErrorMessage }", but got: ${ result }`);
}
});

it('stringifyError should return call toString when error message is an object', () => {
const expectedErrorMessage = '[object Object]';
const result = stringifyError({}, 1);

if (result !== expectedErrorMessage) {
throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ result }`);
}
});

it('stringifyError should return call toString from Object.prototype when error message is object', () => {
const expectedErrorMessage = '[object Object]';
const result = stringifyError({ toString: null }, 1);

if (result !== expectedErrorMessage) {
throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ result }`);
}
});

it('stringifyError should handle error when something when wrong', () => {
const expectedErrorMessage = 'Error while stringifying error';
const result = stringifyError({ toString: () => {
throw new Error('unexpected error');
} }, 2);

if (!result.startsWith(expectedErrorMessage)) {
throw new Error(`should throw the error message starting wit "${ expectedErrorMessage }", but got: ${ result }`);
}
});
});
25 changes: 25 additions & 0 deletions test/tests/util/values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* @flow */

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

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

it('should return object values when Object.values is available', () => {
const result = values({ a: true });

if (!result[0]) {
throw new Error(`should return the value from the original object, but got: ${ String(result) }`);
}
});

it('should return object values when Object.values is unavailable', () => {
const originalFunc = Object.values;
Reflect.deleteProperty(Object, 'values');
const result = values({ a: true });

if (!result[0]) {
throw new Error(`should return the value from the original object, but got: ${ String(result) }`);
}
Reflect.defineProperty(Object, 'values', originalFunc);
});
});