Commit | Line | Data |
---|---|---|
85cce27f | 1 | import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'; |
f64ca10c | 2 | import { expect } from 'expect'; |
a2111e8d | 3 | |
878e026c | 4 | import { Constants } from '../../src/utils/Constants'; |
9bf0ef23 JB |
5 | import { |
6 | cloneObject, | |
7 | convertToBoolean, | |
8 | convertToDate, | |
9 | convertToFloat, | |
10 | convertToInt, | |
1553f6df | 11 | extractTimeSeriesValues, |
be4c6702 | 12 | formatDurationMilliSeconds, |
8053c842 | 13 | formatDurationSeconds, |
9bf0ef23 JB |
14 | generateUUID, |
15 | getRandomFloat, | |
16 | getRandomInteger, | |
17 | hasOwnProp, | |
80c58041 | 18 | isArraySorted, |
9bf0ef23 JB |
19 | isEmptyArray, |
20 | isEmptyObject, | |
21 | isEmptyString, | |
22 | isIterable, | |
23 | isNotEmptyArray, | |
24 | isNotEmptyString, | |
25 | isNullOrUndefined, | |
26 | isObject, | |
27 | isUndefined, | |
0bd926c1 | 28 | isValidTime, |
5f742aac | 29 | once, |
9bf0ef23 JB |
30 | roundTo, |
31 | secureRandom, | |
32 | sleep, | |
33 | validateUUID, | |
34 | } from '../../src/utils/Utils'; | |
a2111e8d | 35 | |
878e026c JB |
36 | describe('Utils test suite', () => { |
37 | it('Verify generateUUID()/validateUUID()', () => { | |
9bf0ef23 | 38 | const uuid = generateUUID(); |
878e026c JB |
39 | expect(uuid).toBeDefined(); |
40 | expect(uuid.length).toEqual(36); | |
9bf0ef23 JB |
41 | expect(validateUUID(uuid)).toBe(true); |
42 | expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); | |
43 | expect(validateUUID('')).toBe(false); | |
878e026c | 44 | // Shall invalidate Nil UUID |
9bf0ef23 JB |
45 | expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); |
46 | expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); | |
878e026c | 47 | }); |
8bd02502 | 48 | |
878e026c | 49 | it('Verify sleep()', async () => { |
f204e9b4 | 50 | const start = performance.now(); |
9bf0ef23 | 51 | await sleep(1000); |
0f6058f3 JB |
52 | const stop = performance.now(); |
53 | expect(stop - start).toBeGreaterThanOrEqual(1000); | |
878e026c | 54 | }); |
45999aab | 55 | |
be4c6702 JB |
56 | it('Verify formatDurationMilliSeconds()', () => { |
57 | expect(formatDurationMilliSeconds(0)).toBe(''); | |
58 | expect(formatDurationMilliSeconds(1000)).toBe('1 second'); | |
59 | expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours'); | |
60 | }); | |
61 | ||
8053c842 JB |
62 | it('Verify formatDurationSeconds()', () => { |
63 | expect(formatDurationSeconds(0)).toBe(''); | |
64 | expect(formatDurationSeconds(1)).toBe('1 second'); | |
65 | expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours'); | |
66 | }); | |
67 | ||
0bd926c1 JB |
68 | it('Verify isValidTime()', () => { |
69 | expect(isValidTime(undefined)).toBe(false); | |
70 | expect(isValidTime(null)).toBe(false); | |
71 | expect(isValidTime('')).toBe(false); | |
72 | expect(isValidTime({})).toBe(false); | |
73 | expect(isValidTime([])).toBe(false); | |
74 | expect(isValidTime(new Map())).toBe(false); | |
75 | expect(isValidTime(new Set())).toBe(false); | |
76 | expect(isValidTime(new WeakMap())).toBe(false); | |
77 | expect(isValidTime(new WeakSet())).toBe(false); | |
78 | expect(isValidTime(-1)).toBe(true); | |
79 | expect(isValidTime(0)).toBe(true); | |
80 | expect(isValidTime(1)).toBe(true); | |
81 | expect(isValidTime(-0.5)).toBe(true); | |
82 | expect(isValidTime(0.5)).toBe(true); | |
83 | expect(isValidTime(new Date())).toBe(true); | |
ac8178a4 JB |
84 | }); |
85 | ||
878e026c | 86 | it('Verify convertToDate()', () => { |
9bf0ef23 | 87 | expect(convertToDate(undefined)).toBe(undefined); |
85cce27f JB |
88 | expect(() => convertToDate('')).toThrowError(new Error("Cannot convert to date: ''")); |
89 | expect(() => convertToDate('00:70:61')).toThrowError( | |
90 | new Error("Cannot convert to date: '00:70:61'"), | |
91 | ); | |
9bf0ef23 | 92 | expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); |
85cce27f | 93 | expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z')); |
878e026c | 94 | const dateStr = '2020-01-01T00:00:00.000Z'; |
9bf0ef23 | 95 | let date = convertToDate(dateStr); |
878e026c JB |
96 | expect(date).toBeInstanceOf(Date); |
97 | expect(date).toStrictEqual(new Date(dateStr)); | |
9bf0ef23 | 98 | date = convertToDate(new Date(dateStr)); |
878e026c JB |
99 | expect(date).toBeInstanceOf(Date); |
100 | expect(date).toStrictEqual(new Date(dateStr)); | |
101 | }); | |
df645d8f | 102 | |
878e026c | 103 | it('Verify convertToInt()', () => { |
9bf0ef23 JB |
104 | expect(convertToInt(undefined)).toBe(0); |
105 | expect(convertToInt(null)).toBe(0); | |
106 | expect(convertToInt(0)).toBe(0); | |
107 | const randomInteger = getRandomInteger(); | |
108 | expect(convertToInt(randomInteger)).toEqual(randomInteger); | |
109 | expect(convertToInt('-1')).toBe(-1); | |
110 | expect(convertToInt('1')).toBe(1); | |
111 | expect(convertToInt('1.1')).toBe(1); | |
112 | expect(convertToInt('1.9')).toBe(1); | |
113 | expect(convertToInt('1.999')).toBe(1); | |
114 | expect(convertToInt(-1)).toBe(-1); | |
115 | expect(convertToInt(1)).toBe(1); | |
116 | expect(convertToInt(1.1)).toBe(1); | |
117 | expect(convertToInt(1.9)).toBe(1); | |
118 | expect(convertToInt(1.999)).toBe(1); | |
878e026c | 119 | expect(() => { |
9bf0ef23 | 120 | convertToInt('NaN'); |
85cce27f | 121 | }).toThrow("Cannot convert to integer: 'NaN'"); |
878e026c | 122 | }); |
df645d8f | 123 | |
878e026c | 124 | it('Verify convertToFloat()', () => { |
9bf0ef23 JB |
125 | expect(convertToFloat(undefined)).toBe(0); |
126 | expect(convertToFloat(null)).toBe(0); | |
127 | expect(convertToFloat(0)).toBe(0); | |
128 | const randomFloat = getRandomFloat(); | |
129 | expect(convertToFloat(randomFloat)).toEqual(randomFloat); | |
130 | expect(convertToFloat('-1')).toBe(-1); | |
131 | expect(convertToFloat('1')).toBe(1); | |
132 | expect(convertToFloat('1.1')).toBe(1.1); | |
133 | expect(convertToFloat('1.9')).toBe(1.9); | |
134 | expect(convertToFloat('1.999')).toBe(1.999); | |
135 | expect(convertToFloat(-1)).toBe(-1); | |
136 | expect(convertToFloat(1)).toBe(1); | |
137 | expect(convertToFloat(1.1)).toBe(1.1); | |
138 | expect(convertToFloat(1.9)).toBe(1.9); | |
139 | expect(convertToFloat(1.999)).toBe(1.999); | |
878e026c | 140 | expect(() => { |
9bf0ef23 | 141 | convertToFloat('NaN'); |
85cce27f | 142 | }).toThrow("Cannot convert to float: 'NaN'"); |
878e026c | 143 | }); |
df645d8f | 144 | |
878e026c | 145 | it('Verify convertToBoolean()', () => { |
9bf0ef23 JB |
146 | expect(convertToBoolean(undefined)).toBe(false); |
147 | expect(convertToBoolean(null)).toBe(false); | |
148 | expect(convertToBoolean('true')).toBe(true); | |
149 | expect(convertToBoolean('false')).toBe(false); | |
150 | expect(convertToBoolean('TRUE')).toBe(true); | |
151 | expect(convertToBoolean('FALSE')).toBe(false); | |
152 | expect(convertToBoolean('1')).toBe(true); | |
153 | expect(convertToBoolean('0')).toBe(false); | |
154 | expect(convertToBoolean(1)).toBe(true); | |
155 | expect(convertToBoolean(0)).toBe(false); | |
156 | expect(convertToBoolean(true)).toBe(true); | |
157 | expect(convertToBoolean(false)).toBe(false); | |
158 | expect(convertToBoolean('')).toBe(false); | |
159 | expect(convertToBoolean('NoNBoolean')).toBe(false); | |
878e026c | 160 | }); |
df645d8f | 161 | |
878e026c | 162 | it('Verify secureRandom()', () => { |
9bf0ef23 | 163 | const random = secureRandom(); |
878e026c JB |
164 | expect(typeof random === 'number').toBe(true); |
165 | expect(random).toBeGreaterThanOrEqual(0); | |
166 | expect(random).toBeLessThan(1); | |
167 | }); | |
a2111e8d | 168 | |
878e026c | 169 | it('Verify getRandomInteger()', () => { |
9bf0ef23 | 170 | let randomInteger = getRandomInteger(); |
878e026c JB |
171 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
172 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
173 | expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER); | |
9bf0ef23 JB |
174 | expect(randomInteger).not.toEqual(getRandomInteger()); |
175 | randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER); | |
878e026c JB |
176 | expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER); |
177 | expect(randomInteger).toBeLessThanOrEqual(0); | |
9bf0ef23 | 178 | expect(() => getRandomInteger(0, 1)).toThrowError( |
5edd8ba0 | 179 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1', |
878e026c | 180 | ); |
9bf0ef23 | 181 | expect(() => getRandomInteger(-1)).toThrowError( |
5edd8ba0 | 182 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0', |
878e026c | 183 | ); |
9bf0ef23 | 184 | expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( |
878e026c JB |
185 | `The value of "max" is out of range. It must be <= ${ |
186 | Constants.MAX_RANDOM_INTEGER + 1 | |
5edd8ba0 | 187 | }. Received 281_474_976_710_656`, |
878e026c | 188 | ); |
9bf0ef23 | 189 | randomInteger = getRandomInteger(2, 1); |
878e026c JB |
190 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
191 | expect(randomInteger).toBeLessThanOrEqual(2); | |
192 | const max = 2.2, | |
193 | min = 1.1; | |
9bf0ef23 | 194 | randomInteger = getRandomInteger(max, min); |
878e026c JB |
195 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); |
196 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); | |
197 | }); | |
a2111e8d | 198 | |
316d1564 | 199 | it('Verify roundTo()', () => { |
9bf0ef23 JB |
200 | expect(roundTo(0, 2)).toBe(0); |
201 | expect(roundTo(0.5, 0)).toBe(1); | |
202 | expect(roundTo(0.5, 2)).toBe(0.5); | |
203 | expect(roundTo(-0.5, 0)).toBe(-1); | |
204 | expect(roundTo(-0.5, 2)).toBe(-0.5); | |
205 | expect(roundTo(1.005, 0)).toBe(1); | |
206 | expect(roundTo(1.005, 2)).toBe(1.01); | |
207 | expect(roundTo(2.175, 2)).toBe(2.18); | |
208 | expect(roundTo(5.015, 2)).toBe(5.02); | |
209 | expect(roundTo(-1.005, 2)).toBe(-1.01); | |
210 | expect(roundTo(-2.175, 2)).toBe(-2.18); | |
211 | expect(roundTo(-5.015, 2)).toBe(-5.02); | |
316d1564 JB |
212 | }); |
213 | ||
878e026c | 214 | it('Verify getRandomFloat()', () => { |
9bf0ef23 | 215 | let randomFloat = getRandomFloat(); |
878e026c JB |
216 | expect(typeof randomFloat === 'number').toBe(true); |
217 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
218 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
9bf0ef23 JB |
219 | expect(randomFloat).not.toEqual(getRandomFloat()); |
220 | expect(() => getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
221 | expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError( | |
5edd8ba0 | 222 | new RangeError('Invalid interval'), |
878e026c | 223 | ); |
9bf0ef23 | 224 | randomFloat = getRandomFloat(0, -Number.MAX_VALUE); |
878e026c JB |
225 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
226 | expect(randomFloat).toBeLessThanOrEqual(0); | |
227 | }); | |
a2111e8d | 228 | |
1553f6df JB |
229 | it('Verify extractTimeSeriesValues()', () => { |
230 | expect(extractTimeSeriesValues([])).toEqual([]); | |
231 | expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]); | |
232 | expect( | |
233 | extractTimeSeriesValues([ | |
234 | { timestamp: Date.now(), value: 1.1 }, | |
235 | { timestamp: Date.now(), value: 2.2 }, | |
236 | ]), | |
237 | ).toEqual([1.1, 2.2]); | |
238 | }); | |
239 | ||
878e026c | 240 | it('Verify isObject()', () => { |
9bf0ef23 JB |
241 | expect(isObject('test')).toBe(false); |
242 | expect(isObject(undefined)).toBe(false); | |
243 | expect(isObject(null)).toBe(false); | |
244 | expect(isObject(0)).toBe(false); | |
245 | expect(isObject([])).toBe(false); | |
246 | expect(isObject([0, 1])).toBe(false); | |
247 | expect(isObject(['0', '1'])).toBe(false); | |
248 | expect(isObject({})).toBe(true); | |
249 | expect(isObject({ 1: 1 })).toBe(true); | |
250 | expect(isObject({ '1': '1' })).toBe(true); | |
251 | expect(isObject(new Map())).toBe(true); | |
252 | expect(isObject(new Set())).toBe(true); | |
253 | expect(isObject(new WeakMap())).toBe(true); | |
254 | expect(isObject(new WeakSet())).toBe(true); | |
878e026c | 255 | }); |
a2111e8d | 256 | |
dd5a1d6c JB |
257 | it('Verify cloneObject()', () => { |
258 | const obj = { 1: 1 }; | |
9bf0ef23 JB |
259 | expect(cloneObject(obj)).toStrictEqual(obj); |
260 | expect(cloneObject(obj) === obj).toBe(false); | |
32f5e42d JB |
261 | const nestedObj = { 1: obj, 2: obj }; |
262 | expect(cloneObject(nestedObj)).toStrictEqual(nestedObj); | |
263 | expect(cloneObject(nestedObj) === nestedObj).toBe(false); | |
dd5a1d6c | 264 | const array = [1, 2]; |
9bf0ef23 JB |
265 | expect(cloneObject(array)).toStrictEqual(array); |
266 | expect(cloneObject(array) === array).toBe(false); | |
32f5e42d JB |
267 | const objArray = [obj, obj]; |
268 | expect(cloneObject(objArray)).toStrictEqual(objArray); | |
269 | expect(cloneObject(objArray) === objArray).toBe(false); | |
dd5a1d6c | 270 | const date = new Date(); |
9bf0ef23 JB |
271 | expect(cloneObject(date)).toStrictEqual(date); |
272 | expect(cloneObject(date) === date).toBe(false); | |
dd5a1d6c | 273 | const map = new Map([['1', '2']]); |
32f5e42d | 274 | expect(cloneObject(map)).toStrictEqual({}); |
dd5a1d6c | 275 | const set = new Set(['1']); |
32f5e42d | 276 | expect(cloneObject(set)).toStrictEqual({}); |
dd5a1d6c JB |
277 | // The URL object seems to have not enumerable properties |
278 | const url = new URL('https://domain.tld'); | |
32f5e42d | 279 | expect(cloneObject(url)).toStrictEqual({}); |
dd5a1d6c | 280 | const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]); |
32f5e42d | 281 | expect(cloneObject(weakMap)).toStrictEqual({}); |
dd5a1d6c | 282 | const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]); |
32f5e42d | 283 | expect(cloneObject(weakSet)).toStrictEqual({}); |
dd5a1d6c JB |
284 | }); |
285 | ||
af29b598 | 286 | it('Verify hasOwnProp()', () => { |
9bf0ef23 JB |
287 | expect(hasOwnProp('test', '')).toBe(false); |
288 | expect(hasOwnProp(undefined, '')).toBe(false); | |
289 | expect(hasOwnProp(null, '')).toBe(false); | |
290 | expect(hasOwnProp([], '')).toBe(false); | |
291 | expect(hasOwnProp({}, '')).toBe(false); | |
292 | expect(hasOwnProp({ 1: 1 }, 1)).toBe(true); | |
293 | expect(hasOwnProp({ 1: 1 }, '1')).toBe(true); | |
294 | expect(hasOwnProp({ 1: 1 }, 2)).toBe(false); | |
295 | expect(hasOwnProp({ 1: 1 }, '2')).toBe(false); | |
296 | expect(hasOwnProp({ '1': '1' }, '1')).toBe(true); | |
297 | expect(hasOwnProp({ '1': '1' }, 1)).toBe(true); | |
298 | expect(hasOwnProp({ '1': '1' }, '2')).toBe(false); | |
299 | expect(hasOwnProp({ '1': '1' }, 2)).toBe(false); | |
af29b598 JB |
300 | }); |
301 | ||
878e026c | 302 | it('Verify isIterable()', () => { |
9bf0ef23 JB |
303 | expect(isIterable('')).toBe(true); |
304 | expect(isIterable(' ')).toBe(true); | |
305 | expect(isIterable('test')).toBe(true); | |
306 | expect(isIterable(undefined)).toBe(false); | |
307 | expect(isIterable(null)).toBe(false); | |
308 | expect(isIterable(0)).toBe(false); | |
309 | expect(isIterable([0, 1])).toBe(true); | |
310 | expect(isIterable({ 1: 1 })).toBe(false); | |
311 | expect(isIterable(new Map())).toBe(true); | |
312 | expect(isIterable(new Set())).toBe(true); | |
313 | expect(isIterable(new WeakMap())).toBe(false); | |
314 | expect(isIterable(new WeakSet())).toBe(false); | |
878e026c | 315 | }); |
a2111e8d | 316 | |
878e026c | 317 | it('Verify isEmptyString()', () => { |
9bf0ef23 JB |
318 | expect(isEmptyString('')).toBe(true); |
319 | expect(isEmptyString(' ')).toBe(true); | |
320 | expect(isEmptyString(' ')).toBe(true); | |
321 | expect(isEmptyString('test')).toBe(false); | |
322 | expect(isEmptyString(' test')).toBe(false); | |
323 | expect(isEmptyString('test ')).toBe(false); | |
324 | expect(isEmptyString(undefined)).toBe(true); | |
325 | expect(isEmptyString(null)).toBe(true); | |
326 | expect(isEmptyString(0)).toBe(false); | |
327 | expect(isEmptyString({})).toBe(false); | |
328 | expect(isEmptyString([])).toBe(false); | |
329 | expect(isEmptyString(new Map())).toBe(false); | |
330 | expect(isEmptyString(new Set())).toBe(false); | |
331 | expect(isEmptyString(new WeakMap())).toBe(false); | |
332 | expect(isEmptyString(new WeakSet())).toBe(false); | |
878e026c | 333 | }); |
5a2a53cf | 334 | |
878e026c | 335 | it('Verify isNotEmptyString()', () => { |
9bf0ef23 JB |
336 | expect(isNotEmptyString('')).toBe(false); |
337 | expect(isNotEmptyString(' ')).toBe(false); | |
338 | expect(isNotEmptyString(' ')).toBe(false); | |
339 | expect(isNotEmptyString('test')).toBe(true); | |
340 | expect(isNotEmptyString(' test')).toBe(true); | |
341 | expect(isNotEmptyString('test ')).toBe(true); | |
342 | expect(isNotEmptyString(undefined)).toBe(false); | |
343 | expect(isNotEmptyString(null)).toBe(false); | |
344 | expect(isNotEmptyString(0)).toBe(false); | |
345 | expect(isNotEmptyString({})).toBe(false); | |
346 | expect(isNotEmptyString([])).toBe(false); | |
347 | expect(isNotEmptyString(new Map())).toBe(false); | |
348 | expect(isNotEmptyString(new Set())).toBe(false); | |
349 | expect(isNotEmptyString(new WeakMap())).toBe(false); | |
350 | expect(isNotEmptyString(new WeakSet())).toBe(false); | |
878e026c | 351 | }); |
429f8c9d | 352 | |
878e026c | 353 | it('Verify isUndefined()', () => { |
9bf0ef23 JB |
354 | expect(isUndefined(undefined)).toBe(true); |
355 | expect(isUndefined(null)).toBe(false); | |
356 | expect(isUndefined('')).toBe(false); | |
357 | expect(isUndefined(0)).toBe(false); | |
358 | expect(isUndefined({})).toBe(false); | |
359 | expect(isUndefined([])).toBe(false); | |
360 | expect(isUndefined(new Map())).toBe(false); | |
361 | expect(isUndefined(new Set())).toBe(false); | |
362 | expect(isUndefined(new WeakMap())).toBe(false); | |
363 | expect(isUndefined(new WeakSet())).toBe(false); | |
878e026c | 364 | }); |
a2111e8d | 365 | |
878e026c | 366 | it('Verify isNullOrUndefined()', () => { |
9bf0ef23 JB |
367 | expect(isNullOrUndefined(undefined)).toBe(true); |
368 | expect(isNullOrUndefined(null)).toBe(true); | |
369 | expect(isNullOrUndefined('')).toBe(false); | |
370 | expect(isNullOrUndefined(0)).toBe(false); | |
371 | expect(isNullOrUndefined({})).toBe(false); | |
372 | expect(isNullOrUndefined([])).toBe(false); | |
373 | expect(isNullOrUndefined(new Map())).toBe(false); | |
374 | expect(isNullOrUndefined(new Set())).toBe(false); | |
375 | expect(isNullOrUndefined(new WeakMap())).toBe(false); | |
376 | expect(isNullOrUndefined(new WeakSet())).toBe(false); | |
878e026c | 377 | }); |
53ac516c | 378 | |
878e026c | 379 | it('Verify isEmptyArray()', () => { |
9bf0ef23 JB |
380 | expect(isEmptyArray([])).toBe(true); |
381 | expect(isEmptyArray([1, 2])).toBe(false); | |
382 | expect(isEmptyArray(['1', '2'])).toBe(false); | |
383 | expect(isEmptyArray(undefined)).toBe(false); | |
384 | expect(isEmptyArray(null)).toBe(false); | |
385 | expect(isEmptyArray('')).toBe(false); | |
386 | expect(isEmptyArray('test')).toBe(false); | |
387 | expect(isEmptyArray(0)).toBe(false); | |
388 | expect(isEmptyArray({})).toBe(false); | |
389 | expect(isEmptyArray(new Map())).toBe(false); | |
390 | expect(isEmptyArray(new Set())).toBe(false); | |
391 | expect(isEmptyArray(new WeakMap())).toBe(false); | |
392 | expect(isEmptyArray(new WeakSet())).toBe(false); | |
878e026c | 393 | }); |
a2111e8d | 394 | |
878e026c | 395 | it('Verify isNotEmptyArray()', () => { |
9bf0ef23 JB |
396 | expect(isNotEmptyArray([])).toBe(false); |
397 | expect(isNotEmptyArray([1, 2])).toBe(true); | |
398 | expect(isNotEmptyArray(['1', '2'])).toBe(true); | |
399 | expect(isNotEmptyArray(undefined)).toBe(false); | |
400 | expect(isNotEmptyArray(null)).toBe(false); | |
401 | expect(isNotEmptyArray('')).toBe(false); | |
402 | expect(isNotEmptyArray('test')).toBe(false); | |
403 | expect(isNotEmptyArray(0)).toBe(false); | |
404 | expect(isNotEmptyArray({})).toBe(false); | |
405 | expect(isNotEmptyArray(new Map())).toBe(false); | |
406 | expect(isNotEmptyArray(new Set())).toBe(false); | |
407 | expect(isNotEmptyArray(new WeakMap())).toBe(false); | |
408 | expect(isNotEmptyArray(new WeakSet())).toBe(false); | |
878e026c JB |
409 | }); |
410 | ||
411 | it('Verify isEmptyObject()', () => { | |
9bf0ef23 JB |
412 | expect(isEmptyObject({})).toBe(true); |
413 | expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false); | |
9bf0ef23 JB |
414 | expect(isEmptyObject(new Map())).toBe(false); |
415 | expect(isEmptyObject(new Set())).toBe(false); | |
416 | expect(isEmptyObject(new WeakMap())).toBe(false); | |
417 | expect(isEmptyObject(new WeakSet())).toBe(false); | |
878e026c | 418 | }); |
80c58041 JB |
419 | |
420 | it('Verify isArraySorted()', () => { | |
421 | expect( | |
422 | isArraySorted([], (a, b) => { | |
423 | return a - b; | |
424 | }), | |
425 | ).toBe(true); | |
426 | expect( | |
427 | isArraySorted([1], (a, b) => { | |
428 | return a - b; | |
429 | }), | |
430 | ).toBe(true); | |
431 | expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true); | |
432 | expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false); | |
479200ff | 433 | expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false); |
80c58041 | 434 | }); |
5f742aac JB |
435 | |
436 | it('Verify once()', () => { | |
437 | let called = 0; | |
438 | const fn = () => ++called; | |
439 | const onceFn = once(fn, this); | |
440 | const result1 = onceFn(); | |
441 | expect(called).toBe(1); | |
442 | expect(result1).toBe(1); | |
443 | const result2 = onceFn(); | |
444 | expect(called).toBe(1); | |
445 | expect(result2).toBe(1); | |
446 | const result3 = onceFn(); | |
447 | expect(called).toBe(1); | |
448 | expect(result3).toBe(1); | |
449 | }); | |
878e026c | 450 | }); |