From 45999aab924e2662784f4c7b0fbf5d4622a18d92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 26 Oct 2022 14:10:51 +0200 Subject: [PATCH] Improve 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 | 4 ++-- test/utils/UtilsTest.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index bc957c24..607843cd 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -186,11 +186,11 @@ export default class Utils { return value == null ? true : false; } - public static isEmptyArray(object: unknown): boolean { + public static isEmptyArray(object: unknown | unknown[]): boolean { if (!Array.isArray(object)) { return false; } - if ((object as unknown[]).length > 0) { + if (object.length > 0) { return false; } return true; diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index c7144e4d..bb919294 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -10,9 +10,18 @@ describe('Utils test suite', () => { expect(Utils.validateUUID(uuid)).toBe(true); expect(Utils.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); expect(Utils.validateUUID('')).toBe(false); + // Shall invalidate Nil UUID + expect(Utils.validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); expect(Utils.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); }); + it('Verify sleep()', async () => { + const start = Date.now(); + await Utils.sleep(1000); + const end = Date.now(); + expect(end - start).toBeGreaterThanOrEqual(1000); + }); + it('Verify secureRandom()', () => { const random = Utils.secureRandom(); expect(typeof random === 'number').toBe(true); @@ -70,6 +79,8 @@ describe('Utils test suite', () => { expect(Utils.isEmptyString([])).toBe(false); expect(Utils.isEmptyString(new Map())).toBe(false); expect(Utils.isEmptyString(new Set())).toBe(false); + expect(Utils.isEmptyString(new WeakMap())).toBe(false); + expect(Utils.isEmptyString(new WeakSet())).toBe(false); }); it('Verify isNullOrUndefined()', () => { @@ -81,6 +92,8 @@ describe('Utils test suite', () => { expect(Utils.isNullOrUndefined([])).toBe(false); expect(Utils.isNullOrUndefined(new Map())).toBe(false); expect(Utils.isNullOrUndefined(new Set())).toBe(false); + expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); + expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); }); it('Verify isEmptyArray()', () => { @@ -89,6 +102,7 @@ describe('Utils test suite', () => { expect(Utils.isEmptyArray(null)).toBe(false); expect(Utils.isEmptyArray(undefined)).toBe(false); expect(Utils.isEmptyArray('')).toBe(false); + expect(Utils.isEmptyArray('test')).toBe(false); expect(Utils.isEmptyArray(0)).toBe(false); expect(Utils.isEmptyArray({})).toBe(false); expect(Utils.isEmptyArray(new Map())).toBe(false); @@ -102,5 +116,7 @@ describe('Utils test suite', () => { expect(Utils.isEmptyObject(undefined)).toBe(false); expect(Utils.isEmptyObject(new Map())).toBe(false); expect(Utils.isEmptyObject(new Set())).toBe(false); + expect(Utils.isEmptyObject(new WeakMap())).toBe(false); + expect(Utils.isEmptyObject(new WeakSet())).toBe(false); }); }); -- 2.34.1