docs: update worker section documentation
[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 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 isValidDate,
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 isValidDate()', () => {
67 expect(isValidDate(undefined)).toBe(false);
68 expect(isValidDate(null)).toBe(false);
69 expect(isValidDate('')).toBe(false);
70 expect(isValidDate({})).toBe(false);
71 expect(isValidDate([])).toBe(false);
72 expect(isValidDate(new Map())).toBe(false);
73 expect(isValidDate(new Set())).toBe(false);
74 expect(isValidDate(new WeakMap())).toBe(false);
75 expect(isValidDate(new WeakSet())).toBe(false);
76 expect(isValidDate(-1)).toBe(true);
77 expect(isValidDate(0)).toBe(true);
78 expect(isValidDate(1)).toBe(true);
79 expect(isValidDate(-0.5)).toBe(true);
80 expect(isValidDate(0.5)).toBe(true);
81 expect(isValidDate(new Date())).toBe(true);
82 });
83
84 it('Verify convertToDate()', () => {
85 expect(convertToDate(undefined)).toBe(undefined);
86 expect(convertToDate(null)).toBe(null);
87 const invalidDate = convertToDate('');
88 expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false);
89 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
90 const dateStr = '2020-01-01T00:00:00.000Z';
91 let date = convertToDate(dateStr);
92 expect(date).toBeInstanceOf(Date);
93 expect(date).toStrictEqual(new Date(dateStr));
94 date = convertToDate(new Date(dateStr));
95 expect(date).toBeInstanceOf(Date);
96 expect(date).toStrictEqual(new Date(dateStr));
97 });
98
99 it('Verify convertToInt()', () => {
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);
115 expect(() => {
116 convertToInt('NaN');
117 }).toThrow('Cannot convert to integer: NaN');
118 });
119
120 it('Verify convertToFloat()', () => {
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);
136 expect(() => {
137 convertToFloat('NaN');
138 }).toThrow('Cannot convert to float: NaN');
139 });
140
141 it('Verify convertToBoolean()', () => {
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);
156 });
157
158 it('Verify secureRandom()', () => {
159 const random = secureRandom();
160 expect(typeof random === 'number').toBe(true);
161 expect(random).toBeGreaterThanOrEqual(0);
162 expect(random).toBeLessThan(1);
163 });
164
165 it('Verify getRandomInteger()', () => {
166 let randomInteger = getRandomInteger();
167 expect(Number.isSafeInteger(randomInteger)).toBe(true);
168 expect(randomInteger).toBeGreaterThanOrEqual(0);
169 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER);
170 expect(randomInteger).not.toEqual(getRandomInteger());
171 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER);
172 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER);
173 expect(randomInteger).toBeLessThanOrEqual(0);
174 expect(() => getRandomInteger(0, 1)).toThrowError(
175 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1',
176 );
177 expect(() => getRandomInteger(-1)).toThrowError(
178 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0',
179 );
180 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError(
181 `The value of "max" is out of range. It must be <= ${
182 Constants.MAX_RANDOM_INTEGER + 1
183 }. Received 281_474_976_710_656`,
184 );
185 randomInteger = getRandomInteger(2, 1);
186 expect(randomInteger).toBeGreaterThanOrEqual(1);
187 expect(randomInteger).toBeLessThanOrEqual(2);
188 const max = 2.2,
189 min = 1.1;
190 randomInteger = getRandomInteger(max, min);
191 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min));
192 expect(randomInteger).toBeLessThanOrEqual(Math.floor(max));
193 });
194
195 it('Verify roundTo()', () => {
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);
208 });
209
210 it('Verify getRandomFloat()', () => {
211 let randomFloat = getRandomFloat();
212 expect(typeof randomFloat === 'number').toBe(true);
213 expect(randomFloat).toBeGreaterThanOrEqual(0);
214 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
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(
218 new RangeError('Invalid interval'),
219 );
220 randomFloat = getRandomFloat(0, -Number.MAX_VALUE);
221 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
222 expect(randomFloat).toBeLessThanOrEqual(0);
223 });
224
225 it('Verify isObject()', () => {
226 expect(isObject('test')).toBe(false);
227 expect(isObject(undefined)).toBe(false);
228 expect(isObject(null)).toBe(false);
229 expect(isObject(0)).toBe(false);
230 expect(isObject([])).toBe(false);
231 expect(isObject([0, 1])).toBe(false);
232 expect(isObject(['0', '1'])).toBe(false);
233 expect(isObject({})).toBe(true);
234 expect(isObject({ 1: 1 })).toBe(true);
235 expect(isObject({ '1': '1' })).toBe(true);
236 expect(isObject(new Map())).toBe(true);
237 expect(isObject(new Set())).toBe(true);
238 expect(isObject(new WeakMap())).toBe(true);
239 expect(isObject(new WeakSet())).toBe(true);
240 });
241
242 it('Verify cloneObject()', () => {
243 const obj = { 1: 1 };
244 expect(cloneObject(obj)).toStrictEqual(obj);
245 expect(cloneObject(obj) === obj).toBe(false);
246 const array = [1, 2];
247 expect(cloneObject(array)).toStrictEqual(array);
248 expect(cloneObject(array) === array).toBe(false);
249 const date = new Date();
250 expect(cloneObject(date)).toStrictEqual(date);
251 expect(cloneObject(date) === date).toBe(false);
252 const map = new Map([['1', '2']]);
253 expect(cloneObject(map)).toStrictEqual(map);
254 expect(cloneObject(map) === map).toBe(false);
255 const set = new Set(['1']);
256 expect(cloneObject(set)).toStrictEqual(set);
257 expect(cloneObject(set) === set).toBe(false);
258 // The URL object seems to have not enumerable properties
259 const url = new URL('https://domain.tld');
260 expect(cloneObject(url)).toStrictEqual(url);
261 expect(cloneObject(url) === url).toBe(true);
262 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
263 expect(cloneObject(weakMap)).toStrictEqual(weakMap);
264 expect(cloneObject(weakMap) === weakMap).toBe(true);
265 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
266 expect(cloneObject(weakSet)).toStrictEqual(weakSet);
267 expect(cloneObject(weakSet) === weakSet).toBe(true);
268 });
269
270 it('Verify hasOwnProp()', () => {
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);
284 });
285
286 it('Verify isIterable()', () => {
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);
299 });
300
301 it('Verify isEmptyString()', () => {
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);
317 });
318
319 it('Verify isNotEmptyString()', () => {
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);
335 });
336
337 it('Verify isUndefined()', () => {
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);
348 });
349
350 it('Verify isNullOrUndefined()', () => {
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);
361 });
362
363 it('Verify isEmptyArray()', () => {
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);
377 });
378
379 it('Verify isNotEmptyArray()', () => {
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);
393 });
394
395 it('Verify isEmptyObject()', () => {
396 expect(isEmptyObject({})).toBe(true);
397 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
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);
402 });
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);
417 });
418 });