test: switch to node.js test runner
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
index 0a672917641794060df5932b7999fbee5f0703d2..5592e68f3800d40ea888af761b5a4d1e98fb48d8 100644 (file)
@@ -1,4 +1,6 @@
-import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns';
+import { describe, it } from 'node:test';
+
+import { hoursToMilliseconds, hoursToSeconds } from 'date-fns';
 import { expect } from 'expect';
 
 import { Constants } from '../../src/utils/Constants';
@@ -26,14 +28,17 @@ import {
   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);
@@ -45,26 +50,26 @@ 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);
   });
 
-  it('Verify formatDurationMilliSeconds()', () => {
+  await 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()', () => {
+  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 isValidTime()', () => {
+  await it('Verify isValidTime()', () => {
     expect(isValidTime(undefined)).toBe(false);
     expect(isValidTime(null)).toBe(false);
     expect(isValidTime('')).toBe(false);
@@ -82,11 +87,14 @@ describe('Utils test suite', () => {
     expect(isValidTime(new Date())).toBe(true);
   });
 
-  it('Verify convertToDate()', () => {
+  await it('Verify convertToDate()', () => {
     expect(convertToDate(undefined)).toBe(undefined);
-    expect(convertToDate(null)).toBe(null);
-    expect(isValid(convertToDate(''))).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);
@@ -96,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);
@@ -114,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);
@@ -135,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);
@@ -155,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);
@@ -185,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);
@@ -207,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);
@@ -222,7 +230,7 @@ describe('Utils test suite', () => {
     expect(randomFloat).toBeLessThanOrEqual(0);
   });
 
-  it('Verify extractTimeSeriesValues()', () => {
+  await it('Verify extractTimeSeriesValues()', () => {
     expect(extractTimeSeriesValues([])).toEqual([]);
     expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]);
     expect(
@@ -233,7 +241,7 @@ describe('Utils test suite', () => {
     ).toEqual([1.1, 2.2]);
   });
 
-  it('Verify isObject()', () => {
+  await it('Verify isObject()', () => {
     expect(isObject('test')).toBe(false);
     expect(isObject(undefined)).toBe(false);
     expect(isObject(null)).toBe(false);
@@ -250,7 +258,7 @@ 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);
@@ -279,7 +287,7 @@ describe('Utils test suite', () => {
     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);
@@ -295,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);
@@ -310,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);
@@ -328,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);
@@ -346,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);
@@ -359,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);
@@ -372,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);
@@ -388,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);
@@ -404,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);
@@ -413,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;
@@ -428,4 +436,35 @@ describe('Utils test suite', () => {
     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);
   });
+
+  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);
+  });
 });