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 | ||
a2111e8d JB |
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); | |
30 | }); | |
31 | ||
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')); | |
40 | }); | |
41 | ||
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')); | |
50 | }); | |
51 | ||
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); | |
5f60a7fd JB |
64 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
65 | expect(Utils.isIterable(new WeakSet())).toBe(false); | |
a2111e8d JB |
66 | }); |
67 | ||
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); | |
45999aab JB |
82 | expect(Utils.isEmptyString(new WeakMap())).toBe(false); |
83 | expect(Utils.isEmptyString(new WeakSet())).toBe(false); | |
a2111e8d JB |
84 | }); |
85 | ||
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); | |
45999aab JB |
95 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
96 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); | |
a2111e8d JB |
97 | }); |
98 | ||
99 | it('Verify isEmptyArray()', () => { | |
100 | expect(Utils.isEmptyArray([])).toBe(true); | |
101 | expect(Utils.isEmptyArray([1, 2])).toBe(false); | |
102 | expect(Utils.isEmptyArray(null)).toBe(false); | |
103 | expect(Utils.isEmptyArray(undefined)).toBe(false); | |
104 | expect(Utils.isEmptyArray('')).toBe(false); | |
45999aab | 105 | expect(Utils.isEmptyArray('test')).toBe(false); |
a2111e8d JB |
106 | expect(Utils.isEmptyArray(0)).toBe(false); |
107 | expect(Utils.isEmptyArray({})).toBe(false); | |
108 | expect(Utils.isEmptyArray(new Map())).toBe(false); | |
109 | expect(Utils.isEmptyArray(new Set())).toBe(false); | |
110 | }); | |
111 | ||
112 | it('Verify isEmptyObject()', () => { | |
113 | expect(Utils.isEmptyObject({})).toBe(true); | |
67f24396 | 114 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
a2111e8d JB |
115 | expect(Utils.isEmptyObject(null)).toBe(false); |
116 | expect(Utils.isEmptyObject(undefined)).toBe(false); | |
117 | expect(Utils.isEmptyObject(new Map())).toBe(false); | |
118 | expect(Utils.isEmptyObject(new Set())).toBe(false); | |
45999aab JB |
119 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
120 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); | |
a2111e8d JB |
121 | }); |
122 | }); |