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(); | |
8 | expect(uuid.length).toEqual(36); | |
9 | expect(Utils.validateUUID(uuid)).toBe(true); | |
96642078 JB |
10 | expect(Utils.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); |
11 | expect(Utils.validateUUID('')).toBe(false); | |
12 | expect(Utils.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); | |
8bd02502 JB |
13 | }); |
14 | ||
a2111e8d JB |
15 | it('Verify secureRandom()', () => { |
16 | const random = Utils.secureRandom(); | |
17 | expect(typeof random === 'number').toBe(true); | |
18 | expect(random).toBeGreaterThanOrEqual(0); | |
19 | expect(random).toBeLessThan(1); | |
20 | }); | |
21 | ||
22 | it('Verify getRandomInteger()', () => { | |
23 | const randomInteger = Utils.getRandomInteger(); | |
24 | expect(Number.isSafeInteger(randomInteger)).toBe(true); | |
25 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
26 | expect(randomInteger).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER); | |
27 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
28 | expect(() => Utils.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval')); | |
29 | expect(() => Utils.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
30 | }); | |
31 | ||
32 | it('Verify getRandomFloat()', () => { | |
33 | const randomFloat = Utils.getRandomFloat(); | |
34 | expect(typeof randomFloat === 'number').toBe(true); | |
35 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
36 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
37 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
38 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); | |
39 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
40 | }); | |
41 | ||
42 | it('Verify isIterable()', () => { | |
43 | expect(Utils.isIterable('')).toBe(false); | |
44 | expect(Utils.isIterable(' ')).toBe(true); | |
45 | expect(Utils.isIterable('test')).toBe(true); | |
46 | expect(Utils.isIterable(null)).toBe(false); | |
47 | expect(Utils.isIterable(undefined)).toBe(false); | |
48 | expect(Utils.isIterable(0)).toBe(false); | |
49 | expect(Utils.isIterable({})).toBe(false); | |
50 | expect(Utils.isIterable({ 1: 1 })).toBe(false); | |
51 | expect(Utils.isIterable([])).toBe(true); | |
52 | expect(Utils.isIterable(new Map())).toBe(true); | |
53 | expect(Utils.isIterable(new Set())).toBe(true); | |
5f60a7fd JB |
54 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
55 | expect(Utils.isIterable(new WeakSet())).toBe(false); | |
a2111e8d JB |
56 | }); |
57 | ||
58 | it('Verify isEmptyString()', () => { | |
59 | expect(Utils.isEmptyString('')).toBe(true); | |
60 | expect(Utils.isEmptyString(' ')).toBe(true); | |
61 | expect(Utils.isEmptyString(' ')).toBe(true); | |
62 | expect(Utils.isEmptyString('test')).toBe(false); | |
63 | expect(Utils.isEmptyString(' test')).toBe(false); | |
64 | expect(Utils.isEmptyString('test ')).toBe(false); | |
65 | expect(Utils.isEmptyString(null)).toBe(false); | |
66 | expect(Utils.isEmptyString(undefined)).toBe(false); | |
67 | expect(Utils.isEmptyString(0)).toBe(false); | |
68 | expect(Utils.isEmptyString({})).toBe(false); | |
69 | expect(Utils.isEmptyString([])).toBe(false); | |
70 | expect(Utils.isEmptyString(new Map())).toBe(false); | |
71 | expect(Utils.isEmptyString(new Set())).toBe(false); | |
72 | }); | |
73 | ||
74 | it('Verify isNullOrUndefined()', () => { | |
75 | expect(Utils.isNullOrUndefined(null)).toBe(true); | |
76 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); | |
77 | expect(Utils.isNullOrUndefined('')).toBe(false); | |
78 | expect(Utils.isNullOrUndefined(0)).toBe(false); | |
79 | expect(Utils.isNullOrUndefined({})).toBe(false); | |
80 | expect(Utils.isNullOrUndefined([])).toBe(false); | |
81 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); | |
82 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); | |
83 | }); | |
84 | ||
85 | it('Verify isEmptyArray()', () => { | |
86 | expect(Utils.isEmptyArray([])).toBe(true); | |
87 | expect(Utils.isEmptyArray([1, 2])).toBe(false); | |
88 | expect(Utils.isEmptyArray(null)).toBe(false); | |
89 | expect(Utils.isEmptyArray(undefined)).toBe(false); | |
90 | expect(Utils.isEmptyArray('')).toBe(false); | |
91 | expect(Utils.isEmptyArray(0)).toBe(false); | |
92 | expect(Utils.isEmptyArray({})).toBe(false); | |
93 | expect(Utils.isEmptyArray(new Map())).toBe(false); | |
94 | expect(Utils.isEmptyArray(new Set())).toBe(false); | |
95 | }); | |
96 | ||
97 | it('Verify isEmptyObject()', () => { | |
98 | expect(Utils.isEmptyObject({})).toBe(true); | |
67f24396 | 99 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
a2111e8d JB |
100 | expect(Utils.isEmptyObject(null)).toBe(false); |
101 | expect(Utils.isEmptyObject(undefined)).toBe(false); | |
102 | expect(Utils.isEmptyObject(new Map())).toBe(false); | |
103 | expect(Utils.isEmptyObject(new Set())).toBe(false); | |
a2111e8d JB |
104 | }); |
105 | }); |