| 1 | import expect from 'expect'; |
| 2 | |
| 3 | import Utils from '../../src/utils/Utils'; |
| 4 | |
| 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); |
| 16 | }); |
| 17 | |
| 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 | |
| 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 | let 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 | randomInteger = Utils.getRandomInteger(2, 1); |
| 41 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
| 42 | expect(randomInteger).toBeLessThanOrEqual(2); |
| 43 | }); |
| 44 | |
| 45 | it('Verify getRandomFloat()', () => { |
| 46 | let randomFloat = Utils.getRandomFloat(); |
| 47 | expect(typeof randomFloat === 'number').toBe(true); |
| 48 | expect(randomFloat).toBeGreaterThanOrEqual(0); |
| 49 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 50 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); |
| 51 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); |
| 52 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); |
| 53 | randomFloat = Utils.getRandomFloat(Number.MAX_VALUE, 0, true); |
| 54 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
| 55 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 56 | }); |
| 57 | |
| 58 | it('Verify isIterable()', () => { |
| 59 | expect(Utils.isIterable('')).toBe(false); |
| 60 | expect(Utils.isIterable(' ')).toBe(true); |
| 61 | expect(Utils.isIterable('test')).toBe(true); |
| 62 | expect(Utils.isIterable(null)).toBe(false); |
| 63 | expect(Utils.isIterable(undefined)).toBe(false); |
| 64 | expect(Utils.isIterable(0)).toBe(false); |
| 65 | expect(Utils.isIterable({})).toBe(false); |
| 66 | expect(Utils.isIterable({ 1: 1 })).toBe(false); |
| 67 | expect(Utils.isIterable([])).toBe(true); |
| 68 | expect(Utils.isIterable(new Map())).toBe(true); |
| 69 | expect(Utils.isIterable(new Set())).toBe(true); |
| 70 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
| 71 | expect(Utils.isIterable(new WeakSet())).toBe(false); |
| 72 | }); |
| 73 | |
| 74 | it('Verify isEmptyString()', () => { |
| 75 | expect(Utils.isEmptyString('')).toBe(true); |
| 76 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 77 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 78 | expect(Utils.isEmptyString('test')).toBe(false); |
| 79 | expect(Utils.isEmptyString(' test')).toBe(false); |
| 80 | expect(Utils.isEmptyString('test ')).toBe(false); |
| 81 | expect(Utils.isEmptyString(null)).toBe(false); |
| 82 | expect(Utils.isEmptyString(undefined)).toBe(false); |
| 83 | expect(Utils.isEmptyString(0)).toBe(false); |
| 84 | expect(Utils.isEmptyString({})).toBe(false); |
| 85 | expect(Utils.isEmptyString([])).toBe(false); |
| 86 | expect(Utils.isEmptyString(new Map())).toBe(false); |
| 87 | expect(Utils.isEmptyString(new Set())).toBe(false); |
| 88 | expect(Utils.isEmptyString(new WeakMap())).toBe(false); |
| 89 | expect(Utils.isEmptyString(new WeakSet())).toBe(false); |
| 90 | }); |
| 91 | |
| 92 | it('Verify isUndefined()', () => { |
| 93 | expect(Utils.isUndefined(undefined)).toBe(true); |
| 94 | expect(Utils.isUndefined(null)).toBe(false); |
| 95 | expect(Utils.isUndefined('')).toBe(false); |
| 96 | expect(Utils.isUndefined(0)).toBe(false); |
| 97 | expect(Utils.isUndefined({})).toBe(false); |
| 98 | expect(Utils.isUndefined([])).toBe(false); |
| 99 | expect(Utils.isUndefined(new Map())).toBe(false); |
| 100 | expect(Utils.isUndefined(new Set())).toBe(false); |
| 101 | expect(Utils.isUndefined(new WeakMap())).toBe(false); |
| 102 | expect(Utils.isUndefined(new WeakSet())).toBe(false); |
| 103 | }); |
| 104 | |
| 105 | it('Verify isNullOrUndefined()', () => { |
| 106 | expect(Utils.isNullOrUndefined(null)).toBe(true); |
| 107 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); |
| 108 | expect(Utils.isNullOrUndefined('')).toBe(false); |
| 109 | expect(Utils.isNullOrUndefined(0)).toBe(false); |
| 110 | expect(Utils.isNullOrUndefined({})).toBe(false); |
| 111 | expect(Utils.isNullOrUndefined([])).toBe(false); |
| 112 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); |
| 113 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); |
| 114 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
| 115 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); |
| 116 | }); |
| 117 | |
| 118 | it('Verify isEmptyArray()', () => { |
| 119 | expect(Utils.isEmptyArray([])).toBe(true); |
| 120 | expect(Utils.isEmptyArray([1, 2])).toBe(false); |
| 121 | expect(Utils.isEmptyArray(null)).toBe(true); |
| 122 | expect(Utils.isEmptyArray(undefined)).toBe(true); |
| 123 | expect(Utils.isEmptyArray('')).toBe(true); |
| 124 | expect(Utils.isEmptyArray('test')).toBe(true); |
| 125 | expect(Utils.isEmptyArray(0)).toBe(true); |
| 126 | expect(Utils.isEmptyArray({})).toBe(true); |
| 127 | expect(Utils.isEmptyArray(new Map())).toBe(true); |
| 128 | expect(Utils.isEmptyArray(new Set())).toBe(true); |
| 129 | expect(Utils.isEmptyArray(new WeakMap())).toBe(true); |
| 130 | expect(Utils.isEmptyArray(new WeakSet())).toBe(true); |
| 131 | }); |
| 132 | |
| 133 | it('Verify isEmptyObject()', () => { |
| 134 | expect(Utils.isEmptyObject({})).toBe(true); |
| 135 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
| 136 | expect(Utils.isEmptyObject(null)).toBe(false); |
| 137 | expect(Utils.isEmptyObject(undefined)).toBe(false); |
| 138 | expect(Utils.isEmptyObject(new Map())).toBe(false); |
| 139 | expect(Utils.isEmptyObject(new Set())).toBe(false); |
| 140 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
| 141 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); |
| 142 | }); |
| 143 | }); |