test: code cleanup
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
1 import { hoursToMilliseconds, hoursToSeconds, isValid } 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 formatDurationMilliSeconds,
12 formatDurationSeconds,
13 generateUUID,
14 getRandomFloat,
15 getRandomInteger,
16 hasOwnProp,
17 isArraySorted,
18 isEmptyArray,
19 isEmptyObject,
20 isEmptyString,
21 isIterable,
22 isNotEmptyArray,
23 isNotEmptyString,
24 isNullOrUndefined,
25 isObject,
26 isUndefined,
27 isValidTime,
28 roundTo,
29 secureRandom,
30 sleep,
31 validateUUID,
32 } from '../../src/utils/Utils';
33
34 describe('Utils test suite', () => {
35 it('Verify generateUUID()/validateUUID()', () => {
36 const uuid = generateUUID();
37 expect(uuid).toBeDefined();
38 expect(uuid.length).toEqual(36);
39 expect(validateUUID(uuid)).toBe(true);
40 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
41 expect(validateUUID('')).toBe(false);
42 // Shall invalidate Nil UUID
43 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
44 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
45 });
46
47 it('Verify sleep()', async () => {
48 const start = performance.now();
49 await sleep(1000);
50 const end = performance.now();
51 expect(end - start).toBeGreaterThanOrEqual(1000);
52 });
53
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
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
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);
82 });
83
84 it('Verify convertToDate()', () => {
85 expect(convertToDate(undefined)).toBe(undefined);
86 expect(convertToDate(null)).toBe(null);
87 expect(isValid(convertToDate(''))).toBe(false);
88 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
89 const dateStr = '2020-01-01T00:00:00.000Z';
90 let date = convertToDate(dateStr);
91 expect(date).toBeInstanceOf(Date);
92 expect(date).toStrictEqual(new Date(dateStr));
93 date = convertToDate(new Date(dateStr));
94 expect(date).toBeInstanceOf(Date);
95 expect(date).toStrictEqual(new Date(dateStr));
96 });
97
98 it('Verify convertToInt()', () => {
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);
114 expect(() => {
115 convertToInt('NaN');
116 }).toThrow('Cannot convert to integer: NaN');
117 });
118
119 it('Verify convertToFloat()', () => {
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);
135 expect(() => {
136 convertToFloat('NaN');
137 }).toThrow('Cannot convert to float: NaN');
138 });
139
140 it('Verify convertToBoolean()', () => {
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);
155 });
156
157 it('Verify secureRandom()', () => {
158 const random = secureRandom();
159 expect(typeof random === 'number').toBe(true);
160 expect(random).toBeGreaterThanOrEqual(0);
161 expect(random).toBeLessThan(1);
162 });
163
164 it('Verify getRandomInteger()', () => {
165 let randomInteger = getRandomInteger();
166 expect(Number.isSafeInteger(randomInteger)).toBe(true);
167 expect(randomInteger).toBeGreaterThanOrEqual(0);
168 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER);
169 expect(randomInteger).not.toEqual(getRandomInteger());
170 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER);
171 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER);
172 expect(randomInteger).toBeLessThanOrEqual(0);
173 expect(() => getRandomInteger(0, 1)).toThrowError(
174 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1',
175 );
176 expect(() => getRandomInteger(-1)).toThrowError(
177 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0',
178 );
179 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError(
180 `The value of "max" is out of range. It must be <= ${
181 Constants.MAX_RANDOM_INTEGER + 1
182 }. Received 281_474_976_710_656`,
183 );
184 randomInteger = getRandomInteger(2, 1);
185 expect(randomInteger).toBeGreaterThanOrEqual(1);
186 expect(randomInteger).toBeLessThanOrEqual(2);
187 const max = 2.2,
188 min = 1.1;
189 randomInteger = getRandomInteger(max, min);
190 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min));
191 expect(randomInteger).toBeLessThanOrEqual(Math.floor(max));
192 });
193
194 it('Verify roundTo()', () => {
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);
207 });
208
209 it('Verify getRandomFloat()', () => {
210 let randomFloat = getRandomFloat();
211 expect(typeof randomFloat === 'number').toBe(true);
212 expect(randomFloat).toBeGreaterThanOrEqual(0);
213 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
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(
217 new RangeError('Invalid interval'),
218 );
219 randomFloat = getRandomFloat(0, -Number.MAX_VALUE);
220 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
221 expect(randomFloat).toBeLessThanOrEqual(0);
222 });
223
224 it('Verify isObject()', () => {
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);
239 });
240
241 it('Verify cloneObject()', () => {
242 const obj = { 1: 1 };
243 expect(cloneObject(obj)).toStrictEqual(obj);
244 expect(cloneObject(obj) === obj).toBe(false);
245 const array = [1, 2];
246 expect(cloneObject(array)).toStrictEqual(array);
247 expect(cloneObject(array) === array).toBe(false);
248 const date = new Date();
249 expect(cloneObject(date)).toStrictEqual(date);
250 expect(cloneObject(date) === date).toBe(false);
251 const map = new Map([['1', '2']]);
252 expect(cloneObject(map)).toStrictEqual(map);
253 expect(cloneObject(map) === map).toBe(false);
254 const set = new Set(['1']);
255 expect(cloneObject(set)).toStrictEqual(set);
256 expect(cloneObject(set) === set).toBe(false);
257 // The URL object seems to have not enumerable properties
258 const url = new URL('https://domain.tld');
259 expect(cloneObject(url)).toStrictEqual(url);
260 expect(cloneObject(url) === url).toBe(true);
261 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
262 expect(cloneObject(weakMap)).toStrictEqual(weakMap);
263 expect(cloneObject(weakMap) === weakMap).toBe(true);
264 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
265 expect(cloneObject(weakSet)).toStrictEqual(weakSet);
266 expect(cloneObject(weakSet) === weakSet).toBe(true);
267 });
268
269 it('Verify hasOwnProp()', () => {
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);
283 });
284
285 it('Verify isIterable()', () => {
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);
298 });
299
300 it('Verify isEmptyString()', () => {
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);
316 });
317
318 it('Verify isNotEmptyString()', () => {
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);
334 });
335
336 it('Verify isUndefined()', () => {
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);
347 });
348
349 it('Verify isNullOrUndefined()', () => {
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);
360 });
361
362 it('Verify isEmptyArray()', () => {
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);
376 });
377
378 it('Verify isNotEmptyArray()', () => {
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);
392 });
393
394 it('Verify isEmptyObject()', () => {
395 expect(isEmptyObject({})).toBe(true);
396 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
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);
401 });
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 });
417 });