X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=test%2Futils%2FUtils.test.ts;h=96fd18b42096a485871c34568c3e8cbce791f4bf;hb=479200ffe080f9c6062ec4dc89db6b78f763db80;hp=4677fcdaa337c9a736d5b0e9df326eaf09b8cf11;hpb=e1d9a0f4d6ff1a90048e9a694fd12b7031cc6961;p=e-mobility-charging-stations-simulator.git diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 4677fcda..96fd18b4 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -1,3 +1,4 @@ +import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns'; import { expect } from 'expect'; import { Constants } from '../../src/utils/Constants'; @@ -7,10 +8,13 @@ import { convertToDate, convertToFloat, convertToInt, + formatDurationMilliSeconds, + formatDurationSeconds, generateUUID, getRandomFloat, getRandomInteger, hasOwnProp, + isArraySorted, isEmptyArray, isEmptyObject, isEmptyString, @@ -20,6 +24,7 @@ import { isNullOrUndefined, isObject, isUndefined, + isValidTime, roundTo, secureRandom, sleep, @@ -46,11 +51,40 @@ 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(isValid(convertToDate(''))).toBe(false); expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); const dateStr = '2020-01-01T00:00:00.000Z'; let date = convertToDate(dateStr); @@ -365,4 +399,20 @@ describe('Utils test suite', () => { 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); + }); });