Skip to content

Commit

Permalink
chore: simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Nov 28, 2023
1 parent 0a303da commit 6e79773
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 99 deletions.
14 changes: 11 additions & 3 deletions packages/spacecat-shared-utils/src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
229 changes: 133 additions & 96 deletions packages/spacecat-shared-utils/test/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,71 @@ 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;
expect(isIsoTimeOffsetsDate('2019-11-11T14:43:00.000+05:11')).to.be.true;
});

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;
expect(hasText('a12dsamklda')).to.be.true;
});

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;
Expand All @@ -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;
Expand All @@ -108,64 +129,75 @@ 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;
expect(isInteger(-123)).to.be.true;
});

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;
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 6e79773

Please sign in to comment.