X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=test%2Futils%2FUtils.test.ts;h=5592e68f3800d40ea888af761b5a4d1e98fb48d8;hb=6c43b4416a202dd8f4cd62a3d141182fcf18e0e5;hp=233ee75d8baa886fcdd5e8920d7c58d544095a75;hpb=80c580411036965e34b983809b663b812ed36997;p=e-mobility-charging-stations-simulator.git diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 233ee75d..5592e68f 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -1,3 +1,6 @@ +import { describe, it } from 'node:test'; + +import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'; import { expect } from 'expect'; import { Constants } from '../../src/utils/Constants'; @@ -7,6 +10,9 @@ import { convertToDate, convertToFloat, convertToInt, + extractTimeSeriesValues, + formatDurationMilliSeconds, + formatDurationSeconds, generateUUID, getRandomFloat, getRandomInteger, @@ -21,14 +27,18 @@ import { isNullOrUndefined, isObject, isUndefined, + isValidTime, + max, + min, + once, roundTo, secureRandom, sleep, validateUUID, } from '../../src/utils/Utils'; -describe('Utils test suite', () => { - it('Verify generateUUID()/validateUUID()', () => { +await describe('Utils test suite', async () => { + await it('Verify generateUUID()/validateUUID()', () => { const uuid = generateUUID(); expect(uuid).toBeDefined(); expect(uuid.length).toEqual(36); @@ -40,19 +50,51 @@ describe('Utils test suite', () => { expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); }); - it('Verify sleep()', async () => { + await it('Verify sleep()', async () => { const start = performance.now(); await sleep(1000); - const end = performance.now(); - expect(end - start).toBeGreaterThanOrEqual(1000); + const stop = performance.now(); + expect(stop - start).toBeGreaterThanOrEqual(1000); + }); + + await it('Verify formatDurationMilliSeconds()', () => { + expect(formatDurationMilliSeconds(0)).toBe(''); + expect(formatDurationMilliSeconds(1000)).toBe('1 second'); + expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours'); + }); + + await it('Verify formatDurationSeconds()', () => { + expect(formatDurationSeconds(0)).toBe(''); + expect(formatDurationSeconds(1)).toBe('1 second'); + expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours'); }); - it('Verify convertToDate()', () => { + await it('Verify isValidTime()', () => { + expect(isValidTime(undefined)).toBe(false); + expect(isValidTime(null)).toBe(false); + expect(isValidTime('')).toBe(false); + expect(isValidTime({})).toBe(false); + expect(isValidTime([])).toBe(false); + expect(isValidTime(new Map())).toBe(false); + expect(isValidTime(new Set())).toBe(false); + expect(isValidTime(new WeakMap())).toBe(false); + expect(isValidTime(new WeakSet())).toBe(false); + expect(isValidTime(-1)).toBe(true); + expect(isValidTime(0)).toBe(true); + expect(isValidTime(1)).toBe(true); + expect(isValidTime(-0.5)).toBe(true); + expect(isValidTime(0.5)).toBe(true); + expect(isValidTime(new Date())).toBe(true); + }); + + await it('Verify convertToDate()', () => { expect(convertToDate(undefined)).toBe(undefined); - expect(convertToDate(null)).toBe(null); - const invalidDate = convertToDate(''); - expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); + expect(() => convertToDate('')).toThrowError(new Error("Cannot convert to date: ''")); + expect(() => convertToDate('00:70:61')).toThrowError( + new Error("Cannot convert to date: '00:70:61'"), + ); expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); + expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z')); const dateStr = '2020-01-01T00:00:00.000Z'; let date = convertToDate(dateStr); expect(date).toBeInstanceOf(Date); @@ -62,7 +104,7 @@ describe('Utils test suite', () => { expect(date).toStrictEqual(new Date(dateStr)); }); - it('Verify convertToInt()', () => { + await it('Verify convertToInt()', () => { expect(convertToInt(undefined)).toBe(0); expect(convertToInt(null)).toBe(0); expect(convertToInt(0)).toBe(0); @@ -80,10 +122,10 @@ describe('Utils test suite', () => { expect(convertToInt(1.999)).toBe(1); expect(() => { convertToInt('NaN'); - }).toThrow('Cannot convert to integer: NaN'); + }).toThrow("Cannot convert to integer: 'NaN'"); }); - it('Verify convertToFloat()', () => { + await it('Verify convertToFloat()', () => { expect(convertToFloat(undefined)).toBe(0); expect(convertToFloat(null)).toBe(0); expect(convertToFloat(0)).toBe(0); @@ -101,10 +143,10 @@ describe('Utils test suite', () => { expect(convertToFloat(1.999)).toBe(1.999); expect(() => { convertToFloat('NaN'); - }).toThrow('Cannot convert to float: NaN'); + }).toThrow("Cannot convert to float: 'NaN'"); }); - it('Verify convertToBoolean()', () => { + await it('Verify convertToBoolean()', () => { expect(convertToBoolean(undefined)).toBe(false); expect(convertToBoolean(null)).toBe(false); expect(convertToBoolean('true')).toBe(true); @@ -121,14 +163,14 @@ describe('Utils test suite', () => { expect(convertToBoolean('NoNBoolean')).toBe(false); }); - it('Verify secureRandom()', () => { + await it('Verify secureRandom()', () => { const random = secureRandom(); expect(typeof random === 'number').toBe(true); expect(random).toBeGreaterThanOrEqual(0); expect(random).toBeLessThan(1); }); - it('Verify getRandomInteger()', () => { + await it('Verify getRandomInteger()', () => { let randomInteger = getRandomInteger(); expect(Number.isSafeInteger(randomInteger)).toBe(true); expect(randomInteger).toBeGreaterThanOrEqual(0); @@ -151,14 +193,14 @@ describe('Utils test suite', () => { randomInteger = getRandomInteger(2, 1); expect(randomInteger).toBeGreaterThanOrEqual(1); expect(randomInteger).toBeLessThanOrEqual(2); - const max = 2.2, - min = 1.1; - randomInteger = getRandomInteger(max, min); - expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); - expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); + const maximum = 2.2, + minimum = 1.1; + randomInteger = getRandomInteger(maximum, minimum); + expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum)); + expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum)); }); - it('Verify roundTo()', () => { + await it('Verify roundTo()', () => { expect(roundTo(0, 2)).toBe(0); expect(roundTo(0.5, 0)).toBe(1); expect(roundTo(0.5, 2)).toBe(0.5); @@ -173,7 +215,7 @@ describe('Utils test suite', () => { expect(roundTo(-5.015, 2)).toBe(-5.02); }); - it('Verify getRandomFloat()', () => { + await it('Verify getRandomFloat()', () => { let randomFloat = getRandomFloat(); expect(typeof randomFloat === 'number').toBe(true); expect(randomFloat).toBeGreaterThanOrEqual(0); @@ -188,7 +230,18 @@ describe('Utils test suite', () => { expect(randomFloat).toBeLessThanOrEqual(0); }); - it('Verify isObject()', () => { + await it('Verify extractTimeSeriesValues()', () => { + expect(extractTimeSeriesValues([])).toEqual([]); + expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]); + expect( + extractTimeSeriesValues([ + { timestamp: Date.now(), value: 1.1 }, + { timestamp: Date.now(), value: 2.2 }, + ]), + ).toEqual([1.1, 2.2]); + }); + + await it('Verify isObject()', () => { expect(isObject('test')).toBe(false); expect(isObject(undefined)).toBe(false); expect(isObject(null)).toBe(false); @@ -205,35 +258,36 @@ describe('Utils test suite', () => { expect(isObject(new WeakSet())).toBe(true); }); - it('Verify cloneObject()', () => { + await it('Verify cloneObject()', () => { const obj = { 1: 1 }; expect(cloneObject(obj)).toStrictEqual(obj); expect(cloneObject(obj) === obj).toBe(false); + const nestedObj = { 1: obj, 2: obj }; + expect(cloneObject(nestedObj)).toStrictEqual(nestedObj); + expect(cloneObject(nestedObj) === nestedObj).toBe(false); const array = [1, 2]; expect(cloneObject(array)).toStrictEqual(array); expect(cloneObject(array) === array).toBe(false); + const objArray = [obj, obj]; + expect(cloneObject(objArray)).toStrictEqual(objArray); + expect(cloneObject(objArray) === objArray).toBe(false); const date = new Date(); expect(cloneObject(date)).toStrictEqual(date); expect(cloneObject(date) === date).toBe(false); const map = new Map([['1', '2']]); - expect(cloneObject(map)).toStrictEqual(map); - expect(cloneObject(map) === map).toBe(false); + expect(cloneObject(map)).toStrictEqual({}); const set = new Set(['1']); - expect(cloneObject(set)).toStrictEqual(set); - expect(cloneObject(set) === set).toBe(false); + expect(cloneObject(set)).toStrictEqual({}); // The URL object seems to have not enumerable properties const url = new URL('https://domain.tld'); - expect(cloneObject(url)).toStrictEqual(url); - expect(cloneObject(url) === url).toBe(true); + expect(cloneObject(url)).toStrictEqual({}); const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]); - expect(cloneObject(weakMap)).toStrictEqual(weakMap); - expect(cloneObject(weakMap) === weakMap).toBe(true); + expect(cloneObject(weakMap)).toStrictEqual({}); const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]); - expect(cloneObject(weakSet)).toStrictEqual(weakSet); - expect(cloneObject(weakSet) === weakSet).toBe(true); + expect(cloneObject(weakSet)).toStrictEqual({}); }); - it('Verify hasOwnProp()', () => { + await it('Verify hasOwnProp()', () => { expect(hasOwnProp('test', '')).toBe(false); expect(hasOwnProp(undefined, '')).toBe(false); expect(hasOwnProp(null, '')).toBe(false); @@ -249,7 +303,7 @@ describe('Utils test suite', () => { expect(hasOwnProp({ '1': '1' }, 2)).toBe(false); }); - it('Verify isIterable()', () => { + await it('Verify isIterable()', () => { expect(isIterable('')).toBe(true); expect(isIterable(' ')).toBe(true); expect(isIterable('test')).toBe(true); @@ -264,7 +318,7 @@ describe('Utils test suite', () => { expect(isIterable(new WeakSet())).toBe(false); }); - it('Verify isEmptyString()', () => { + await it('Verify isEmptyString()', () => { expect(isEmptyString('')).toBe(true); expect(isEmptyString(' ')).toBe(true); expect(isEmptyString(' ')).toBe(true); @@ -282,7 +336,7 @@ describe('Utils test suite', () => { expect(isEmptyString(new WeakSet())).toBe(false); }); - it('Verify isNotEmptyString()', () => { + await it('Verify isNotEmptyString()', () => { expect(isNotEmptyString('')).toBe(false); expect(isNotEmptyString(' ')).toBe(false); expect(isNotEmptyString(' ')).toBe(false); @@ -300,7 +354,7 @@ describe('Utils test suite', () => { expect(isNotEmptyString(new WeakSet())).toBe(false); }); - it('Verify isUndefined()', () => { + await it('Verify isUndefined()', () => { expect(isUndefined(undefined)).toBe(true); expect(isUndefined(null)).toBe(false); expect(isUndefined('')).toBe(false); @@ -313,7 +367,7 @@ describe('Utils test suite', () => { expect(isUndefined(new WeakSet())).toBe(false); }); - it('Verify isNullOrUndefined()', () => { + await it('Verify isNullOrUndefined()', () => { expect(isNullOrUndefined(undefined)).toBe(true); expect(isNullOrUndefined(null)).toBe(true); expect(isNullOrUndefined('')).toBe(false); @@ -326,7 +380,7 @@ describe('Utils test suite', () => { expect(isNullOrUndefined(new WeakSet())).toBe(false); }); - it('Verify isEmptyArray()', () => { + await it('Verify isEmptyArray()', () => { expect(isEmptyArray([])).toBe(true); expect(isEmptyArray([1, 2])).toBe(false); expect(isEmptyArray(['1', '2'])).toBe(false); @@ -342,7 +396,7 @@ describe('Utils test suite', () => { expect(isEmptyArray(new WeakSet())).toBe(false); }); - it('Verify isNotEmptyArray()', () => { + await it('Verify isNotEmptyArray()', () => { expect(isNotEmptyArray([])).toBe(false); expect(isNotEmptyArray([1, 2])).toBe(true); expect(isNotEmptyArray(['1', '2'])).toBe(true); @@ -358,7 +412,7 @@ describe('Utils test suite', () => { expect(isNotEmptyArray(new WeakSet())).toBe(false); }); - it('Verify isEmptyObject()', () => { + await it('Verify isEmptyObject()', () => { expect(isEmptyObject({})).toBe(true); expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false); expect(isEmptyObject(new Map())).toBe(false); @@ -367,7 +421,7 @@ describe('Utils test suite', () => { expect(isEmptyObject(new WeakSet())).toBe(false); }); - it('Verify isArraySorted()', () => { + await it('Verify isArraySorted()', () => { expect( isArraySorted([], (a, b) => { return a - b; @@ -380,5 +434,37 @@ describe('Utils test suite', () => { ).toBe(true); expect(isArraySorted([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true); expect(isArraySorted([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false); + expect(isArraySorted([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false); + }); + + await it('Verify once()', () => { + let called = 0; + const fn = () => ++called; + const onceFn = once(fn, this); + const result1 = onceFn(); + expect(called).toBe(1); + expect(result1).toBe(1); + const result2 = onceFn(); + expect(called).toBe(1); + expect(result2).toBe(1); + const result3 = onceFn(); + expect(called).toBe(1); + expect(result3).toBe(1); + }); + + await it('Verify min()', () => { + expect(min()).toBe(Infinity); + expect(min(0, 1)).toBe(0); + expect(min(1, 0)).toBe(0); + expect(min(0, -1)).toBe(-1); + expect(min(-1, 0)).toBe(-1); + }); + + await it('Verify max()', () => { + expect(max()).toBe(-Infinity); + expect(max(0, 1)).toBe(1); + expect(max(1, 0)).toBe(1); + expect(max(0, -1)).toBe(0); + expect(max(-1, 0)).toBe(0); }); });