Commit | Line | Data |
---|---|---|
a2111e8d JB |
1 | import expect from 'expect'; |
2 | ||
3 | import Utils from '../../src/utils/Utils'; | |
4 | ||
5 | describe('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 JB |
25 | it('Verify convertToDate()', () => { |
26 | const dateStr = '2020-01-01T00:00:00.000Z'; | |
27 | let date = Utils.convertToDate(dateStr); | |
28 | expect(date).toBeInstanceOf(Date); | |
29 | expect(date.getUTCFullYear()).toBe(2020); | |
30 | expect(date.getUTCMonth()).toBe(0); | |
31 | expect(date.getUTCDate()).toBe(1); | |
32 | expect(date.getUTCHours()).toBe(0); | |
33 | expect(date.getUTCMinutes()).toBe(0); | |
34 | expect(date.getUTCSeconds()).toBe(0); | |
35 | expect(date.getUTCMilliseconds()).toBe(0); | |
36 | date = Utils.convertToDate(new Date(dateStr)); | |
37 | expect(date).toBeInstanceOf(Date); | |
38 | expect(date.getUTCFullYear()).toBe(2020); | |
39 | expect(date.getUTCMonth()).toBe(0); | |
40 | expect(date.getUTCDate()).toBe(1); | |
41 | expect(date.getUTCHours()).toBe(0); | |
42 | expect(date.getUTCMinutes()).toBe(0); | |
43 | expect(date.getUTCSeconds()).toBe(0); | |
44 | expect(date.getUTCMilliseconds()).toBe(0); | |
45 | }); | |
46 | ||
47 | it('Verify convertToInt()', () => { | |
48 | const random = Utils.getRandomInteger(); | |
49 | expect(Utils.convertToInt(random)).toEqual(random); | |
50 | expect(Utils.convertToInt('1')).toBe(1); | |
51 | expect(Utils.convertToInt('1.1')).toBe(1); | |
52 | expect(Utils.convertToInt('1.9')).toBe(1); | |
53 | expect(Utils.convertToInt('1.999')).toBe(1); | |
54 | expect(Utils.convertToInt(1)).toBe(1); | |
55 | expect(Utils.convertToInt(1.1)).toBe(1); | |
56 | expect(Utils.convertToInt(1.9)).toBe(1); | |
57 | expect(Utils.convertToInt(1.999)).toBe(1); | |
58 | }); | |
59 | ||
60 | it('Verify convertToFloat()', () => { | |
61 | const random = Utils.getRandomFloat(); | |
62 | expect(Utils.convertToFloat(random)).toEqual(random); | |
63 | expect(Utils.convertToFloat('1')).toBe(1); | |
64 | expect(Utils.convertToFloat('1.1')).toBe(1.1); | |
65 | expect(Utils.convertToFloat('1.9')).toBe(1.9); | |
66 | expect(Utils.convertToFloat('1.999')).toBe(1.999); | |
67 | expect(Utils.convertToFloat(1)).toBe(1); | |
68 | expect(Utils.convertToFloat(1.1)).toBe(1.1); | |
69 | expect(Utils.convertToFloat(1.9)).toBe(1.9); | |
70 | expect(Utils.convertToFloat(1.999)).toBe(1.999); | |
71 | }); | |
72 | ||
73 | it('Verify convertToBoolean()', () => { | |
74 | expect(Utils.convertToBoolean('true')).toBe(true); | |
75 | expect(Utils.convertToBoolean('false')).toBe(false); | |
76 | expect(Utils.convertToBoolean('TRUE')).toBe(true); | |
77 | expect(Utils.convertToBoolean('FALSE')).toBe(false); | |
78 | expect(Utils.convertToBoolean('1')).toBe(true); | |
79 | expect(Utils.convertToBoolean('0')).toBe(false); | |
80 | expect(Utils.convertToBoolean(1)).toBe(true); | |
81 | expect(Utils.convertToBoolean(0)).toBe(false); | |
82 | expect(Utils.convertToBoolean(true)).toBe(true); | |
83 | expect(Utils.convertToBoolean(false)).toBe(false); | |
84 | }); | |
85 | ||
a2111e8d JB |
86 | it('Verify secureRandom()', () => { |
87 | const random = Utils.secureRandom(); | |
88 | expect(typeof random === 'number').toBe(true); | |
89 | expect(random).toBeGreaterThanOrEqual(0); | |
90 | expect(random).toBeLessThan(1); | |
91 | }); | |
92 | ||
93 | it('Verify getRandomInteger()', () => { | |
bd9f680e | 94 | let randomInteger = Utils.getRandomInteger(); |
a2111e8d JB |
95 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
96 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
97 | expect(randomInteger).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER); | |
98 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
99 | expect(() => Utils.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval')); | |
100 | expect(() => Utils.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
bd9f680e JB |
101 | randomInteger = Utils.getRandomInteger(2, 1); |
102 | expect(randomInteger).toBeGreaterThanOrEqual(1); | |
103 | expect(randomInteger).toBeLessThanOrEqual(2); | |
a2111e8d JB |
104 | }); |
105 | ||
106 | it('Verify getRandomFloat()', () => { | |
bd9f680e | 107 | let randomFloat = Utils.getRandomFloat(); |
a2111e8d JB |
108 | expect(typeof randomFloat === 'number').toBe(true); |
109 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
110 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
111 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
112 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); | |
113 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
bd9f680e JB |
114 | randomFloat = Utils.getRandomFloat(Number.MAX_VALUE, 0, true); |
115 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); | |
116 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
a2111e8d JB |
117 | }); |
118 | ||
119 | it('Verify isIterable()', () => { | |
120 | expect(Utils.isIterable('')).toBe(false); | |
121 | expect(Utils.isIterable(' ')).toBe(true); | |
122 | expect(Utils.isIterable('test')).toBe(true); | |
123 | expect(Utils.isIterable(null)).toBe(false); | |
124 | expect(Utils.isIterable(undefined)).toBe(false); | |
125 | expect(Utils.isIterable(0)).toBe(false); | |
126 | expect(Utils.isIterable({})).toBe(false); | |
127 | expect(Utils.isIterable({ 1: 1 })).toBe(false); | |
128 | expect(Utils.isIterable([])).toBe(true); | |
129 | expect(Utils.isIterable(new Map())).toBe(true); | |
130 | expect(Utils.isIterable(new Set())).toBe(true); | |
5f60a7fd JB |
131 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
132 | expect(Utils.isIterable(new WeakSet())).toBe(false); | |
a2111e8d JB |
133 | }); |
134 | ||
135 | it('Verify isEmptyString()', () => { | |
136 | expect(Utils.isEmptyString('')).toBe(true); | |
137 | expect(Utils.isEmptyString(' ')).toBe(true); | |
138 | expect(Utils.isEmptyString(' ')).toBe(true); | |
139 | expect(Utils.isEmptyString('test')).toBe(false); | |
140 | expect(Utils.isEmptyString(' test')).toBe(false); | |
141 | expect(Utils.isEmptyString('test ')).toBe(false); | |
142 | expect(Utils.isEmptyString(null)).toBe(false); | |
143 | expect(Utils.isEmptyString(undefined)).toBe(false); | |
144 | expect(Utils.isEmptyString(0)).toBe(false); | |
145 | expect(Utils.isEmptyString({})).toBe(false); | |
146 | expect(Utils.isEmptyString([])).toBe(false); | |
147 | expect(Utils.isEmptyString(new Map())).toBe(false); | |
148 | expect(Utils.isEmptyString(new Set())).toBe(false); | |
45999aab JB |
149 | expect(Utils.isEmptyString(new WeakMap())).toBe(false); |
150 | expect(Utils.isEmptyString(new WeakSet())).toBe(false); | |
a2111e8d JB |
151 | }); |
152 | ||
429f8c9d JB |
153 | it('Verify isUndefined()', () => { |
154 | expect(Utils.isUndefined(undefined)).toBe(true); | |
155 | expect(Utils.isUndefined(null)).toBe(false); | |
156 | expect(Utils.isUndefined('')).toBe(false); | |
157 | expect(Utils.isUndefined(0)).toBe(false); | |
158 | expect(Utils.isUndefined({})).toBe(false); | |
159 | expect(Utils.isUndefined([])).toBe(false); | |
160 | expect(Utils.isUndefined(new Map())).toBe(false); | |
161 | expect(Utils.isUndefined(new Set())).toBe(false); | |
162 | expect(Utils.isUndefined(new WeakMap())).toBe(false); | |
163 | expect(Utils.isUndefined(new WeakSet())).toBe(false); | |
164 | }); | |
165 | ||
a2111e8d JB |
166 | it('Verify isNullOrUndefined()', () => { |
167 | expect(Utils.isNullOrUndefined(null)).toBe(true); | |
168 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); | |
169 | expect(Utils.isNullOrUndefined('')).toBe(false); | |
170 | expect(Utils.isNullOrUndefined(0)).toBe(false); | |
171 | expect(Utils.isNullOrUndefined({})).toBe(false); | |
172 | expect(Utils.isNullOrUndefined([])).toBe(false); | |
173 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); | |
174 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); | |
45999aab JB |
175 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
176 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); | |
a2111e8d JB |
177 | }); |
178 | ||
179 | it('Verify isEmptyArray()', () => { | |
180 | expect(Utils.isEmptyArray([])).toBe(true); | |
181 | expect(Utils.isEmptyArray([1, 2])).toBe(false); | |
30e27491 JB |
182 | expect(Utils.isEmptyArray(null)).toBe(true); |
183 | expect(Utils.isEmptyArray(undefined)).toBe(true); | |
184 | expect(Utils.isEmptyArray('')).toBe(true); | |
185 | expect(Utils.isEmptyArray('test')).toBe(true); | |
186 | expect(Utils.isEmptyArray(0)).toBe(true); | |
187 | expect(Utils.isEmptyArray({})).toBe(true); | |
188 | expect(Utils.isEmptyArray(new Map())).toBe(true); | |
189 | expect(Utils.isEmptyArray(new Set())).toBe(true); | |
190 | expect(Utils.isEmptyArray(new WeakMap())).toBe(true); | |
191 | expect(Utils.isEmptyArray(new WeakSet())).toBe(true); | |
a2111e8d JB |
192 | }); |
193 | ||
194 | it('Verify isEmptyObject()', () => { | |
195 | expect(Utils.isEmptyObject({})).toBe(true); | |
67f24396 | 196 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
a2111e8d JB |
197 | expect(Utils.isEmptyObject(null)).toBe(false); |
198 | expect(Utils.isEmptyObject(undefined)).toBe(false); | |
199 | expect(Utils.isEmptyObject(new Map())).toBe(false); | |
200 | expect(Utils.isEmptyObject(new Set())).toBe(false); | |
45999aab JB |
201 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
202 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); | |
a2111e8d JB |
203 | }); |
204 | }); |