X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=test%2Futils%2FUtilsTest.ts;h=8093d5e759346baabfe634d0db575e863c8a65e0;hb=d439da5cfbb01ef12fcf9a46150715d91bf06c81;hp=7eb6aeb240363db2924eb1c2fbcf7e9764fe9036;hpb=04dda061d7b337390a0ee7b9d0bd41d4895fd4ca;p=e-mobility-charging-stations-simulator.git diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index 7eb6aeb2..8093d5e7 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -25,26 +25,22 @@ describe('Utils test suite', () => { it('Verify convertToDate()', () => { expect(Utils.convertToDate(undefined)).toBe(undefined); expect(Utils.convertToDate(null)).toBe(null); - expect(Utils.convertToDate(0)).toBe(0); + const invalidDate = Utils.convertToDate(''); + expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); + expect(Utils.convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); + expect(Utils.convertToDate([])).toBe(null); + expect(Utils.convertToDate({})).toBe(null); + expect(Utils.convertToDate(new Map())).toBe(null); + expect(Utils.convertToDate(new Set())).toBe(null); + expect(Utils.convertToDate(new WeakMap())).toBe(null); + expect(Utils.convertToDate(new WeakSet())).toBe(null); const dateStr = '2020-01-01T00:00:00.000Z'; let date = Utils.convertToDate(dateStr); expect(date).toBeInstanceOf(Date); - expect(date.getUTCFullYear()).toBe(2020); - expect(date.getUTCMonth()).toBe(0); - expect(date.getUTCDate()).toBe(1); - expect(date.getUTCHours()).toBe(0); - expect(date.getUTCMinutes()).toBe(0); - expect(date.getUTCSeconds()).toBe(0); - expect(date.getUTCMilliseconds()).toBe(0); + expect(date).toStrictEqual(new Date(dateStr)); date = Utils.convertToDate(new Date(dateStr)); expect(date).toBeInstanceOf(Date); - expect(date.getUTCFullYear()).toBe(2020); - expect(date.getUTCMonth()).toBe(0); - expect(date.getUTCDate()).toBe(1); - expect(date.getUTCHours()).toBe(0); - expect(date.getUTCMinutes()).toBe(0); - expect(date.getUTCSeconds()).toBe(0); - expect(date.getUTCMilliseconds()).toBe(0); + expect(date).toStrictEqual(new Date(dateStr)); }); it('Verify convertToInt()', () => { @@ -53,10 +49,12 @@ describe('Utils test suite', () => { expect(Utils.convertToInt(0)).toBe(0); const randomInteger = Utils.getRandomInteger(); expect(Utils.convertToInt(randomInteger)).toEqual(randomInteger); + expect(Utils.convertToInt('-1')).toBe(-1); expect(Utils.convertToInt('1')).toBe(1); expect(Utils.convertToInt('1.1')).toBe(1); expect(Utils.convertToInt('1.9')).toBe(1); expect(Utils.convertToInt('1.999')).toBe(1); + expect(Utils.convertToInt(-1)).toBe(-1); expect(Utils.convertToInt(1)).toBe(1); expect(Utils.convertToInt(1.1)).toBe(1); expect(Utils.convertToInt(1.9)).toBe(1); @@ -69,10 +67,12 @@ describe('Utils test suite', () => { expect(Utils.convertToFloat(0)).toBe(0); const randomFloat = Utils.getRandomFloat(); expect(Utils.convertToFloat(randomFloat)).toEqual(randomFloat); + expect(Utils.convertToFloat('-1')).toBe(-1); expect(Utils.convertToFloat('1')).toBe(1); expect(Utils.convertToFloat('1.1')).toBe(1.1); expect(Utils.convertToFloat('1.9')).toBe(1.9); expect(Utils.convertToFloat('1.999')).toBe(1.999); + expect(Utils.convertToFloat(-1)).toBe(-1); expect(Utils.convertToFloat(1)).toBe(1); expect(Utils.convertToFloat(1.1)).toBe(1.1); expect(Utils.convertToFloat(1.9)).toBe(1.9); @@ -92,6 +92,8 @@ describe('Utils test suite', () => { expect(Utils.convertToBoolean(0)).toBe(false); expect(Utils.convertToBoolean(true)).toBe(true); expect(Utils.convertToBoolean(false)).toBe(false); + expect(Utils.convertToBoolean('')).toBe(false); + expect(Utils.convertToBoolean('NoNBoolean')).toBe(false); }); it('Verify secureRandom()', () => { @@ -112,6 +114,11 @@ describe('Utils test suite', () => { randomInteger = Utils.getRandomInteger(2, 1); expect(randomInteger).toBeGreaterThanOrEqual(1); expect(randomInteger).toBeLessThanOrEqual(2); + const max = 2.2, + min = 1.1; + randomInteger = Utils.getRandomInteger(max, min); + expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); + expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); }); it('Verify getRandomFloat()', () => {