refactor: factor out ATG and charging profiles sanity checks
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
index e3b8cc3ee9be9a32155574e7a5f54a3b4c2b5503..67da558e326169b4969744dd1ac7a8d38efbf4d6 100644 (file)
@@ -1,3 +1,4 @@
+import { hoursToMilliseconds, hoursToSeconds } 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,6 +51,36 @@ 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);
@@ -360,11 +395,24 @@ 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);
+  });
 });