fix: fix integer check condition
[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);
32f5e42d
JB
245 const nestedObj = { 1: obj, 2: obj };
246 expect(cloneObject(nestedObj)).toStrictEqual(nestedObj);
247 expect(cloneObject(nestedObj) === nestedObj).toBe(false);
dd5a1d6c 248 const array = [1, 2];
9bf0ef23
JB
249 expect(cloneObject(array)).toStrictEqual(array);
250 expect(cloneObject(array) === array).toBe(false);
32f5e42d
JB
251 const objArray = [obj, obj];
252 expect(cloneObject(objArray)).toStrictEqual(objArray);
253 expect(cloneObject(objArray) === objArray).toBe(false);
dd5a1d6c 254 const date = new Date();
9bf0ef23
JB
255 expect(cloneObject(date)).toStrictEqual(date);
256 expect(cloneObject(date) === date).toBe(false);
dd5a1d6c 257 const map = new Map([['1', '2']]);
32f5e42d 258 expect(cloneObject(map)).toStrictEqual({});
dd5a1d6c 259 const set = new Set(['1']);
32f5e42d 260 expect(cloneObject(set)).toStrictEqual({});
dd5a1d6c
JB
261 // The URL object seems to have not enumerable properties
262 const url = new URL('https://domain.tld');
32f5e42d 263 expect(cloneObject(url)).toStrictEqual({});
dd5a1d6c 264 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
32f5e42d 265 expect(cloneObject(weakMap)).toStrictEqual({});
dd5a1d6c 266 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
32f5e42d 267 expect(cloneObject(weakSet)).toStrictEqual({});
dd5a1d6c
JB
268 });
269
af29b598 270 it('Verify hasOwnProp()', () => {
9bf0ef23
JB
271 expect(hasOwnProp('test', '')).toBe(false);
272 expect(hasOwnProp(undefined, '')).toBe(false);
273 expect(hasOwnProp(null, '')).toBe(false);
274 expect(hasOwnProp([], '')).toBe(false);
275 expect(hasOwnProp({}, '')).toBe(false);
276 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true);
277 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true);
278 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false);
279 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false);
280 expect(hasOwnProp({ '1': '1' }, '1')).toBe(true);
281 expect(hasOwnProp({ '1': '1' }, 1)).toBe(true);
282 expect(hasOwnProp({ '1': '1' }, '2')).toBe(false);
283 expect(hasOwnProp({ '1': '1' }, 2)).toBe(false);
af29b598
JB
284 });
285
878e026c 286 it('Verify isIterable()', () => {
9bf0ef23
JB
287 expect(isIterable('')).toBe(true);
288 expect(isIterable(' ')).toBe(true);
289 expect(isIterable('test')).toBe(true);
290 expect(isIterable(undefined)).toBe(false);
291 expect(isIterable(null)).toBe(false);
292 expect(isIterable(0)).toBe(false);
293 expect(isIterable([0, 1])).toBe(true);
294 expect(isIterable({ 1: 1 })).toBe(false);
295 expect(isIterable(new Map())).toBe(true);
296 expect(isIterable(new Set())).toBe(true);
297 expect(isIterable(new WeakMap())).toBe(false);
298 expect(isIterable(new WeakSet())).toBe(false);
878e026c 299 });
a2111e8d 300
878e026c 301 it('Verify isEmptyString()', () => {
9bf0ef23
JB
302 expect(isEmptyString('')).toBe(true);
303 expect(isEmptyString(' ')).toBe(true);
304 expect(isEmptyString(' ')).toBe(true);
305 expect(isEmptyString('test')).toBe(false);
306 expect(isEmptyString(' test')).toBe(false);
307 expect(isEmptyString('test ')).toBe(false);
308 expect(isEmptyString(undefined)).toBe(true);
309 expect(isEmptyString(null)).toBe(true);
310 expect(isEmptyString(0)).toBe(false);
311 expect(isEmptyString({})).toBe(false);
312 expect(isEmptyString([])).toBe(false);
313 expect(isEmptyString(new Map())).toBe(false);
314 expect(isEmptyString(new Set())).toBe(false);
315 expect(isEmptyString(new WeakMap())).toBe(false);
316 expect(isEmptyString(new WeakSet())).toBe(false);
878e026c 317 });
5a2a53cf 318
878e026c 319 it('Verify isNotEmptyString()', () => {
9bf0ef23
JB
320 expect(isNotEmptyString('')).toBe(false);
321 expect(isNotEmptyString(' ')).toBe(false);
322 expect(isNotEmptyString(' ')).toBe(false);
323 expect(isNotEmptyString('test')).toBe(true);
324 expect(isNotEmptyString(' test')).toBe(true);
325 expect(isNotEmptyString('test ')).toBe(true);
326 expect(isNotEmptyString(undefined)).toBe(false);
327 expect(isNotEmptyString(null)).toBe(false);
328 expect(isNotEmptyString(0)).toBe(false);
329 expect(isNotEmptyString({})).toBe(false);
330 expect(isNotEmptyString([])).toBe(false);
331 expect(isNotEmptyString(new Map())).toBe(false);
332 expect(isNotEmptyString(new Set())).toBe(false);
333 expect(isNotEmptyString(new WeakMap())).toBe(false);
334 expect(isNotEmptyString(new WeakSet())).toBe(false);
878e026c 335 });
429f8c9d 336
878e026c 337 it('Verify isUndefined()', () => {
9bf0ef23
JB
338 expect(isUndefined(undefined)).toBe(true);
339 expect(isUndefined(null)).toBe(false);
340 expect(isUndefined('')).toBe(false);
341 expect(isUndefined(0)).toBe(false);
342 expect(isUndefined({})).toBe(false);
343 expect(isUndefined([])).toBe(false);
344 expect(isUndefined(new Map())).toBe(false);
345 expect(isUndefined(new Set())).toBe(false);
346 expect(isUndefined(new WeakMap())).toBe(false);
347 expect(isUndefined(new WeakSet())).toBe(false);
878e026c 348 });
a2111e8d 349
878e026c 350 it('Verify isNullOrUndefined()', () => {
9bf0ef23
JB
351 expect(isNullOrUndefined(undefined)).toBe(true);
352 expect(isNullOrUndefined(null)).toBe(true);
353 expect(isNullOrUndefined('')).toBe(false);
354 expect(isNullOrUndefined(0)).toBe(false);
355 expect(isNullOrUndefined({})).toBe(false);
356 expect(isNullOrUndefined([])).toBe(false);
357 expect(isNullOrUndefined(new Map())).toBe(false);
358 expect(isNullOrUndefined(new Set())).toBe(false);
359 expect(isNullOrUndefined(new WeakMap())).toBe(false);
360 expect(isNullOrUndefined(new WeakSet())).toBe(false);
878e026c 361 });
53ac516c 362
878e026c 363 it('Verify isEmptyArray()', () => {
9bf0ef23
JB
364 expect(isEmptyArray([])).toBe(true);
365 expect(isEmptyArray([1, 2])).toBe(false);
366 expect(isEmptyArray(['1', '2'])).toBe(false);
367 expect(isEmptyArray(undefined)).toBe(false);
368 expect(isEmptyArray(null)).toBe(false);
369 expect(isEmptyArray('')).toBe(false);
370 expect(isEmptyArray('test')).toBe(false);
371 expect(isEmptyArray(0)).toBe(false);
372 expect(isEmptyArray({})).toBe(false);
373 expect(isEmptyArray(new Map())).toBe(false);
374 expect(isEmptyArray(new Set())).toBe(false);
375 expect(isEmptyArray(new WeakMap())).toBe(false);
376 expect(isEmptyArray(new WeakSet())).toBe(false);
878e026c 377 });
a2111e8d 378
878e026c 379 it('Verify isNotEmptyArray()', () => {
9bf0ef23
JB
380 expect(isNotEmptyArray([])).toBe(false);
381 expect(isNotEmptyArray([1, 2])).toBe(true);
382 expect(isNotEmptyArray(['1', '2'])).toBe(true);
383 expect(isNotEmptyArray(undefined)).toBe(false);
384 expect(isNotEmptyArray(null)).toBe(false);
385 expect(isNotEmptyArray('')).toBe(false);
386 expect(isNotEmptyArray('test')).toBe(false);
387 expect(isNotEmptyArray(0)).toBe(false);
388 expect(isNotEmptyArray({})).toBe(false);
389 expect(isNotEmptyArray(new Map())).toBe(false);
390 expect(isNotEmptyArray(new Set())).toBe(false);
391 expect(isNotEmptyArray(new WeakMap())).toBe(false);
392 expect(isNotEmptyArray(new WeakSet())).toBe(false);
878e026c
JB
393 });
394
395 it('Verify isEmptyObject()', () => {
9bf0ef23
JB
396 expect(isEmptyObject({})).toBe(true);
397 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
9bf0ef23
JB
398 expect(isEmptyObject(new Map())).toBe(false);
399 expect(isEmptyObject(new Set())).toBe(false);
400 expect(isEmptyObject(new WeakMap())).toBe(false);
401 expect(isEmptyObject(new WeakSet())).toBe(false);
878e026c 402 });
80c58041
JB
403
404 it('Verify isArraySorted()', () => {
405 expect(
406 isArraySorted([], (a, b) => {
407 return a - b;
408 }),
409 ).toBe(true);
410 expect(
411 isArraySorted([1], (a, b) => {
412 return a - b;
413 }),
414 ).toBe(true);
415 expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
416 expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
479200ff 417 expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
80c58041 418 });
878e026c 419});