36d06ddb2ebb2a5240832b9a31214f4ab77f8a85
1 import expect from
'expect';
3 import Utils from
'../../src/utils/Utils';
5 describe('Utils test suite', () => {
6 it('Verify generateUUID()/validateUUID()', () => {
7 const uuid
= Utils
.generateUUID();
8 expect(uuid
).toBeDefined();
9 expect(uuid
.length
).toEqual(36);
10 expect(Utils
.validateUUID(uuid
)).toBe(true);
11 expect(Utils
.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
12 expect(Utils
.validateUUID('')).toBe(false);
13 // Shall invalidate Nil UUID
14 expect(Utils
.validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
15 expect(Utils
.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
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);
25 it('Verify convertToDate()', () => {
26 expect(Utils
.convertToDate(undefined)).toBe(undefined);
27 expect(Utils
.convertToDate(null)).toBe(null);
28 expect(Utils
.convertToDate(0)).toBe(0);
29 const dateStr
= '2020-01-01T00:00:00.000Z';
30 let date
= Utils
.convertToDate(dateStr
);
31 expect(date
).toBeInstanceOf(Date);
32 expect(date
.getUTCFullYear()).toBe(2020);
33 expect(date
.getUTCMonth()).toBe(0);
34 expect(date
.getUTCDate()).toBe(1);
35 expect(date
.getUTCHours()).toBe(0);
36 expect(date
.getUTCMinutes()).toBe(0);
37 expect(date
.getUTCSeconds()).toBe(0);
38 expect(date
.getUTCMilliseconds()).toBe(0);
39 date
= Utils
.convertToDate(new Date(dateStr
));
40 expect(date
).toBeInstanceOf(Date);
41 expect(date
.getUTCFullYear()).toBe(2020);
42 expect(date
.getUTCMonth()).toBe(0);
43 expect(date
.getUTCDate()).toBe(1);
44 expect(date
.getUTCHours()).toBe(0);
45 expect(date
.getUTCMinutes()).toBe(0);
46 expect(date
.getUTCSeconds()).toBe(0);
47 expect(date
.getUTCMilliseconds()).toBe(0);
50 it('Verify convertToInt()', () => {
51 expect(Utils
.convertToInt(undefined)).toBe(0);
52 expect(Utils
.convertToInt(null)).toBe(0);
53 expect(Utils
.convertToInt(0)).toBe(0);
54 const randomInteger
= Utils
.getRandomInteger();
55 expect(Utils
.convertToInt(randomInteger
)).toEqual(randomInteger
);
56 expect(Utils
.convertToInt('-1')).toBe(-1);
57 expect(Utils
.convertToInt('1')).toBe(1);
58 expect(Utils
.convertToInt('1.1')).toBe(1);
59 expect(Utils
.convertToInt('1.9')).toBe(1);
60 expect(Utils
.convertToInt('1.999')).toBe(1);
61 expect(Utils
.convertToInt(-1)).toBe(-1);
62 expect(Utils
.convertToInt(1)).toBe(1);
63 expect(Utils
.convertToInt(1.1)).toBe(1);
64 expect(Utils
.convertToInt(1.9)).toBe(1);
65 expect(Utils
.convertToInt(1.999)).toBe(1);
68 it('Verify convertToFloat()', () => {
69 expect(Utils
.convertToFloat(undefined)).toBe(0);
70 expect(Utils
.convertToFloat(null)).toBe(0);
71 expect(Utils
.convertToFloat(0)).toBe(0);
72 const randomFloat
= Utils
.getRandomFloat();
73 expect(Utils
.convertToFloat(randomFloat
)).toEqual(randomFloat
);
74 expect(Utils
.convertToFloat('-1')).toBe(-1);
75 expect(Utils
.convertToFloat('1')).toBe(1);
76 expect(Utils
.convertToFloat('1.1')).toBe(1.1);
77 expect(Utils
.convertToFloat('1.9')).toBe(1.9);
78 expect(Utils
.convertToFloat('1.999')).toBe(1.999);
79 expect(Utils
.convertToFloat(-1)).toBe(-1);
80 expect(Utils
.convertToFloat(1)).toBe(1);
81 expect(Utils
.convertToFloat(1.1)).toBe(1.1);
82 expect(Utils
.convertToFloat(1.9)).toBe(1.9);
83 expect(Utils
.convertToFloat(1.999)).toBe(1.999);
86 it('Verify convertToBoolean()', () => {
87 expect(Utils
.convertToBoolean(undefined)).toBe(false);
88 expect(Utils
.convertToBoolean(null)).toBe(false);
89 expect(Utils
.convertToBoolean('true')).toBe(true);
90 expect(Utils
.convertToBoolean('false')).toBe(false);
91 expect(Utils
.convertToBoolean('TRUE')).toBe(true);
92 expect(Utils
.convertToBoolean('FALSE')).toBe(false);
93 expect(Utils
.convertToBoolean('1')).toBe(true);
94 expect(Utils
.convertToBoolean('0')).toBe(false);
95 expect(Utils
.convertToBoolean(1)).toBe(true);
96 expect(Utils
.convertToBoolean(0)).toBe(false);
97 expect(Utils
.convertToBoolean(true)).toBe(true);
98 expect(Utils
.convertToBoolean(false)).toBe(false);
99 expect(Utils
.convertToBoolean('')).toBe(false);
100 expect(Utils
.convertToBoolean('NoNBoolean')).toBe(false);
103 it('Verify secureRandom()', () => {
104 const random
= Utils
.secureRandom();
105 expect(typeof random
=== 'number').toBe(true);
106 expect(random
).toBeGreaterThanOrEqual(0);
107 expect(random
).toBeLessThan(1);
110 it('Verify getRandomInteger()', () => {
111 let randomInteger
= Utils
.getRandomInteger();
112 expect(Number.isSafeInteger(randomInteger
)).toBe(true);
113 expect(randomInteger
).toBeGreaterThanOrEqual(0);
114 expect(randomInteger
).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER
);
115 expect(() => Utils
.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval'));
116 expect(() => Utils
.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval'));
117 expect(() => Utils
.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval'));
118 randomInteger
= Utils
.getRandomInteger(2, 1);
119 expect(randomInteger
).toBeGreaterThanOrEqual(1);
120 expect(randomInteger
).toBeLessThanOrEqual(2);
123 randomInteger
= Utils
.getRandomInteger(max
, min
);
124 expect(randomInteger
).toBeGreaterThanOrEqual(Math.ceil(min
));
125 expect(randomInteger
).toBeLessThanOrEqual(Math.floor(max
));
128 it('Verify getRandomFloat()', () => {
129 let randomFloat
= Utils
.getRandomFloat();
130 expect(typeof randomFloat
=== 'number').toBe(true);
131 expect(randomFloat
).toBeGreaterThanOrEqual(0);
132 expect(randomFloat
).toBeLessThanOrEqual(Number.MAX_VALUE
);
133 expect(() => Utils
.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval'));
134 expect(() => Utils
.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval'));
135 expect(() => Utils
.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval'));
136 randomFloat
= Utils
.getRandomFloat(Number.MAX_VALUE
, 0, true);
137 expect(randomFloat
).toBeGreaterThanOrEqual(-Number.MAX_VALUE
);
138 expect(randomFloat
).toBeLessThanOrEqual(Number.MAX_VALUE
);
141 it('Verify isIterable()', () => {
142 expect(Utils
.isIterable('')).toBe(false);
143 expect(Utils
.isIterable(' ')).toBe(true);
144 expect(Utils
.isIterable('test')).toBe(true);
145 expect(Utils
.isIterable(null)).toBe(false);
146 expect(Utils
.isIterable(undefined)).toBe(false);
147 expect(Utils
.isIterable(0)).toBe(false);
148 expect(Utils
.isIterable({})).toBe(false);
149 expect(Utils
.isIterable({ 1: 1 })).toBe(false);
150 expect(Utils
.isIterable([])).toBe(true);
151 expect(Utils
.isIterable(new Map())).toBe(true);
152 expect(Utils
.isIterable(new Set())).toBe(true);
153 expect(Utils
.isIterable(new WeakMap())).toBe(false);
154 expect(Utils
.isIterable(new WeakSet())).toBe(false);
157 it('Verify isEmptyString()', () => {
158 expect(Utils
.isEmptyString('')).toBe(true);
159 expect(Utils
.isEmptyString(' ')).toBe(true);
160 expect(Utils
.isEmptyString(' ')).toBe(true);
161 expect(Utils
.isEmptyString('test')).toBe(false);
162 expect(Utils
.isEmptyString(' test')).toBe(false);
163 expect(Utils
.isEmptyString('test ')).toBe(false);
164 expect(Utils
.isEmptyString(null)).toBe(false);
165 expect(Utils
.isEmptyString(undefined)).toBe(false);
166 expect(Utils
.isEmptyString(0)).toBe(false);
167 expect(Utils
.isEmptyString({})).toBe(false);
168 expect(Utils
.isEmptyString([])).toBe(false);
169 expect(Utils
.isEmptyString(new Map())).toBe(false);
170 expect(Utils
.isEmptyString(new Set())).toBe(false);
171 expect(Utils
.isEmptyString(new WeakMap())).toBe(false);
172 expect(Utils
.isEmptyString(new WeakSet())).toBe(false);
175 it('Verify isUndefined()', () => {
176 expect(Utils
.isUndefined(undefined)).toBe(true);
177 expect(Utils
.isUndefined(null)).toBe(false);
178 expect(Utils
.isUndefined('')).toBe(false);
179 expect(Utils
.isUndefined(0)).toBe(false);
180 expect(Utils
.isUndefined({})).toBe(false);
181 expect(Utils
.isUndefined([])).toBe(false);
182 expect(Utils
.isUndefined(new Map())).toBe(false);
183 expect(Utils
.isUndefined(new Set())).toBe(false);
184 expect(Utils
.isUndefined(new WeakMap())).toBe(false);
185 expect(Utils
.isUndefined(new WeakSet())).toBe(false);
188 it('Verify isNullOrUndefined()', () => {
189 expect(Utils
.isNullOrUndefined(null)).toBe(true);
190 expect(Utils
.isNullOrUndefined(undefined)).toBe(true);
191 expect(Utils
.isNullOrUndefined('')).toBe(false);
192 expect(Utils
.isNullOrUndefined(0)).toBe(false);
193 expect(Utils
.isNullOrUndefined({})).toBe(false);
194 expect(Utils
.isNullOrUndefined([])).toBe(false);
195 expect(Utils
.isNullOrUndefined(new Map())).toBe(false);
196 expect(Utils
.isNullOrUndefined(new Set())).toBe(false);
197 expect(Utils
.isNullOrUndefined(new WeakMap())).toBe(false);
198 expect(Utils
.isNullOrUndefined(new WeakSet())).toBe(false);
201 it('Verify isEmptyArray()', () => {
202 expect(Utils
.isEmptyArray([])).toBe(true);
203 expect(Utils
.isEmptyArray([1, 2])).toBe(false);
204 expect(Utils
.isEmptyArray(null)).toBe(true);
205 expect(Utils
.isEmptyArray(undefined)).toBe(true);
206 expect(Utils
.isEmptyArray('')).toBe(true);
207 expect(Utils
.isEmptyArray('test')).toBe(true);
208 expect(Utils
.isEmptyArray(0)).toBe(true);
209 expect(Utils
.isEmptyArray({})).toBe(true);
210 expect(Utils
.isEmptyArray(new Map())).toBe(true);
211 expect(Utils
.isEmptyArray(new Set())).toBe(true);
212 expect(Utils
.isEmptyArray(new WeakMap())).toBe(true);
213 expect(Utils
.isEmptyArray(new WeakSet())).toBe(true);
216 it('Verify isEmptyObject()', () => {
217 expect(Utils
.isEmptyObject({})).toBe(true);
218 expect(Utils
.isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
219 expect(Utils
.isEmptyObject(null)).toBe(false);
220 expect(Utils
.isEmptyObject(undefined)).toBe(false);
221 expect(Utils
.isEmptyObject(new Map())).toBe(false);
222 expect(Utils
.isEmptyObject(new Set())).toBe(false);
223 expect(Utils
.isEmptyObject(new WeakMap())).toBe(false);
224 expect(Utils
.isEmptyObject(new WeakSet())).toBe(false);