X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=test%2Futils%2FUtils.test.ts;h=8804e577bb73a81c89fd80f2fa9640a547d6c015;hb=85cce27f97abb607fbc4a2c4c6b335680dd65c2b;hp=892f25a56462aea681eeda17aa737cf1150494c0;hpb=9bf0ef23c51160abc6866ad8d07eea85e308edb8;p=e-mobility-charging-stations-simulator.git diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 892f25a5..8804e577 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -1,3 +1,4 @@ +import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'; import { expect } from 'expect'; import { Constants } from '../../src/utils/Constants'; @@ -7,10 +8,14 @@ import { convertToDate, convertToFloat, convertToInt, + extractTimeSeriesValues, + formatDurationMilliSeconds, + formatDurationSeconds, generateUUID, getRandomFloat, getRandomInteger, hasOwnProp, + isArraySorted, isEmptyArray, isEmptyObject, isEmptyString, @@ -20,6 +25,7 @@ import { isNullOrUndefined, isObject, isUndefined, + isValidTime, roundTo, secureRandom, sleep, @@ -46,12 +52,46 @@ describe('Utils test suite', () => { expect(end - start).toBeGreaterThanOrEqual(1000); }); + it('Verify formatDurationMilliSeconds()', () => { + expect(formatDurationMilliSeconds(0)).toBe(''); + expect(formatDurationMilliSeconds(1000)).toBe('1 second'); + expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours'); + }); + + it('Verify formatDurationSeconds()', () => { + expect(formatDurationSeconds(0)).toBe(''); + expect(formatDurationSeconds(1)).toBe('1 second'); + expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours'); + }); + + 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); + }); + 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('1999-12-31T23:00:00.000Z')); + expect(convertToDate('-1')).toStrictEqual(new Date('2000-12-31T23:00:00.000Z')); 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); @@ -79,7 +119,7 @@ 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()', () => { @@ -100,7 +140,7 @@ 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()', () => { @@ -137,15 +177,15 @@ describe('Utils test suite', () => { expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER); expect(randomInteger).toBeLessThanOrEqual(0); expect(() => getRandomInteger(0, 1)).toThrowError( - 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1' + 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1', ); expect(() => getRandomInteger(-1)).toThrowError( - 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0' + 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0', ); expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( `The value of "max" is out of range. It must be <= ${ Constants.MAX_RANDOM_INTEGER + 1 - }. Received 281_474_976_710_656` + }. Received 281_474_976_710_656`, ); randomInteger = getRandomInteger(2, 1); expect(randomInteger).toBeGreaterThanOrEqual(1); @@ -180,13 +220,24 @@ describe('Utils test suite', () => { expect(randomFloat).not.toEqual(getRandomFloat()); expect(() => getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError( - new RangeError('Invalid interval') + new RangeError('Invalid interval'), ); randomFloat = getRandomFloat(0, -Number.MAX_VALUE); expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); expect(randomFloat).toBeLessThanOrEqual(0); }); + 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]); + }); + it('Verify isObject()', () => { expect(isObject('test')).toBe(false); expect(isObject(undefined)).toBe(false); @@ -208,28 +259,29 @@ describe('Utils test suite', () => { 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()', () => { @@ -360,11 +412,25 @@ describe('Utils test suite', () => { it('Verify isEmptyObject()', () => { expect(isEmptyObject({})).toBe(true); expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false); - expect(isEmptyObject(undefined)).toBe(false); - expect(isEmptyObject(null)).toBe(false); expect(isEmptyObject(new Map())).toBe(false); expect(isEmptyObject(new Set())).toBe(false); expect(isEmptyObject(new WeakMap())).toBe(false); expect(isEmptyObject(new WeakSet())).toBe(false); }); + + it('Verify isArraySorted()', () => { + expect( + isArraySorted([], (a, b) => { + return a - b; + }), + ).toBe(true); + expect( + isArraySorted([1], (a, b) => { + return a - b; + }), + ).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); + }); });