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