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 secureRandom()', () => {
26 const random
= Utils
.secureRandom();
27 expect(typeof random
=== 'number').toBe(true);
28 expect(random
).toBeGreaterThanOrEqual(0);
29 expect(random
).toBeLessThan(1);
32 it('Verify getRandomInteger()', () => {
33 const randomInteger
= Utils
.getRandomInteger();
34 expect(Number.isSafeInteger(randomInteger
)).toBe(true);
35 expect(randomInteger
).toBeGreaterThanOrEqual(0);
36 expect(randomInteger
).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER
);
37 expect(() => Utils
.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval'));
38 expect(() => Utils
.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval'));
39 expect(() => Utils
.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval'));
42 it('Verify getRandomFloat()', () => {
43 const randomFloat
= Utils
.getRandomFloat();
44 expect(typeof randomFloat
=== 'number').toBe(true);
45 expect(randomFloat
).toBeGreaterThanOrEqual(0);
46 expect(randomFloat
).toBeLessThanOrEqual(Number.MAX_VALUE
);
47 expect(() => Utils
.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval'));
48 expect(() => Utils
.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval'));
49 expect(() => Utils
.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval'));
52 it('Verify isIterable()', () => {
53 expect(Utils
.isIterable('')).toBe(false);
54 expect(Utils
.isIterable(' ')).toBe(true);
55 expect(Utils
.isIterable('test')).toBe(true);
56 expect(Utils
.isIterable(null)).toBe(false);
57 expect(Utils
.isIterable(undefined)).toBe(false);
58 expect(Utils
.isIterable(0)).toBe(false);
59 expect(Utils
.isIterable({})).toBe(false);
60 expect(Utils
.isIterable({ 1: 1 })).toBe(false);
61 expect(Utils
.isIterable([])).toBe(true);
62 expect(Utils
.isIterable(new Map())).toBe(true);
63 expect(Utils
.isIterable(new Set())).toBe(true);
64 expect(Utils
.isIterable(new WeakMap())).toBe(false);
65 expect(Utils
.isIterable(new WeakSet())).toBe(false);
68 it('Verify isEmptyString()', () => {
69 expect(Utils
.isEmptyString('')).toBe(true);
70 expect(Utils
.isEmptyString(' ')).toBe(true);
71 expect(Utils
.isEmptyString(' ')).toBe(true);
72 expect(Utils
.isEmptyString('test')).toBe(false);
73 expect(Utils
.isEmptyString(' test')).toBe(false);
74 expect(Utils
.isEmptyString('test ')).toBe(false);
75 expect(Utils
.isEmptyString(null)).toBe(false);
76 expect(Utils
.isEmptyString(undefined)).toBe(false);
77 expect(Utils
.isEmptyString(0)).toBe(false);
78 expect(Utils
.isEmptyString({})).toBe(false);
79 expect(Utils
.isEmptyString([])).toBe(false);
80 expect(Utils
.isEmptyString(new Map())).toBe(false);
81 expect(Utils
.isEmptyString(new Set())).toBe(false);
82 expect(Utils
.isEmptyString(new WeakMap())).toBe(false);
83 expect(Utils
.isEmptyString(new WeakSet())).toBe(false);
86 it('Verify isNullOrUndefined()', () => {
87 expect(Utils
.isNullOrUndefined(null)).toBe(true);
88 expect(Utils
.isNullOrUndefined(undefined)).toBe(true);
89 expect(Utils
.isNullOrUndefined('')).toBe(false);
90 expect(Utils
.isNullOrUndefined(0)).toBe(false);
91 expect(Utils
.isNullOrUndefined({})).toBe(false);
92 expect(Utils
.isNullOrUndefined([])).toBe(false);
93 expect(Utils
.isNullOrUndefined(new Map())).toBe(false);
94 expect(Utils
.isNullOrUndefined(new Set())).toBe(false);
95 expect(Utils
.isNullOrUndefined(new WeakMap())).toBe(false);
96 expect(Utils
.isNullOrUndefined(new WeakSet())).toBe(false);
99 it('Verify isEmptyArray()', () => {
100 expect(Utils
.isEmptyArray([])).toBe(true);
101 expect(Utils
.isEmptyArray([1, 2])).toBe(false);
102 expect(Utils
.isEmptyArray(null)).toBe(true);
103 expect(Utils
.isEmptyArray(undefined)).toBe(true);
104 expect(Utils
.isEmptyArray('')).toBe(true);
105 expect(Utils
.isEmptyArray('test')).toBe(true);
106 expect(Utils
.isEmptyArray(0)).toBe(true);
107 expect(Utils
.isEmptyArray({})).toBe(true);
108 expect(Utils
.isEmptyArray(new Map())).toBe(true);
109 expect(Utils
.isEmptyArray(new Set())).toBe(true);
110 expect(Utils
.isEmptyArray(new WeakMap())).toBe(true);
111 expect(Utils
.isEmptyArray(new WeakSet())).toBe(true);
114 it('Verify isEmptyObject()', () => {
115 expect(Utils
.isEmptyObject({})).toBe(true);
116 expect(Utils
.isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
117 expect(Utils
.isEmptyObject(null)).toBe(false);
118 expect(Utils
.isEmptyObject(undefined)).toBe(false);
119 expect(Utils
.isEmptyObject(new Map())).toBe(false);
120 expect(Utils
.isEmptyObject(new Set())).toBe(false);
121 expect(Utils
.isEmptyObject(new WeakMap())).toBe(false);
122 expect(Utils
.isEmptyObject(new WeakSet())).toBe(false);