Commit | Line | Data |
---|---|---|
a2111e8d JB |
1 | import expect from 'expect'; |
2 | ||
3 | import Utils from '../../src/utils/Utils'; | |
4 | ||
5 | describe('Utils test suite', () => { | |
6 | it('Verify secureRandom()', () => { | |
7 | const random = Utils.secureRandom(); | |
8 | expect(typeof random === 'number').toBe(true); | |
9 | expect(random).toBeGreaterThanOrEqual(0); | |
10 | expect(random).toBeLessThan(1); | |
11 | }); | |
12 | ||
13 | it('Verify getRandomInteger()', () => { | |
14 | const randomInteger = Utils.getRandomInteger(); | |
15 | expect(Number.isSafeInteger(randomInteger)).toBe(true); | |
16 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
17 | expect(randomInteger).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER); | |
18 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
19 | expect(() => Utils.getRandomInteger(-1)).toThrowError(new RangeError('Invalid interval')); | |
20 | expect(() => Utils.getRandomInteger(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
21 | }); | |
22 | ||
23 | it('Verify getRandomFloat()', () => { | |
24 | const randomFloat = Utils.getRandomFloat(); | |
25 | expect(typeof randomFloat === 'number').toBe(true); | |
26 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
27 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
28 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); | |
29 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); | |
30 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); | |
31 | }); | |
32 | ||
33 | it('Verify isIterable()', () => { | |
34 | expect(Utils.isIterable('')).toBe(false); | |
35 | expect(Utils.isIterable(' ')).toBe(true); | |
36 | expect(Utils.isIterable('test')).toBe(true); | |
37 | expect(Utils.isIterable(null)).toBe(false); | |
38 | expect(Utils.isIterable(undefined)).toBe(false); | |
39 | expect(Utils.isIterable(0)).toBe(false); | |
40 | expect(Utils.isIterable({})).toBe(false); | |
41 | expect(Utils.isIterable({ 1: 1 })).toBe(false); | |
42 | expect(Utils.isIterable([])).toBe(true); | |
43 | expect(Utils.isIterable(new Map())).toBe(true); | |
44 | expect(Utils.isIterable(new Set())).toBe(true); | |
5f60a7fd JB |
45 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
46 | expect(Utils.isIterable(new WeakSet())).toBe(false); | |
a2111e8d JB |
47 | }); |
48 | ||
49 | it('Verify isEmptyString()', () => { | |
50 | expect(Utils.isEmptyString('')).toBe(true); | |
51 | expect(Utils.isEmptyString(' ')).toBe(true); | |
52 | expect(Utils.isEmptyString(' ')).toBe(true); | |
53 | expect(Utils.isEmptyString('test')).toBe(false); | |
54 | expect(Utils.isEmptyString(' test')).toBe(false); | |
55 | expect(Utils.isEmptyString('test ')).toBe(false); | |
56 | expect(Utils.isEmptyString(null)).toBe(false); | |
57 | expect(Utils.isEmptyString(undefined)).toBe(false); | |
58 | expect(Utils.isEmptyString(0)).toBe(false); | |
59 | expect(Utils.isEmptyString({})).toBe(false); | |
60 | expect(Utils.isEmptyString([])).toBe(false); | |
61 | expect(Utils.isEmptyString(new Map())).toBe(false); | |
62 | expect(Utils.isEmptyString(new Set())).toBe(false); | |
63 | }); | |
64 | ||
65 | it('Verify isNullOrUndefined()', () => { | |
66 | expect(Utils.isNullOrUndefined(null)).toBe(true); | |
67 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); | |
68 | expect(Utils.isNullOrUndefined('')).toBe(false); | |
69 | expect(Utils.isNullOrUndefined(0)).toBe(false); | |
70 | expect(Utils.isNullOrUndefined({})).toBe(false); | |
71 | expect(Utils.isNullOrUndefined([])).toBe(false); | |
72 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); | |
73 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); | |
74 | }); | |
75 | ||
76 | it('Verify isEmptyArray()', () => { | |
77 | expect(Utils.isEmptyArray([])).toBe(true); | |
78 | expect(Utils.isEmptyArray([1, 2])).toBe(false); | |
79 | expect(Utils.isEmptyArray(null)).toBe(false); | |
80 | expect(Utils.isEmptyArray(undefined)).toBe(false); | |
81 | expect(Utils.isEmptyArray('')).toBe(false); | |
82 | expect(Utils.isEmptyArray(0)).toBe(false); | |
83 | expect(Utils.isEmptyArray({})).toBe(false); | |
84 | expect(Utils.isEmptyArray(new Map())).toBe(false); | |
85 | expect(Utils.isEmptyArray(new Set())).toBe(false); | |
86 | }); | |
87 | ||
88 | it('Verify isEmptyObject()', () => { | |
89 | expect(Utils.isEmptyObject({})).toBe(true); | |
90 | expect(Utils.isEmptyObject(null)).toBe(false); | |
91 | expect(Utils.isEmptyObject(undefined)).toBe(false); | |
92 | expect(Utils.isEmptyObject(new Map())).toBe(false); | |
93 | expect(Utils.isEmptyObject(new Set())).toBe(false); | |
94 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); | |
95 | }); | |
96 | }); |