From a2111e8df05d41021f5f0def70af8a832d8555c8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 26 Oct 2022 00:02:25 +0200 Subject: [PATCH] Add Utils unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 8 ++-- test/utils/UtilsTest.ts | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 test/utils/UtilsTest.ts diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 1d422af2..719200fa 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -185,17 +185,17 @@ export default class Utils { } public static isEmptyArray(object: unknown): boolean { - if (!object) { - return true; + if (!Array.isArray(object)) { + return false; } - if (Array.isArray(object) === true && (object as unknown[]).length > 0) { + if ((object as unknown[]).length > 0) { return false; } return true; } public static isEmptyObject(obj: object): boolean { - if (obj.constructor !== Object) { + if (obj?.constructor !== Object) { return false; } // Iterates over the keys of an object, if diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts new file mode 100644 index 00000000..bc260cee --- /dev/null +++ b/test/utils/UtilsTest.ts @@ -0,0 +1,94 @@ +import expect from 'expect'; + +import Utils from '../../src/utils/Utils'; + +describe('Utils test suite', () => { + it('Verify secureRandom()', () => { + const random = Utils.secureRandom(); + expect(typeof random === 'number').toBe(true); + expect(random).toBeGreaterThanOrEqual(0); + expect(random).toBeLessThan(1); + }); + + it('Verify getRandomInteger()', () => { + const randomInteger = Utils.getRandomInteger(); + expect(Number.isSafeInteger(randomInteger)).toBe(true); + expect(randomInteger).toBeGreaterThanOrEqual(0); + expect(randomInteger).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER); + expect(() => Utils.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval')); + expect(() => Utils.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval')); + expect(() => Utils.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval')); + }); + + it('Verify getRandomFloat()', () => { + const randomFloat = Utils.getRandomFloat(); + expect(typeof randomFloat === 'number').toBe(true); + expect(randomFloat).toBeGreaterThanOrEqual(0); + expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); + expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); + expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); + expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); + }); + + it('Verify isIterable()', () => { + expect(Utils.isIterable('')).toBe(false); + expect(Utils.isIterable(' ')).toBe(true); + expect(Utils.isIterable('test')).toBe(true); + expect(Utils.isIterable(null)).toBe(false); + expect(Utils.isIterable(undefined)).toBe(false); + expect(Utils.isIterable(0)).toBe(false); + expect(Utils.isIterable({})).toBe(false); + expect(Utils.isIterable({ 1: 1 })).toBe(false); + expect(Utils.isIterable([])).toBe(true); + expect(Utils.isIterable(new Map())).toBe(true); + expect(Utils.isIterable(new Set())).toBe(true); + }); + + it('Verify isEmptyString()', () => { + expect(Utils.isEmptyString('')).toBe(true); + expect(Utils.isEmptyString(' ')).toBe(true); + expect(Utils.isEmptyString(' ')).toBe(true); + expect(Utils.isEmptyString('test')).toBe(false); + expect(Utils.isEmptyString(' test')).toBe(false); + expect(Utils.isEmptyString('test ')).toBe(false); + expect(Utils.isEmptyString(null)).toBe(false); + expect(Utils.isEmptyString(undefined)).toBe(false); + expect(Utils.isEmptyString(0)).toBe(false); + expect(Utils.isEmptyString({})).toBe(false); + expect(Utils.isEmptyString([])).toBe(false); + expect(Utils.isEmptyString(new Map())).toBe(false); + expect(Utils.isEmptyString(new Set())).toBe(false); + }); + + it('Verify isNullOrUndefined()', () => { + expect(Utils.isNullOrUndefined(null)).toBe(true); + expect(Utils.isNullOrUndefined(undefined)).toBe(true); + expect(Utils.isNullOrUndefined('')).toBe(false); + expect(Utils.isNullOrUndefined(0)).toBe(false); + expect(Utils.isNullOrUndefined({})).toBe(false); + expect(Utils.isNullOrUndefined([])).toBe(false); + expect(Utils.isNullOrUndefined(new Map())).toBe(false); + expect(Utils.isNullOrUndefined(new Set())).toBe(false); + }); + + it('Verify isEmptyArray()', () => { + expect(Utils.isEmptyArray([])).toBe(true); + expect(Utils.isEmptyArray([1, 2])).toBe(false); + expect(Utils.isEmptyArray(null)).toBe(false); + expect(Utils.isEmptyArray(undefined)).toBe(false); + expect(Utils.isEmptyArray('')).toBe(false); + expect(Utils.isEmptyArray(0)).toBe(false); + expect(Utils.isEmptyArray({})).toBe(false); + expect(Utils.isEmptyArray(new Map())).toBe(false); + expect(Utils.isEmptyArray(new Set())).toBe(false); + }); + + it('Verify isEmptyObject()', () => { + expect(Utils.isEmptyObject({})).toBe(true); + expect(Utils.isEmptyObject(null)).toBe(false); + expect(Utils.isEmptyObject(undefined)).toBe(false); + expect(Utils.isEmptyObject(new Map())).toBe(false); + expect(Utils.isEmptyObject(new Set())).toBe(false); + expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); + }); +}); -- 2.34.1