test: code cleanup
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
CommitLineData
3dcf7b67 1import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns';
f64ca10c 2import { expect } from 'expect';
a2111e8d 3
878e026c 4import { Constants } from '../../src/utils/Constants';
9bf0ef23
JB
5import {
6 cloneObject,
7 convertToBoolean,
8 convertToDate,
9 convertToFloat,
10 convertToInt,
be4c6702 11 formatDurationMilliSeconds,
8053c842 12 formatDurationSeconds,
9bf0ef23
JB
13 generateUUID,
14 getRandomFloat,
15 getRandomInteger,
16 hasOwnProp,
80c58041 17 isArraySorted,
9bf0ef23
JB
18 isEmptyArray,
19 isEmptyObject,
20 isEmptyString,
21 isIterable,
22 isNotEmptyArray,
23 isNotEmptyString,
24 isNullOrUndefined,
25 isObject,
26 isUndefined,
0bd926c1 27 isValidTime,
9bf0ef23
JB
28 roundTo,
29 secureRandom,
30 sleep,
31 validateUUID,
32} from '../../src/utils/Utils';
a2111e8d 33
878e026c
JB
34describe('Utils test suite', () => {
35 it('Verify generateUUID()/validateUUID()', () => {
9bf0ef23 36 const uuid = generateUUID();
878e026c
JB
37 expect(uuid).toBeDefined();
38 expect(uuid.length).toEqual(36);
9bf0ef23
JB
39 expect(validateUUID(uuid)).toBe(true);
40 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
41 expect(validateUUID('')).toBe(false);
878e026c 42 // Shall invalidate Nil UUID
9bf0ef23
JB
43 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
44 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
878e026c 45 });
8bd02502 46
878e026c 47 it('Verify sleep()', async () => {
f204e9b4 48 const start = performance.now();
9bf0ef23 49 await sleep(1000);
f204e9b4 50 const end = performance.now();
878e026c
JB
51 expect(end - start).toBeGreaterThanOrEqual(1000);
52 });
45999aab 53
be4c6702
JB
54 it('Verify formatDurationMilliSeconds()', () => {
55 expect(formatDurationMilliSeconds(0)).toBe('');
56 expect(formatDurationMilliSeconds(1000)).toBe('1 second');
57 expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours');
58 });
59
8053c842
JB
60 it('Verify formatDurationSeconds()', () => {
61 expect(formatDurationSeconds(0)).toBe('');
62 expect(formatDurationSeconds(1)).toBe('1 second');
63 expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours');
64 });
65
0bd926c1
JB
66 it('Verify isValidTime()', () => {
67 expect(isValidTime(undefined)).toBe(false);
68 expect(isValidTime(null)).toBe(false);
69 expect(isValidTime('')).toBe(false);
70 expect(isValidTime({})).toBe(false);
71 expect(isValidTime([])).toBe(false);
72 expect(isValidTime(new Map())).toBe(false);
73 expect(isValidTime(new Set())).toBe(false);
74 expect(isValidTime(new WeakMap())).toBe(false);
75 expect(isValidTime(new WeakSet())).toBe(false);
76 expect(isValidTime(-1)).toBe(true);
77 expect(isValidTime(0)).toBe(true);
78 expect(isValidTime(1)).toBe(true);
79 expect(isValidTime(-0.5)).toBe(true);
80 expect(isValidTime(0.5)).toBe(true);
81 expect(isValidTime(new Date())).toBe(true);
ac8178a4
JB
82 });
83
878e026c 84 it('Verify convertToDate()', () => {
9bf0ef23
JB
85 expect(convertToDate(undefined)).toBe(undefined);
86 expect(convertToDate(null)).toBe(null);
3dcf7b67 87 expect(isValid(convertToDate(''))).toBe(false);
9bf0ef23 88 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
878e026c 89 const dateStr = '2020-01-01T00:00:00.000Z';
9bf0ef23 90 let date = convertToDate(dateStr);
878e026c
JB
91 expect(date).toBeInstanceOf(Date);
92 expect(date).toStrictEqual(new Date(dateStr));
9bf0ef23 93 date = convertToDate(new Date(dateStr));
878e026c
JB
94 expect(date).toBeInstanceOf(Date);
95 expect(date).toStrictEqual(new Date(dateStr));
96 });
df645d8f 97
878e026c 98 it('Verify convertToInt()', () => {
9bf0ef23
JB
99 expect(convertToInt(undefined)).toBe(0);
100 expect(convertToInt(null)).toBe(0);
101 expect(convertToInt(0)).toBe(0);
102 const randomInteger = getRandomInteger();
103 expect(convertToInt(randomInteger)).toEqual(randomInteger);
104 expect(convertToInt('-1')).toBe(-1);
105 expect(convertToInt('1')).toBe(1);
106 expect(convertToInt('1.1')).toBe(1);
107 expect(convertToInt('1.9')).toBe(1);
108 expect(convertToInt('1.999')).toBe(1);
109 expect(convertToInt(-1)).toBe(-1);
110 expect(convertToInt(1)).toBe(1);
111 expect(convertToInt(1.1)).toBe(1);
112 expect(convertToInt(1.9)).toBe(1);
113 expect(convertToInt(1.999)).toBe(1);
878e026c 114 expect(() => {
9bf0ef23 115 convertToInt('NaN');
878e026c
JB
116 }).toThrow('Cannot convert to integer: NaN');
117 });
df645d8f 118
878e026c 119 it('Verify convertToFloat()', () => {
9bf0ef23
JB
120 expect(convertToFloat(undefined)).toBe(0);
121 expect(convertToFloat(null)).toBe(0);
122 expect(convertToFloat(0)).toBe(0);
123 const randomFloat = getRandomFloat();
124 expect(convertToFloat(randomFloat)).toEqual(randomFloat);
125 expect(convertToFloat('-1')).toBe(-1);
126 expect(convertToFloat('1')).toBe(1);
127 expect(convertToFloat('1.1')).toBe(1.1);
128 expect(convertToFloat('1.9')).toBe(1.9);
129 expect(convertToFloat('1.999')).toBe(1.999);
130 expect(convertToFloat(-1)).toBe(-1);
131 expect(convertToFloat(1)).toBe(1);
132 expect(convertToFloat(1.1)).toBe(1.1);
133 expect(convertToFloat(1.9)).toBe(1.9);
134 expect(convertToFloat(1.999)).toBe(1.999);
878e026c 135 expect(() => {
9bf0ef23 136 convertToFloat('NaN');
878e026c
JB
137 }).toThrow('Cannot convert to float: NaN');
138 });
df645d8f 139
878e026c 140 it('Verify convertToBoolean()', () => {
9bf0ef23
JB
141 expect(convertToBoolean(undefined)).toBe(false);
142 expect(convertToBoolean(null)).toBe(false);
143 expect(convertToBoolean('true')).toBe(true);
144 expect(convertToBoolean('false')).toBe(false);
145 expect(convertToBoolean('TRUE')).toBe(true);
146 expect(convertToBoolean('FALSE')).toBe(false);
147 expect(convertToBoolean('1')).toBe(true);
148 expect(convertToBoolean('0')).toBe(false);
149 expect(convertToBoolean(1)).toBe(true);
150 expect(convertToBoolean(0)).toBe(false);
151 expect(convertToBoolean(true)).toBe(true);
152 expect(convertToBoolean(false)).toBe(false);
153 expect(convertToBoolean('')).toBe(false);
154 expect(convertToBoolean('NoNBoolean')).toBe(false);
878e026c 155 });
df645d8f 156
878e026c 157 it('Verify secureRandom()', () => {
9bf0ef23 158 const random = secureRandom();
878e026c
JB
159 expect(typeof random === 'number').toBe(true);
160 expect(random).toBeGreaterThanOrEqual(0);
161 expect(random).toBeLessThan(1);
162 });
a2111e8d 163
878e026c 164 it('Verify getRandomInteger()', () => {
9bf0ef23 165 let randomInteger = getRandomInteger();
878e026c
JB
166 expect(Number.isSafeInteger(randomInteger)).toBe(true);
167 expect(randomInteger).toBeGreaterThanOrEqual(0);
168 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER);
9bf0ef23
JB
169 expect(randomInteger).not.toEqual(getRandomInteger());
170 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER);
878e026c
JB
171 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER);
172 expect(randomInteger).toBeLessThanOrEqual(0);
9bf0ef23 173 expect(() => getRandomInteger(0, 1)).toThrowError(
5edd8ba0 174 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1',
878e026c 175 );
9bf0ef23 176 expect(() => getRandomInteger(-1)).toThrowError(
5edd8ba0 177 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0',
878e026c 178 );
9bf0ef23 179 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError(
878e026c
JB
180 `The value of "max" is out of range. It must be <= ${
181 Constants.MAX_RANDOM_INTEGER + 1
5edd8ba0 182 }. Received 281_474_976_710_656`,
878e026c 183 );
9bf0ef23 184 randomInteger = getRandomInteger(2, 1);
878e026c
JB
185 expect(randomInteger).toBeGreaterThanOrEqual(1);
186 expect(randomInteger).toBeLessThanOrEqual(2);
187 const max = 2.2,
188 min = 1.1;
9bf0ef23 189 randomInteger = getRandomInteger(max, min);
878e026c
JB
190 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min));
191 expect(randomInteger).toBeLessThanOrEqual(Math.floor(max));
192 });
a2111e8d 193
316d1564 194 it('Verify roundTo()', () => {
9bf0ef23
JB
195 expect(roundTo(0, 2)).toBe(0);
196 expect(roundTo(0.5, 0)).toBe(1);
197 expect(roundTo(0.5, 2)).toBe(0.5);
198 expect(roundTo(-0.5, 0)).toBe(-1);
199 expect(roundTo(-0.5, 2)).toBe(-0.5);
200 expect(roundTo(1.005, 0)).toBe(1);
201 expect(roundTo(1.005, 2)).toBe(1.01);
202 expect(roundTo(2.175, 2)).toBe(2.18);
203 expect(roundTo(5.015, 2)).toBe(5.02);
204 expect(roundTo(-1.005, 2)).toBe(-1.01);
205 expect(roundTo(-2.175, 2)).toBe(-2.18);
206 expect(roundTo(-5.015, 2)).toBe(-5.02);
316d1564
JB
207 });
208
878e026c 209 it('Verify getRandomFloat()', () => {
9bf0ef23 210 let randomFloat = getRandomFloat();
878e026c
JB
211 expect(typeof randomFloat === 'number').toBe(true);
212 expect(randomFloat).toBeGreaterThanOrEqual(0);
213 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
9bf0ef23
JB
214 expect(randomFloat).not.toEqual(getRandomFloat());
215 expect(() => getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval'));
216 expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError(
5edd8ba0 217 new RangeError('Invalid interval'),
878e026c 218 );
9bf0ef23 219 randomFloat = getRandomFloat(0, -Number.MAX_VALUE);
878e026c
JB
220 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
221 expect(randomFloat).toBeLessThanOrEqual(0);
222 });
a2111e8d 223
878e026c 224 it('Verify isObject()', () => {
9bf0ef23
JB
225 expect(isObject('test')).toBe(false);
226 expect(isObject(undefined)).toBe(false);
227 expect(isObject(null)).toBe(false);
228 expect(isObject(0)).toBe(false);
229 expect(isObject([])).toBe(false);
230 expect(isObject([0, 1])).toBe(false);
231 expect(isObject(['0', '1'])).toBe(false);
232 expect(isObject({})).toBe(true);
233 expect(isObject({ 1: 1 })).toBe(true);
234 expect(isObject({ '1': '1' })).toBe(true);
235 expect(isObject(new Map())).toBe(true);
236 expect(isObject(new Set())).toBe(true);
237 expect(isObject(new WeakMap())).toBe(true);
238 expect(isObject(new WeakSet())).toBe(true);
878e026c 239 });
a2111e8d 240
dd5a1d6c
JB
241 it('Verify cloneObject()', () => {
242 const obj = { 1: 1 };
9bf0ef23
JB
243 expect(cloneObject(obj)).toStrictEqual(obj);
244 expect(cloneObject(obj) === obj).toBe(false);
dd5a1d6c 245 const array = [1, 2];
9bf0ef23
JB
246 expect(cloneObject(array)).toStrictEqual(array);
247 expect(cloneObject(array) === array).toBe(false);
dd5a1d6c 248 const date = new Date();
9bf0ef23
JB
249 expect(cloneObject(date)).toStrictEqual(date);
250 expect(cloneObject(date) === date).toBe(false);
dd5a1d6c 251 const map = new Map([['1', '2']]);
9bf0ef23
JB
252 expect(cloneObject(map)).toStrictEqual(map);
253 expect(cloneObject(map) === map).toBe(false);
dd5a1d6c 254 const set = new Set(['1']);
9bf0ef23
JB
255 expect(cloneObject(set)).toStrictEqual(set);
256 expect(cloneObject(set) === set).toBe(false);
dd5a1d6c
JB
257 // The URL object seems to have not enumerable properties
258 const url = new URL('https://domain.tld');
9bf0ef23
JB
259 expect(cloneObject(url)).toStrictEqual(url);
260 expect(cloneObject(url) === url).toBe(true);
dd5a1d6c 261 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
9bf0ef23
JB
262 expect(cloneObject(weakMap)).toStrictEqual(weakMap);
263 expect(cloneObject(weakMap) === weakMap).toBe(true);
dd5a1d6c 264 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
9bf0ef23
JB
265 expect(cloneObject(weakSet)).toStrictEqual(weakSet);
266 expect(cloneObject(weakSet) === weakSet).toBe(true);
dd5a1d6c
JB
267 });
268
af29b598 269 it('Verify hasOwnProp()', () => {
9bf0ef23
JB
270 expect(hasOwnProp('test', '')).toBe(false);
271 expect(hasOwnProp(undefined, '')).toBe(false);
272 expect(hasOwnProp(null, '')).toBe(false);
273 expect(hasOwnProp([], '')).toBe(false);
274 expect(hasOwnProp({}, '')).toBe(false);
275 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true);
276 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true);
277 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false);
278 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false);
279 expect(hasOwnProp({ '1': '1' }, '1')).toBe(true);
280 expect(hasOwnProp({ '1': '1' }, 1)).toBe(true);
281 expect(hasOwnProp({ '1': '1' }, '2')).toBe(false);
282 expect(hasOwnProp({ '1': '1' }, 2)).toBe(false);
af29b598
JB
283 });
284
878e026c 285 it('Verify isIterable()', () => {
9bf0ef23
JB
286 expect(isIterable('')).toBe(true);
287 expect(isIterable(' ')).toBe(true);
288 expect(isIterable('test')).toBe(true);
289 expect(isIterable(undefined)).toBe(false);
290 expect(isIterable(null)).toBe(false);
291 expect(isIterable(0)).toBe(false);
292 expect(isIterable([0, 1])).toBe(true);
293 expect(isIterable({ 1: 1 })).toBe(false);
294 expect(isIterable(new Map())).toBe(true);
295 expect(isIterable(new Set())).toBe(true);
296 expect(isIterable(new WeakMap())).toBe(false);
297 expect(isIterable(new WeakSet())).toBe(false);
878e026c 298 });
a2111e8d 299
878e026c 300 it('Verify isEmptyString()', () => {
9bf0ef23
JB
301 expect(isEmptyString('')).toBe(true);
302 expect(isEmptyString(' ')).toBe(true);
303 expect(isEmptyString(' ')).toBe(true);
304 expect(isEmptyString('test')).toBe(false);
305 expect(isEmptyString(' test')).toBe(false);
306 expect(isEmptyString('test ')).toBe(false);
307 expect(isEmptyString(undefined)).toBe(true);
308 expect(isEmptyString(null)).toBe(true);
309 expect(isEmptyString(0)).toBe(false);
310 expect(isEmptyString({})).toBe(false);
311 expect(isEmptyString([])).toBe(false);
312 expect(isEmptyString(new Map())).toBe(false);
313 expect(isEmptyString(new Set())).toBe(false);
314 expect(isEmptyString(new WeakMap())).toBe(false);
315 expect(isEmptyString(new WeakSet())).toBe(false);
878e026c 316 });
5a2a53cf 317
878e026c 318 it('Verify isNotEmptyString()', () => {
9bf0ef23
JB
319 expect(isNotEmptyString('')).toBe(false);
320 expect(isNotEmptyString(' ')).toBe(false);
321 expect(isNotEmptyString(' ')).toBe(false);
322 expect(isNotEmptyString('test')).toBe(true);
323 expect(isNotEmptyString(' test')).toBe(true);
324 expect(isNotEmptyString('test ')).toBe(true);
325 expect(isNotEmptyString(undefined)).toBe(false);
326 expect(isNotEmptyString(null)).toBe(false);
327 expect(isNotEmptyString(0)).toBe(false);
328 expect(isNotEmptyString({})).toBe(false);
329 expect(isNotEmptyString([])).toBe(false);
330 expect(isNotEmptyString(new Map())).toBe(false);
331 expect(isNotEmptyString(new Set())).toBe(false);
332 expect(isNotEmptyString(new WeakMap())).toBe(false);
333 expect(isNotEmptyString(new WeakSet())).toBe(false);
878e026c 334 });
429f8c9d 335
878e026c 336 it('Verify isUndefined()', () => {
9bf0ef23
JB
337 expect(isUndefined(undefined)).toBe(true);
338 expect(isUndefined(null)).toBe(false);
339 expect(isUndefined('')).toBe(false);
340 expect(isUndefined(0)).toBe(false);
341 expect(isUndefined({})).toBe(false);
342 expect(isUndefined([])).toBe(false);
343 expect(isUndefined(new Map())).toBe(false);
344 expect(isUndefined(new Set())).toBe(false);
345 expect(isUndefined(new WeakMap())).toBe(false);
346 expect(isUndefined(new WeakSet())).toBe(false);
878e026c 347 });
a2111e8d 348
878e026c 349 it('Verify isNullOrUndefined()', () => {
9bf0ef23
JB
350 expect(isNullOrUndefined(undefined)).toBe(true);
351 expect(isNullOrUndefined(null)).toBe(true);
352 expect(isNullOrUndefined('')).toBe(false);
353 expect(isNullOrUndefined(0)).toBe(false);
354 expect(isNullOrUndefined({})).toBe(false);
355 expect(isNullOrUndefined([])).toBe(false);
356 expect(isNullOrUndefined(new Map())).toBe(false);
357 expect(isNullOrUndefined(new Set())).toBe(false);
358 expect(isNullOrUndefined(new WeakMap())).toBe(false);
359 expect(isNullOrUndefined(new WeakSet())).toBe(false);
878e026c 360 });
53ac516c 361
878e026c 362 it('Verify isEmptyArray()', () => {
9bf0ef23
JB
363 expect(isEmptyArray([])).toBe(true);
364 expect(isEmptyArray([1, 2])).toBe(false);
365 expect(isEmptyArray(['1', '2'])).toBe(false);
366 expect(isEmptyArray(undefined)).toBe(false);
367 expect(isEmptyArray(null)).toBe(false);
368 expect(isEmptyArray('')).toBe(false);
369 expect(isEmptyArray('test')).toBe(false);
370 expect(isEmptyArray(0)).toBe(false);
371 expect(isEmptyArray({})).toBe(false);
372 expect(isEmptyArray(new Map())).toBe(false);
373 expect(isEmptyArray(new Set())).toBe(false);
374 expect(isEmptyArray(new WeakMap())).toBe(false);
375 expect(isEmptyArray(new WeakSet())).toBe(false);
878e026c 376 });
a2111e8d 377
878e026c 378 it('Verify isNotEmptyArray()', () => {
9bf0ef23
JB
379 expect(isNotEmptyArray([])).toBe(false);
380 expect(isNotEmptyArray([1, 2])).toBe(true);
381 expect(isNotEmptyArray(['1', '2'])).toBe(true);
382 expect(isNotEmptyArray(undefined)).toBe(false);
383 expect(isNotEmptyArray(null)).toBe(false);
384 expect(isNotEmptyArray('')).toBe(false);
385 expect(isNotEmptyArray('test')).toBe(false);
386 expect(isNotEmptyArray(0)).toBe(false);
387 expect(isNotEmptyArray({})).toBe(false);
388 expect(isNotEmptyArray(new Map())).toBe(false);
389 expect(isNotEmptyArray(new Set())).toBe(false);
390 expect(isNotEmptyArray(new WeakMap())).toBe(false);
391 expect(isNotEmptyArray(new WeakSet())).toBe(false);
878e026c
JB
392 });
393
394 it('Verify isEmptyObject()', () => {
9bf0ef23
JB
395 expect(isEmptyObject({})).toBe(true);
396 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
9bf0ef23
JB
397 expect(isEmptyObject(new Map())).toBe(false);
398 expect(isEmptyObject(new Set())).toBe(false);
399 expect(isEmptyObject(new WeakMap())).toBe(false);
400 expect(isEmptyObject(new WeakSet())).toBe(false);
878e026c 401 });
80c58041
JB
402
403 it('Verify isArraySorted()', () => {
404 expect(
405 isArraySorted([], (a, b) => {
406 return a - b;
407 }),
408 ).toBe(true);
409 expect(
410 isArraySorted([1], (a, b) => {
411 return a - b;
412 }),
413 ).toBe(true);
414 expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
415 expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
416 });
878e026c 417});