test: improve isArraySorted() test
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
index 892f25a56462aea681eeda17aa737cf1150494c0..96fd18b42096a485871c34568c3e8cbce791f4bf 100644 (file)
@@ -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);
@@ -137,15 +171,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,7 +214,7 @@ 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);
@@ -360,11 +394,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<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
+    expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
+    expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
+  });
 });