Commit | Line | Data |
---|---|---|
a2111e8d JB |
1 | import expect from 'expect'; |
2 | ||
59395dc1 | 3 | import Constants from '../../src/utils/Constants'; |
a2111e8d JB |
4 | import Utils from '../../src/utils/Utils'; |
5 | ||
6 | describe('Utils test suite', () => { | |
8bd02502 JB |
7 | it('Verify generateUUID()/validateUUID()', () => { |
8 | const uuid = Utils.generateUUID(); | |
d12e7f3c | 9 | expect(uuid).toBeDefined(); |
8bd02502 JB |
10 | expect(uuid.length).toEqual(36); |
11 | expect(Utils.validateUUID(uuid)).toBe(true); | |
96642078 JB |
12 | expect(Utils.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); |
13 | expect(Utils.validateUUID('')).toBe(false); | |
45999aab JB |
14 | // Shall invalidate Nil UUID |
15 | expect(Utils.validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); | |
96642078 | 16 | expect(Utils.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); |
8bd02502 JB |
17 | }); |
18 | ||
45999aab JB |
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); | |
24 | }); | |
25 | ||
df645d8f | 26 | it('Verify convertToDate()', () => { |
04dda061 JB |
27 | expect(Utils.convertToDate(undefined)).toBe(undefined); |
28 | expect(Utils.convertToDate(null)).toBe(null); | |
d439da5c JB |
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')); | |
df645d8f JB |
32 | const dateStr = '2020-01-01T00:00:00.000Z'; |
33 | let date = Utils.convertToDate(dateStr); | |
34 | expect(date).toBeInstanceOf(Date); | |
d439da5c | 35 | expect(date).toStrictEqual(new Date(dateStr)); |
df645d8f JB |
36 | date = Utils.convertToDate(new Date(dateStr)); |
37 | expect(date).toBeInstanceOf(Date); | |
d439da5c | 38 | expect(date).toStrictEqual(new Date(dateStr)); |
df645d8f JB |
39 | }); |
40 | ||
41 | it('Verify convertToInt()', () => { | |
04dda061 JB |
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); | |
0b312208 | 47 | expect(Utils.convertToInt('-1')).toBe(-1); |
df645d8f JB |
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); | |
0b312208 | 52 | expect(Utils.convertToInt(-1)).toBe(-1); |
df645d8f JB |
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); | |
b1340ec1 JB |
57 | expect(() => { |
58 | Utils.convertToInt('NaN'); | |
59 | }).toThrow('Cannot convert to integer: NaN'); | |
df645d8f JB |
60 | }); |
61 | ||
62 | it('Verify convertToFloat()', () => { | |
04dda061 JB |
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); | |
0b312208 | 68 | expect(Utils.convertToFloat('-1')).toBe(-1); |
df645d8f JB |
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); | |
0b312208 | 73 | expect(Utils.convertToFloat(-1)).toBe(-1); |
df645d8f JB |
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); | |
b1340ec1 JB |
78 | expect(() => { |
79 | Utils.convertToFloat('NaN'); | |
80 | }).toThrow('Cannot convert to float: NaN'); | |
df645d8f JB |
81 | }); |
82 | ||
83 | it('Verify convertToBoolean()', () => { | |
04dda061 JB |
84 | expect(Utils.convertToBoolean(undefined)).toBe(false); |
85 | expect(Utils.convertToBoolean(null)).toBe(false); | |
df645d8f JB |
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); | |
0b312208 JB |
96 | expect(Utils.convertToBoolean('')).toBe(false); |
97 | expect(Utils.convertToBoolean('NoNBoolean')).toBe(false); | |
df645d8f JB |
98 | }); |
99 | ||
a2111e8d JB |
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); | |
105 | }); | |
106 | ||
107 | it('Verify getRandomInteger()', () => { | |
bd9f680e | 108 | let randomInteger = Utils.getRandomInteger(); |
a2111e8d JB |
109 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
110 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
59395dc1 | 111 | expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER); |
7253a9bf | 112 | expect(randomInteger).not.toEqual(Utils.getRandomInteger()); |
59395dc1 JB |
113 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError( |
114 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1' | |
115 | ); | |
116 | expect(() => Utils.getRandomInteger(-1)).toThrowError( | |
117 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0' | |
118 | ); | |
119 | expect(() => Utils.getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( | |
120 | `The value of "max" is out of range. It must be <= ${ | |
121 | Constants.MAX_RANDOM_INTEGER + 1 | |
122 | }. Received 281_474_976_710_656` | |
123 | ); | |
bd9f680e JB |
124 | randomInteger = Utils.getRandomInteger(2, 1); |
125 | expect(randomInteger).toBeGreaterThanOrEqual(1); | |
126 | expect(randomInteger).toBeLessThanOrEqual(2); | |
0b312208 JB |
127 | const max = 2.2, |
128 | min = 1.1; | |
129 | randomInteger = Utils.getRandomInteger(max, min); | |
130 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); | |
131 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); | |
a2111e8d JB |
132 | }); |
133 | ||
134 | it('Verify getRandomFloat()', () => { | |
bd9f680e | 135 | let randomFloat = Utils.getRandomFloat(); |
a2111e8d JB |
136 | expect(typeof randomFloat === 'number').toBe(true); |
137 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
138 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
7253a9bf | 139 | expect(randomFloat).not.toEqual(Utils.getRandomFloat()); |
a2111e8d JB |
140 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); |
141 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); | |
142 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
bd9f680e JB |
143 | randomFloat = Utils.getRandomFloat(Number.MAX_VALUE, 0, true); |
144 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); | |
145 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
a2111e8d JB |
146 | }); |
147 | ||
148 | it('Verify isIterable()', () => { | |
293aaa31 | 149 | expect(Utils.isIterable('')).toBe(true); |
a2111e8d JB |
150 | expect(Utils.isIterable(' ')).toBe(true); |
151 | expect(Utils.isIterable('test')).toBe(true); | |
152 | expect(Utils.isIterable(null)).toBe(false); | |
153 | expect(Utils.isIterable(undefined)).toBe(false); | |
a2111e8d | 154 | expect(Utils.isIterable([])).toBe(true); |
a2111e8d JB |
155 | }); |
156 | ||
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); | |
1a4a811c JB |
164 | expect(Utils.isEmptyString(null)).toBe(true); |
165 | expect(Utils.isEmptyString(undefined)).toBe(true); | |
166 | expect(Utils.isEmptyString(0)).toBe(true); | |
167 | expect(Utils.isEmptyString({})).toBe(true); | |
168 | expect(Utils.isEmptyString([])).toBe(true); | |
169 | expect(Utils.isEmptyString(new Map())).toBe(true); | |
170 | expect(Utils.isEmptyString(new Set())).toBe(true); | |
171 | expect(Utils.isEmptyString(new WeakMap())).toBe(true); | |
172 | expect(Utils.isEmptyString(new WeakSet())).toBe(true); | |
a2111e8d JB |
173 | }); |
174 | ||
429f8c9d JB |
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); | |
186 | }); | |
187 | ||
a2111e8d JB |
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); | |
45999aab JB |
197 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
198 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); | |
a2111e8d JB |
199 | }); |
200 | ||
201 | it('Verify isEmptyArray()', () => { | |
202 | expect(Utils.isEmptyArray([])).toBe(true); | |
203 | expect(Utils.isEmptyArray([1, 2])).toBe(false); | |
30e27491 JB |
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); | |
a2111e8d JB |
214 | }); |
215 | ||
216 | it('Verify isEmptyObject()', () => { | |
217 | expect(Utils.isEmptyObject({})).toBe(true); | |
67f24396 | 218 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
a2111e8d JB |
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); | |
45999aab JB |
223 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
224 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); | |
a2111e8d JB |
225 | }); |
226 | }); |