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