| 1 | import expect from 'expect'; |
| 2 | |
| 3 | import Constants from '../../src/utils/Constants'; |
| 4 | import Utils from '../../src/utils/Utils'; |
| 5 | |
| 6 | describe('Utils test suite', () => { |
| 7 | it('Verify generateUUID()/validateUUID()', () => { |
| 8 | const uuid = Utils.generateUUID(); |
| 9 | expect(uuid).toBeDefined(); |
| 10 | expect(uuid.length).toEqual(36); |
| 11 | expect(Utils.validateUUID(uuid)).toBe(true); |
| 12 | expect(Utils.validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); |
| 13 | expect(Utils.validateUUID('')).toBe(false); |
| 14 | // Shall invalidate Nil UUID |
| 15 | expect(Utils.validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); |
| 16 | expect(Utils.validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); |
| 17 | }); |
| 18 | |
| 19 | it('Verify sleep()', async () => { |
| 20 | const start = Date.now(); |
| 21 | await Utils.sleep(1000); |
| 22 | const end = Date.now(); |
| 23 | expect(end - start).toBeGreaterThanOrEqual(1000); |
| 24 | }); |
| 25 | |
| 26 | it('Verify convertToDate()', () => { |
| 27 | expect(Utils.convertToDate(undefined)).toBe(undefined); |
| 28 | expect(Utils.convertToDate(null)).toBe(null); |
| 29 | const invalidDate = Utils.convertToDate(''); |
| 30 | expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); |
| 31 | expect(Utils.convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); |
| 32 | const dateStr = '2020-01-01T00:00:00.000Z'; |
| 33 | let date = Utils.convertToDate(dateStr); |
| 34 | expect(date).toBeInstanceOf(Date); |
| 35 | expect(date).toStrictEqual(new Date(dateStr)); |
| 36 | date = Utils.convertToDate(new Date(dateStr)); |
| 37 | expect(date).toBeInstanceOf(Date); |
| 38 | expect(date).toStrictEqual(new Date(dateStr)); |
| 39 | }); |
| 40 | |
| 41 | it('Verify convertToInt()', () => { |
| 42 | expect(Utils.convertToInt(undefined)).toBe(0); |
| 43 | expect(Utils.convertToInt(null)).toBe(0); |
| 44 | expect(Utils.convertToInt(0)).toBe(0); |
| 45 | const randomInteger = Utils.getRandomInteger(); |
| 46 | expect(Utils.convertToInt(randomInteger)).toEqual(randomInteger); |
| 47 | expect(Utils.convertToInt('-1')).toBe(-1); |
| 48 | expect(Utils.convertToInt('1')).toBe(1); |
| 49 | expect(Utils.convertToInt('1.1')).toBe(1); |
| 50 | expect(Utils.convertToInt('1.9')).toBe(1); |
| 51 | expect(Utils.convertToInt('1.999')).toBe(1); |
| 52 | expect(Utils.convertToInt(-1)).toBe(-1); |
| 53 | expect(Utils.convertToInt(1)).toBe(1); |
| 54 | expect(Utils.convertToInt(1.1)).toBe(1); |
| 55 | expect(Utils.convertToInt(1.9)).toBe(1); |
| 56 | expect(Utils.convertToInt(1.999)).toBe(1); |
| 57 | expect(() => { |
| 58 | Utils.convertToInt('NaN'); |
| 59 | }).toThrow('Cannot convert to integer: NaN'); |
| 60 | }); |
| 61 | |
| 62 | it('Verify convertToFloat()', () => { |
| 63 | expect(Utils.convertToFloat(undefined)).toBe(0); |
| 64 | expect(Utils.convertToFloat(null)).toBe(0); |
| 65 | expect(Utils.convertToFloat(0)).toBe(0); |
| 66 | const randomFloat = Utils.getRandomFloat(); |
| 67 | expect(Utils.convertToFloat(randomFloat)).toEqual(randomFloat); |
| 68 | expect(Utils.convertToFloat('-1')).toBe(-1); |
| 69 | expect(Utils.convertToFloat('1')).toBe(1); |
| 70 | expect(Utils.convertToFloat('1.1')).toBe(1.1); |
| 71 | expect(Utils.convertToFloat('1.9')).toBe(1.9); |
| 72 | expect(Utils.convertToFloat('1.999')).toBe(1.999); |
| 73 | expect(Utils.convertToFloat(-1)).toBe(-1); |
| 74 | expect(Utils.convertToFloat(1)).toBe(1); |
| 75 | expect(Utils.convertToFloat(1.1)).toBe(1.1); |
| 76 | expect(Utils.convertToFloat(1.9)).toBe(1.9); |
| 77 | expect(Utils.convertToFloat(1.999)).toBe(1.999); |
| 78 | expect(() => { |
| 79 | Utils.convertToFloat('NaN'); |
| 80 | }).toThrow('Cannot convert to float: NaN'); |
| 81 | }); |
| 82 | |
| 83 | it('Verify convertToBoolean()', () => { |
| 84 | expect(Utils.convertToBoolean(undefined)).toBe(false); |
| 85 | expect(Utils.convertToBoolean(null)).toBe(false); |
| 86 | expect(Utils.convertToBoolean('true')).toBe(true); |
| 87 | expect(Utils.convertToBoolean('false')).toBe(false); |
| 88 | expect(Utils.convertToBoolean('TRUE')).toBe(true); |
| 89 | expect(Utils.convertToBoolean('FALSE')).toBe(false); |
| 90 | expect(Utils.convertToBoolean('1')).toBe(true); |
| 91 | expect(Utils.convertToBoolean('0')).toBe(false); |
| 92 | expect(Utils.convertToBoolean(1)).toBe(true); |
| 93 | expect(Utils.convertToBoolean(0)).toBe(false); |
| 94 | expect(Utils.convertToBoolean(true)).toBe(true); |
| 95 | expect(Utils.convertToBoolean(false)).toBe(false); |
| 96 | expect(Utils.convertToBoolean('')).toBe(false); |
| 97 | expect(Utils.convertToBoolean('NoNBoolean')).toBe(false); |
| 98 | }); |
| 99 | |
| 100 | it('Verify secureRandom()', () => { |
| 101 | const random = Utils.secureRandom(); |
| 102 | expect(typeof random === 'number').toBe(true); |
| 103 | expect(random).toBeGreaterThanOrEqual(0); |
| 104 | expect(random).toBeLessThan(1); |
| 105 | }); |
| 106 | |
| 107 | it('Verify getRandomInteger()', () => { |
| 108 | let randomInteger = Utils.getRandomInteger(); |
| 109 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
| 110 | expect(randomInteger).toBeGreaterThanOrEqual(0); |
| 111 | expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER); |
| 112 | expect(randomInteger).not.toEqual(Utils.getRandomInteger()); |
| 113 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError( |
| 114 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1' |
| 115 | ); |
| 116 | expect(() => Utils.getRandomInteger(-1)).toThrowError( |
| 117 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0' |
| 118 | ); |
| 119 | expect(() => Utils.getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( |
| 120 | `The value of "max" is out of range. It must be <= ${ |
| 121 | Constants.MAX_RANDOM_INTEGER + 1 |
| 122 | }. Received 281_474_976_710_656` |
| 123 | ); |
| 124 | randomInteger = Utils.getRandomInteger(2, 1); |
| 125 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
| 126 | expect(randomInteger).toBeLessThanOrEqual(2); |
| 127 | const max = 2.2, |
| 128 | min = 1.1; |
| 129 | randomInteger = Utils.getRandomInteger(max, min); |
| 130 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); |
| 131 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); |
| 132 | }); |
| 133 | |
| 134 | it('Verify getRandomFloat()', () => { |
| 135 | let randomFloat = Utils.getRandomFloat(); |
| 136 | expect(typeof randomFloat === 'number').toBe(true); |
| 137 | expect(randomFloat).toBeGreaterThanOrEqual(0); |
| 138 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 139 | expect(randomFloat).not.toEqual(Utils.getRandomFloat()); |
| 140 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); |
| 141 | expect(() => Utils.getRandomFloat(-1)).toThrowError(new RangeError('Invalid interval')); |
| 142 | expect(() => Utils.getRandomFloat(0, -1)).toThrowError(new RangeError('Invalid interval')); |
| 143 | randomFloat = Utils.getRandomFloat(Number.MAX_VALUE, 0, true); |
| 144 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
| 145 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 146 | }); |
| 147 | |
| 148 | it('Verify isIterable()', () => { |
| 149 | expect(Utils.isIterable('')).toBe(false); |
| 150 | expect(Utils.isIterable(' ')).toBe(true); |
| 151 | expect(Utils.isIterable('test')).toBe(true); |
| 152 | expect(Utils.isIterable(null)).toBe(false); |
| 153 | expect(Utils.isIterable(undefined)).toBe(false); |
| 154 | expect(Utils.isIterable(0)).toBe(false); |
| 155 | expect(Utils.isIterable({})).toBe(false); |
| 156 | expect(Utils.isIterable({ 1: 1 })).toBe(false); |
| 157 | expect(Utils.isIterable([])).toBe(true); |
| 158 | expect(Utils.isIterable(new Map())).toBe(true); |
| 159 | expect(Utils.isIterable(new Set())).toBe(true); |
| 160 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
| 161 | expect(Utils.isIterable(new WeakSet())).toBe(false); |
| 162 | }); |
| 163 | |
| 164 | it('Verify isEmptyString()', () => { |
| 165 | expect(Utils.isEmptyString('')).toBe(true); |
| 166 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 167 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 168 | expect(Utils.isEmptyString('test')).toBe(false); |
| 169 | expect(Utils.isEmptyString(' test')).toBe(false); |
| 170 | expect(Utils.isEmptyString('test ')).toBe(false); |
| 171 | expect(Utils.isEmptyString(null)).toBe(false); |
| 172 | expect(Utils.isEmptyString(undefined)).toBe(false); |
| 173 | expect(Utils.isEmptyString(0)).toBe(false); |
| 174 | expect(Utils.isEmptyString({})).toBe(false); |
| 175 | expect(Utils.isEmptyString([])).toBe(false); |
| 176 | expect(Utils.isEmptyString(new Map())).toBe(false); |
| 177 | expect(Utils.isEmptyString(new Set())).toBe(false); |
| 178 | expect(Utils.isEmptyString(new WeakMap())).toBe(false); |
| 179 | expect(Utils.isEmptyString(new WeakSet())).toBe(false); |
| 180 | }); |
| 181 | |
| 182 | it('Verify isUndefined()', () => { |
| 183 | expect(Utils.isUndefined(undefined)).toBe(true); |
| 184 | expect(Utils.isUndefined(null)).toBe(false); |
| 185 | expect(Utils.isUndefined('')).toBe(false); |
| 186 | expect(Utils.isUndefined(0)).toBe(false); |
| 187 | expect(Utils.isUndefined({})).toBe(false); |
| 188 | expect(Utils.isUndefined([])).toBe(false); |
| 189 | expect(Utils.isUndefined(new Map())).toBe(false); |
| 190 | expect(Utils.isUndefined(new Set())).toBe(false); |
| 191 | expect(Utils.isUndefined(new WeakMap())).toBe(false); |
| 192 | expect(Utils.isUndefined(new WeakSet())).toBe(false); |
| 193 | }); |
| 194 | |
| 195 | it('Verify isNullOrUndefined()', () => { |
| 196 | expect(Utils.isNullOrUndefined(null)).toBe(true); |
| 197 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); |
| 198 | expect(Utils.isNullOrUndefined('')).toBe(false); |
| 199 | expect(Utils.isNullOrUndefined(0)).toBe(false); |
| 200 | expect(Utils.isNullOrUndefined({})).toBe(false); |
| 201 | expect(Utils.isNullOrUndefined([])).toBe(false); |
| 202 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); |
| 203 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); |
| 204 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
| 205 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); |
| 206 | }); |
| 207 | |
| 208 | it('Verify isEmptyArray()', () => { |
| 209 | expect(Utils.isEmptyArray([])).toBe(true); |
| 210 | expect(Utils.isEmptyArray([1, 2])).toBe(false); |
| 211 | expect(Utils.isEmptyArray(null)).toBe(true); |
| 212 | expect(Utils.isEmptyArray(undefined)).toBe(true); |
| 213 | expect(Utils.isEmptyArray('')).toBe(true); |
| 214 | expect(Utils.isEmptyArray('test')).toBe(true); |
| 215 | expect(Utils.isEmptyArray(0)).toBe(true); |
| 216 | expect(Utils.isEmptyArray({})).toBe(true); |
| 217 | expect(Utils.isEmptyArray(new Map())).toBe(true); |
| 218 | expect(Utils.isEmptyArray(new Set())).toBe(true); |
| 219 | expect(Utils.isEmptyArray(new WeakMap())).toBe(true); |
| 220 | expect(Utils.isEmptyArray(new WeakSet())).toBe(true); |
| 221 | }); |
| 222 | |
| 223 | it('Verify isEmptyObject()', () => { |
| 224 | expect(Utils.isEmptyObject({})).toBe(true); |
| 225 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
| 226 | expect(Utils.isEmptyObject(null)).toBe(false); |
| 227 | expect(Utils.isEmptyObject(undefined)).toBe(false); |
| 228 | expect(Utils.isEmptyObject(new Map())).toBe(false); |
| 229 | expect(Utils.isEmptyObject(new Set())).toBe(false); |
| 230 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
| 231 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); |
| 232 | }); |
| 233 | }); |