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