From 64ee31999c5acff1aa6ff19bfee0638e0dad9c4b Mon Sep 17 00:00:00 2001 From: Arturo Riveron Borodovisina Date: Wed, 16 Mar 2022 15:12:03 +0100 Subject: [PATCH] test(utils): increase the unit test coverage --- test/tests/util/awayKey.js | 28 ++++++++ test/tests/util/extend.js | 22 ++++++ test/tests/util/index.js | 6 ++ test/tests/util/match.js | 13 ++++ test/tests/util/patch.js | 23 +++++++ test/tests/util/stringify.js | 13 ++++ test/tests/util/stringifyError.js | 108 ++++++++++++++++++++++++++++++ test/tests/util/values.js | 25 +++++++ 8 files changed, 238 insertions(+) create mode 100644 test/tests/util/awayKey.js create mode 100644 test/tests/util/match.js create mode 100644 test/tests/util/patch.js create mode 100644 test/tests/util/stringify.js create mode 100644 test/tests/util/stringifyError.js create mode 100644 test/tests/util/values.js diff --git a/test/tests/util/awayKey.js b/test/tests/util/awayKey.js new file mode 100644 index 00000000..ccd82121 --- /dev/null +++ b/test/tests/util/awayKey.js @@ -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 }`); + } + }); +}); diff --git a/test/tests/util/extend.js b/test/tests/util/extend.js index 28036e02..9e84e88e 100644 --- a/test/tests/util/extend.js +++ b/test/tests/util/extend.js @@ -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, @@ -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); + }); }); diff --git a/test/tests/util/index.js b/test/tests/util/index.js index db9658a8..1b0ddc87 100644 --- a/test/tests/util/index.js +++ b/test/tests/util/index.js @@ -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'; diff --git a/test/tests/util/match.js b/test/tests/util/match.js new file mode 100644 index 00000000..4aeaa8fb --- /dev/null +++ b/test/tests/util/match.js @@ -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) }`); + } + }); +}); diff --git a/test/tests/util/patch.js b/test/tests/util/patch.js new file mode 100644 index 00000000..a07ec47e --- /dev/null +++ b/test/tests/util/patch.js @@ -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 }`); + } + }); +}); diff --git a/test/tests/util/stringify.js b/test/tests/util/stringify.js new file mode 100644 index 00000000..d3b09a8f --- /dev/null +++ b/test/tests/util/stringify.js @@ -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 }`); + } + }); +}); diff --git a/test/tests/util/stringifyError.js b/test/tests/util/stringifyError.js new file mode 100644 index 00000000..53f3dfd8 --- /dev/null +++ b/test/tests/util/stringifyError.js @@ -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 = ``; + 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.`; + 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 }`); + } + }); +}); diff --git a/test/tests/util/values.js b/test/tests/util/values.js new file mode 100644 index 00000000..238676a0 --- /dev/null +++ b/test/tests/util/values.js @@ -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); + }); +});