From 6e7977386b1b53eef18e395ce73b566237679133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Tue, 28 Nov 2023 14:26:58 +0100 Subject: [PATCH] chore: simplify tests --- .../spacecat-shared-utils/src/functions.js | 14 +- .../test/functions.test.js | 229 ++++++++++-------- 2 files changed, 144 insertions(+), 99 deletions(-) diff --git a/packages/spacecat-shared-utils/src/functions.js b/packages/spacecat-shared-utils/src/functions.js index 1f61373b..5cc79ea0 100644 --- a/packages/spacecat-shared-utils/src/functions.js +++ b/packages/spacecat-shared-utils/src/functions.js @@ -10,6 +10,10 @@ * governing permissions and limitations under the License. */ +// Precompile regular expressions +const REGEX_ISO_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/; +const REGEX_TIME_OFFSET_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(Z|[+-]\d{2}:\d{2})/; + /** * Determines if the given value is a boolean or a string representation of a boolean. * @@ -89,8 +93,12 @@ function isValidDate(obj) { * @returns {boolean} True if the given string validates successfully. */ function isIsoDate(str) { - return /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str) - && new Date(str).toISOString() === str; + if (!REGEX_ISO_DATE.test(str)) { + return false; + } + + const date = new Date(str); + return isValidDate(date) && date.toISOString() === str; } /** @@ -101,7 +109,7 @@ function isIsoDate(str) { * @returns {boolean} True if the given string validates successfully. */ function isIsoTimeOffsetsDate(str) { - return /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(Z|[+-]\d{2}:\d{2})/.test(str); + return REGEX_TIME_OFFSET_DATE.test(str); } /** diff --git a/packages/spacecat-shared-utils/test/functions.test.js b/packages/spacecat-shared-utils/test/functions.test.js index d3b8fe63..96fb5036 100644 --- a/packages/spacecat-shared-utils/test/functions.test.js +++ b/packages/spacecat-shared-utils/test/functions.test.js @@ -33,19 +33,29 @@ import { describe('Shared functions', () => { describe('Commons', () => { it('is iso date', () => { - expect(isIsoDate('')).to.be.false; - expect(isIsoDate('2011-10-05')).to.be.false; - expect(isIsoDate('2011-10-05T14:48:00.000')).to.be.false; - expect(isIsoDate('Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)')).to.be.false; + const invalidDates = [ + '', + '2011-10-05', + '2011-10-05T14:48:00.000', + 'Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)', + '2011-13-01T14:48:00.000Z', + '2011-00-01T14:48:00.000Z', + ]; + + invalidDates.forEach((date) => expect(isIsoDate(date)).to.be.false); expect(isIsoDate('2011-10-05T14:48:00.000Z')).to.be.true; }); it('is iso date with time offset', () => { - expect(isIsoTimeOffsetsDate('')).to.be.false; - expect(isIsoTimeOffsetsDate('2019-11-15')).to.be.false; - expect(isIsoTimeOffsetsDate('2019-11-05T14:43:00.000')).to.be.false; - expect(isIsoTimeOffsetsDate('Wed Oct 11 2019 14:43:00 GMT+0200 (CEST)')).to.be.false; + const invalidOffsetDates = [ + '', + '2019-11-15', + '2019-11-05T14:43:00.000', + 'Wed Oct 11 2019 14:43:00 GMT+0200 (CEST)', + ]; + + invalidOffsetDates.forEach((date) => expect(isIsoTimeOffsetsDate(date)).to.be.false); expect(isIsoTimeOffsetsDate('2019-11-11T14:43:00.000Z')).to.be.true; expect(isIsoTimeOffsetsDate('2019-11-11T14:43:00.000-00:00')).to.be.true; @@ -53,15 +63,18 @@ describe('Shared functions', () => { }); it('has text', () => { - expect(hasText()).to.be.false; - expect(hasText(null)).to.be.false; - expect(hasText(undefined)).to.be.false; - expect(hasText(123)).to.be.false; - expect(hasText({})).to.be.false; - expect(hasText([])).to.be.false; - expect(hasText(['asd'])).to.be.false; - expect(hasText({ asd: 'dsa' })).to.be.false; - expect(hasText('')).to.be.false; + const invalidTexts = [ + null, + undefined, + 123, + [], + ['dasd'], + {}, + { asd: 'dsa' }, + '', + ]; + + invalidTexts.forEach((value) => expect(hasText(value)).to.be.false); expect(hasText('a')).to.be.true; expect(hasText('1')).to.be.true; @@ -69,17 +82,22 @@ describe('Shared functions', () => { }); it('is boolean', () => { - expect(isBoolean()).to.be.false; - expect(isBoolean(null)).to.be.false; - expect(isBoolean(undefined)).to.be.false; - expect(isBoolean([])).to.be.false; - expect(isBoolean('foo')).to.be.false; - expect(isBoolean({})).to.be.false; - expect(isBoolean(NaN)).to.be.false; - expect(isBoolean(Infinity)).to.be.false; - expect(isBoolean(-Infinity)).to.be.false; - expect(isBoolean(-Infinity)).to.be.false; - expect(isBoolean(123)).to.be.false; + const invalidBooleans = [ + null, + undefined, + [], + ['dasd'], + {}, + { asd: 'dsa' }, + '', + 'dasd', + NaN, + Infinity, + -Infinity, + 123, + ]; + + invalidBooleans.forEach((value) => expect(isBoolean(value)).to.be.false); expect(isBoolean('true')).to.be.true; expect(isBoolean('false')).to.be.true; @@ -88,18 +106,21 @@ describe('Shared functions', () => { }); it('is number', () => { - expect(isNumber()).to.be.false; - expect(isNumber(null)).to.be.false; - expect(isNumber(undefined)).to.be.false; - expect(isNumber([])).to.be.false; - expect(isNumber(['dasd'])).to.be.false; - expect(isNumber({})).to.be.false; - expect(isNumber({ asd: 'dsa' })).to.be.false; - expect(isNumber('')).to.be.false; - expect(isNumber('dasd')).to.be.false; - expect(isNumber(NaN)).to.be.false; - expect(isNumber(Infinity)).to.be.false; - expect(isNumber(-Infinity)).to.be.false; + const invalidNumbers = [ + null, + undefined, + [], + ['dasd'], + {}, + { asd: 'dsa' }, + '', + 'dasd', + NaN, + Infinity, + -Infinity, + ]; + + invalidNumbers.forEach((value) => expect(isNumber(value)).to.be.false); expect(isNumber(0)).to.be.true; expect(isNumber(123)).to.be.true; @@ -108,19 +129,22 @@ describe('Shared functions', () => { }); it('is integer', () => { - expect(isInteger()).to.be.false; - expect(isInteger(null)).to.be.false; - expect(isInteger(undefined)).to.be.false; - expect(isInteger([])).to.be.false; - expect(isInteger(['dasd'])).to.be.false; - expect(isInteger({})).to.be.false; - expect(isInteger({ asd: 'dsa' })).to.be.false; - expect(isInteger('')).to.be.false; - expect(isInteger('dasd')).to.be.false; - expect(isInteger(NaN)).to.be.false; - expect(isInteger(Infinity)).to.be.false; - expect(isInteger(-Infinity)).to.be.false; - expect(isInteger(12.3)).to.be.false; + const invalidIntegers = [ + null, + undefined, + [], + ['dasd'], + {}, + { asd: 'dsa' }, + '', + 'dasd', + NaN, + Infinity, + -Infinity, + 12.3, + ]; + + invalidIntegers.forEach((value) => expect(isInteger(value)).to.be.false); expect(isInteger(0)).to.be.true; expect(isInteger(123)).to.be.true; @@ -128,44 +152,52 @@ describe('Shared functions', () => { }); it('is object', () => { - expect(isObject()).to.be.false; - expect(isObject(null)).to.be.false; - expect(isObject(undefined)).to.be.false; - expect(isObject(123)).to.be.false; - expect(isObject('dasd')).to.be.false; - expect(isObject([])).to.be.false; - expect(isObject(['dasd'])).to.be.false; + const invalidObjects = [ + null, + undefined, + 123, + 'dasd', + [], + ['dasd'], + ]; + + invalidObjects.forEach((value) => expect(isObject(value)).to.be.false); expect(isObject({})).to.be.true; expect(isObject({ asd: 'dsa' })).to.be.true; }); it('is string', () => { - expect(isString()).to.be.false; - expect(isString(null)).to.be.false; - expect(isString(undefined)).to.be.false; - expect(isString(123)).to.be.false; - expect(isString([])).to.be.false; - expect(isString(['dasd'])).to.be.false; - expect(isString({})).to.be.false; - expect(isString({ asd: 'dsa' })).to.be.false; + const invalidStrings = [ + null, + undefined, + 123, + [], + ['dasd'], + {}, + { asd: 'dsa' }, + ]; + + invalidStrings.forEach((value) => expect(isString(value)).to.be.false); expect(isString('')).to.be.true; expect(isString('dasd')).to.be.true; }); it('toBoolean', () => { - expect(() => toBoolean()).to.throw('Not a boolean value'); - expect(() => toBoolean(null)).to.throw('Not a boolean value'); - expect(() => toBoolean(undefined)).to.throw('Not a boolean value'); - expect(() => toBoolean([])).to.throw('Not a boolean value'); - expect(() => toBoolean('foo')).to.throw('Not a boolean value'); - expect(() => toBoolean({})).to.throw('Not a boolean value'); - expect(() => toBoolean(NaN)).to.throw('Not a boolean value'); - expect(() => toBoolean(Infinity)).to.throw('Not a boolean value'); - expect(() => toBoolean(-Infinity)).to.throw('Not a boolean value'); - expect(() => toBoolean(-Infinity)).to.throw('Not a boolean value'); - expect(() => toBoolean(123)).to.throw('Not a boolean value'); + const invalidBooleans = [ + undefined, + null, + [], + 'foo', + {}, + NaN, + Infinity, + -Infinity, + 123, + ]; + + invalidBooleans.forEach((value) => expect(() => toBoolean(value)).to.throw(Error, 'Not a boolean value')); expect(toBoolean('true')).to.be.true; expect(toBoolean('false')).to.be.false; @@ -184,15 +216,18 @@ describe('Shared functions', () => { describe('isValidUrl', () => { it('returns false for invalid Url', async () => { - expect(isValidUrl(null)).to.be.false; - expect(isValidUrl(undefined)).to.be.false; - expect(isValidUrl('dummy')).to.be.false; - expect(isValidUrl(1234)).to.be.false; - expect(isValidUrl(true)).to.be.false; - expect(isValidUrl('example.com')).to.be.false; - expect(isValidUrl('www.example.com')).to.be.false; - expect(isValidUrl('255.255.255.256')).to.be.false; - expect(isValidUrl('ftp://abc.com')).to.be.false; + const invalidUrls = [ + null, + undefined, + 1234, + true, + 'example.com', + 'www.example.com', + '255.255.255.256', + 'ftp://abc.com', + ]; + + invalidUrls.forEach((url) => expect(isValidUrl(url)).to.be.false); }); it('returns true for valid url', async () => { @@ -203,14 +238,16 @@ describe('Shared functions', () => { describe('isValidDate', () => { it('returns false for invalid date', async () => { - expect(isValidDate(null)).to.be.false; - expect(isValidDate(undefined)).to.be.false; - expect(isValidDate({})).to.be.false; - expect(isValidDate([])).to.be.false; - expect(isValidDate(1234)).to.be.false; - expect(isValidDate('1234')).to.be.false; - expect(isValidDate(new Date('2019-11-11T14:43:89.000-00:00'))).to.be.false; - expect(isValidDate(new Date('invalid date'))).to.be.false; + const invalidDates = [ + null, + undefined, + 1234, + true, + '2019-11-11T14:43:89.000-00:00', + 'invalid date', + ]; + + invalidDates.forEach((date) => expect(isValidDate(date)).to.be.false); }); it('returns true for valid date', async () => {