From: Jérôme Benoit Date: Wed, 26 Oct 2022 12:10:51 +0000 (+0200) Subject: Improve Utils unit tests X-Git-Tag: v1.1.86~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=45999aab924e2662784f4c7b0fbf5d4622a18d92;p=e-mobility-charging-stations-simulator.git Improve Utils unit tests Signed-off-by: Jérôme Benoit --- 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); }); });