Linter fixes
[e-mobility-charging-stations-simulator.git] / test / utils / UtilsTest.ts
CommitLineData
a2111e8d
JB
1import expect from 'expect';
2
3import Utils from '../../src/utils/Utils';
4
5describe('Utils test suite', () => {
8bd02502
JB
6 it('Verify generateUUID()/validateUUID()', () => {
7 const uuid = Utils.generateUUID();
d12e7f3c 8 expect(uuid).toBeDefined();
8bd02502
JB
9 expect(uuid.length).toEqual(36);
10 expect(Utils.validateUUID(uuid)).toBe(true);
96642078
JB
11 expect(Utils.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
12 expect(Utils.validateUUID('')).toBe(false);
45999aab
JB
13 // Shall invalidate Nil UUID
14 expect(Utils.validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
96642078 15 expect(Utils.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
8bd02502
JB
16 });
17
45999aab
JB
18 it('Verify sleep()', async () => {
19 const start = Date.now();
20 await Utils.sleep(1000);
21 const end = Date.now();
22 expect(end - start).toBeGreaterThanOrEqual(1000);
23 });
24
df645d8f 25 it('Verify convertToDate()', () => {
04dda061
JB
26 expect(Utils.convertToDate(undefined)).toBe(undefined);
27 expect(Utils.convertToDate(null)).toBe(null);
d439da5c
JB
28 const invalidDate = Utils.convertToDate('');
29 expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false);
30 expect(Utils.convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
df645d8f
JB
31 const dateStr = '2020-01-01T00:00:00.000Z';
32 let date = Utils.convertToDate(dateStr);
33 expect(date).toBeInstanceOf(Date);
d439da5c 34 expect(date).toStrictEqual(new Date(dateStr));
df645d8f
JB
35 date = Utils.convertToDate(new Date(dateStr));
36 expect(date).toBeInstanceOf(Date);
d439da5c 37 expect(date).toStrictEqual(new Date(dateStr));
df645d8f
JB
38 });
39
40 it('Verify convertToInt()', () => {
04dda061
JB
41 expect(Utils.convertToInt(undefined)).toBe(0);
42 expect(Utils.convertToInt(null)).toBe(0);
43 expect(Utils.convertToInt(0)).toBe(0);
44 const randomInteger = Utils.getRandomInteger();
45 expect(Utils.convertToInt(randomInteger)).toEqual(randomInteger);
0b312208 46 expect(Utils.convertToInt('-1')).toBe(-1);
df645d8f
JB
47 expect(Utils.convertToInt('1')).toBe(1);
48 expect(Utils.convertToInt('1.1')).toBe(1);
49 expect(Utils.convertToInt('1.9')).toBe(1);
50 expect(Utils.convertToInt('1.999')).toBe(1);
0b312208 51 expect(Utils.convertToInt(-1)).toBe(-1);
df645d8f
JB
52 expect(Utils.convertToInt(1)).toBe(1);
53 expect(Utils.convertToInt(1.1)).toBe(1);
54 expect(Utils.convertToInt(1.9)).toBe(1);
55 expect(Utils.convertToInt(1.999)).toBe(1);
b1340ec1
JB
56 expect(() => {
57 Utils.convertToInt('NaN');
58 }).toThrow('Cannot convert to integer: NaN');
df645d8f
JB
59 });
60
61 it('Verify convertToFloat()', () => {
04dda061
JB
62 expect(Utils.convertToFloat(undefined)).toBe(0);
63 expect(Utils.convertToFloat(null)).toBe(0);
64 expect(Utils.convertToFloat(0)).toBe(0);
65 const randomFloat = Utils.getRandomFloat();
66 expect(Utils.convertToFloat(randomFloat)).toEqual(randomFloat);
0b312208 67 expect(Utils.convertToFloat('-1')).toBe(-1);
df645d8f
JB
68 expect(Utils.convertToFloat('1')).toBe(1);
69 expect(Utils.convertToFloat('1.1')).toBe(1.1);
70 expect(Utils.convertToFloat('1.9')).toBe(1.9);
71 expect(Utils.convertToFloat('1.999')).toBe(1.999);
0b312208 72 expect(Utils.convertToFloat(-1)).toBe(-1);
df645d8f
JB
73 expect(Utils.convertToFloat(1)).toBe(1);
74 expect(Utils.convertToFloat(1.1)).toBe(1.1);
75 expect(Utils.convertToFloat(1.9)).toBe(1.9);
76 expect(Utils.convertToFloat(1.999)).toBe(1.999);
b1340ec1
JB
77 expect(() => {
78 Utils.convertToFloat('NaN');
79 }).toThrow('Cannot convert to float: NaN');
df645d8f
JB
80 });
81
82 it('Verify convertToBoolean()', () => {
04dda061
JB
83 expect(Utils.convertToBoolean(undefined)).toBe(false);
84 expect(Utils.convertToBoolean(null)).toBe(false);
df645d8f
JB
85 expect(Utils.convertToBoolean('true')).toBe(true);
86 expect(Utils.convertToBoolean('false')).toBe(false);
87 expect(Utils.convertToBoolean('TRUE')).toBe(true);
88 expect(Utils.convertToBoolean('FALSE')).toBe(false);
89 expect(Utils.convertToBoolean('1')).toBe(true);
90 expect(Utils.convertToBoolean('0')).toBe(false);
91 expect(Utils.convertToBoolean(1)).toBe(true);
92 expect(Utils.convertToBoolean(0)).toBe(false);
93 expect(Utils.convertToBoolean(true)).toBe(true);
94 expect(Utils.convertToBoolean(false)).toBe(false);
0b312208
JB
95 expect(Utils.convertToBoolean('')).toBe(false);
96 expect(Utils.convertToBoolean('NoNBoolean')).toBe(false);
df645d8f
JB
97 });
98
a2111e8d
JB
99 it('Verify secureRandom()', () => {
100 const random = Utils.secureRandom();
101 expect(typeof random === 'number').toBe(true);
102 expect(random).toBeGreaterThanOrEqual(0);
103 expect(random).toBeLessThan(1);
104 });
105
106 it('Verify getRandomInteger()', () => {
bd9f680e 107 let randomInteger = Utils.getRandomInteger();
a2111e8d
JB
108 expect(Number.isSafeInteger(randomInteger)).toBe(true);
109 expect(randomInteger).toBeGreaterThanOrEqual(0);
110 expect(randomInteger).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER);
7253a9bf 111 expect(randomInteger).not.toEqual(Utils.getRandomInteger());
a2111e8d
JB
112 expect(() => Utils.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval'));
113 expect(() => Utils.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval'));
114 expect(() => Utils.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval'));
bd9f680e
JB
115 randomInteger = Utils.getRandomInteger(2, 1);
116 expect(randomInteger).toBeGreaterThanOrEqual(1);
117 expect(randomInteger).toBeLessThanOrEqual(2);
0b312208
JB
118 const max = 2.2,
119 min = 1.1;
120 randomInteger = Utils.getRandomInteger(max, min);
121 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min));
122 expect(randomInteger).toBeLessThanOrEqual(Math.floor(max));
a2111e8d
JB
123 });
124
125 it('Verify getRandomFloat()', () => {
bd9f680e 126 let randomFloat = Utils.getRandomFloat();
a2111e8d
JB
127 expect(typeof randomFloat === 'number').toBe(true);
128 expect(randomFloat).toBeGreaterThanOrEqual(0);
129 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
7253a9bf 130 expect(randomFloat).not.toEqual(Utils.getRandomFloat());
a2111e8d
JB
131 expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval'));
132 expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval'));
133 expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval'));
bd9f680e
JB
134 randomFloat = Utils.getRandomFloat(Number.MAX_VALUE, 0, true);
135 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
136 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
a2111e8d
JB
137 });
138
139 it('Verify isIterable()', () => {
140 expect(Utils.isIterable('')).toBe(false);
141 expect(Utils.isIterable(' ')).toBe(true);
142 expect(Utils.isIterable('test')).toBe(true);
143 expect(Utils.isIterable(null)).toBe(false);
144 expect(Utils.isIterable(undefined)).toBe(false);
145 expect(Utils.isIterable(0)).toBe(false);
146 expect(Utils.isIterable({})).toBe(false);
147 expect(Utils.isIterable({ 1: 1 })).toBe(false);
148 expect(Utils.isIterable([])).toBe(true);
149 expect(Utils.isIterable(new Map())).toBe(true);
150 expect(Utils.isIterable(new Set())).toBe(true);
5f60a7fd
JB
151 expect(Utils.isIterable(new WeakMap())).toBe(false);
152 expect(Utils.isIterable(new WeakSet())).toBe(false);
a2111e8d
JB
153 });
154
155 it('Verify isEmptyString()', () => {
156 expect(Utils.isEmptyString('')).toBe(true);
157 expect(Utils.isEmptyString(' ')).toBe(true);
158 expect(Utils.isEmptyString(' ')).toBe(true);
159 expect(Utils.isEmptyString('test')).toBe(false);
160 expect(Utils.isEmptyString(' test')).toBe(false);
161 expect(Utils.isEmptyString('test ')).toBe(false);
162 expect(Utils.isEmptyString(null)).toBe(false);
163 expect(Utils.isEmptyString(undefined)).toBe(false);
164 expect(Utils.isEmptyString(0)).toBe(false);
165 expect(Utils.isEmptyString({})).toBe(false);
166 expect(Utils.isEmptyString([])).toBe(false);
167 expect(Utils.isEmptyString(new Map())).toBe(false);
168 expect(Utils.isEmptyString(new Set())).toBe(false);
45999aab
JB
169 expect(Utils.isEmptyString(new WeakMap())).toBe(false);
170 expect(Utils.isEmptyString(new WeakSet())).toBe(false);
a2111e8d
JB
171 });
172
429f8c9d
JB
173 it('Verify isUndefined()', () => {
174 expect(Utils.isUndefined(undefined)).toBe(true);
175 expect(Utils.isUndefined(null)).toBe(false);
176 expect(Utils.isUndefined('')).toBe(false);
177 expect(Utils.isUndefined(0)).toBe(false);
178 expect(Utils.isUndefined({})).toBe(false);
179 expect(Utils.isUndefined([])).toBe(false);
180 expect(Utils.isUndefined(new Map())).toBe(false);
181 expect(Utils.isUndefined(new Set())).toBe(false);
182 expect(Utils.isUndefined(new WeakMap())).toBe(false);
183 expect(Utils.isUndefined(new WeakSet())).toBe(false);
184 });
185
a2111e8d
JB
186 it('Verify isNullOrUndefined()', () => {
187 expect(Utils.isNullOrUndefined(null)).toBe(true);
188 expect(Utils.isNullOrUndefined(undefined)).toBe(true);
189 expect(Utils.isNullOrUndefined('')).toBe(false);
190 expect(Utils.isNullOrUndefined(0)).toBe(false);
191 expect(Utils.isNullOrUndefined({})).toBe(false);
192 expect(Utils.isNullOrUndefined([])).toBe(false);
193 expect(Utils.isNullOrUndefined(new Map())).toBe(false);
194 expect(Utils.isNullOrUndefined(new Set())).toBe(false);
45999aab
JB
195 expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false);
196 expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false);
a2111e8d
JB
197 });
198
199 it('Verify isEmptyArray()', () => {
200 expect(Utils.isEmptyArray([])).toBe(true);
201 expect(Utils.isEmptyArray([1, 2])).toBe(false);
30e27491
JB
202 expect(Utils.isEmptyArray(null)).toBe(true);
203 expect(Utils.isEmptyArray(undefined)).toBe(true);
204 expect(Utils.isEmptyArray('')).toBe(true);
205 expect(Utils.isEmptyArray('test')).toBe(true);
206 expect(Utils.isEmptyArray(0)).toBe(true);
207 expect(Utils.isEmptyArray({})).toBe(true);
208 expect(Utils.isEmptyArray(new Map())).toBe(true);
209 expect(Utils.isEmptyArray(new Set())).toBe(true);
210 expect(Utils.isEmptyArray(new WeakMap())).toBe(true);
211 expect(Utils.isEmptyArray(new WeakSet())).toBe(true);
a2111e8d
JB
212 });
213
214 it('Verify isEmptyObject()', () => {
215 expect(Utils.isEmptyObject({})).toBe(true);
67f24396 216 expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
a2111e8d
JB
217 expect(Utils.isEmptyObject(null)).toBe(false);
218 expect(Utils.isEmptyObject(undefined)).toBe(false);
219 expect(Utils.isEmptyObject(new Map())).toBe(false);
220 expect(Utils.isEmptyObject(new Set())).toBe(false);
45999aab
JB
221 expect(Utils.isEmptyObject(new WeakMap())).toBe(false);
222 expect(Utils.isEmptyObject(new WeakSet())).toBe(false);
a2111e8d
JB
223 });
224});