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