| 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 | randomInteger = Utils.getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER); |
| 114 | expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER); |
| 115 | expect(randomInteger).toBeLessThanOrEqual(0); |
| 116 | expect(() => Utils.getRandomInteger(0, 1)).toThrowError( |
| 117 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1' |
| 118 | ); |
| 119 | expect(() => Utils.getRandomInteger(-1)).toThrowError( |
| 120 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0' |
| 121 | ); |
| 122 | expect(() => Utils.getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( |
| 123 | `The value of "max" is out of range. It must be <= ${ |
| 124 | Constants.MAX_RANDOM_INTEGER + 1 |
| 125 | }. Received 281_474_976_710_656` |
| 126 | ); |
| 127 | randomInteger = Utils.getRandomInteger(2, 1); |
| 128 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
| 129 | expect(randomInteger).toBeLessThanOrEqual(2); |
| 130 | const max = 2.2, |
| 131 | min = 1.1; |
| 132 | randomInteger = Utils.getRandomInteger(max, min); |
| 133 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); |
| 134 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); |
| 135 | }); |
| 136 | |
| 137 | it('Verify getRandomFloat()', () => { |
| 138 | let randomFloat = Utils.getRandomFloat(); |
| 139 | expect(typeof randomFloat === 'number').toBe(true); |
| 140 | expect(randomFloat).toBeGreaterThanOrEqual(0); |
| 141 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 142 | expect(randomFloat).not.toEqual(Utils.getRandomFloat()); |
| 143 | expect(() => Utils.getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); |
| 144 | expect(() => Utils.getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError( |
| 145 | new RangeError('Invalid interval') |
| 146 | ); |
| 147 | randomFloat = Utils.getRandomFloat(0, -Number.MAX_VALUE); |
| 148 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
| 149 | expect(randomFloat).toBeLessThanOrEqual(0); |
| 150 | }); |
| 151 | |
| 152 | it('Verify isIterable()', () => { |
| 153 | expect(Utils.isIterable('')).toBe(true); |
| 154 | expect(Utils.isIterable(' ')).toBe(true); |
| 155 | expect(Utils.isIterable('test')).toBe(true); |
| 156 | expect(Utils.isIterable(undefined)).toBe(false); |
| 157 | expect(Utils.isIterable(null)).toBe(false); |
| 158 | expect(Utils.isIterable(0)).toBe(false); |
| 159 | expect(Utils.isIterable([0, 1])).toBe(true); |
| 160 | expect(Utils.isIterable({ 1: 1 })).toBe(false); |
| 161 | expect(Utils.isIterable(new Map())).toBe(true); |
| 162 | expect(Utils.isIterable(new Set())).toBe(true); |
| 163 | expect(Utils.isIterable(new WeakMap())).toBe(false); |
| 164 | expect(Utils.isIterable(new WeakSet())).toBe(false); |
| 165 | }); |
| 166 | |
| 167 | it('Verify isEmptyString()', () => { |
| 168 | expect(Utils.isEmptyString('')).toBe(true); |
| 169 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 170 | expect(Utils.isEmptyString(' ')).toBe(true); |
| 171 | expect(Utils.isEmptyString('test')).toBe(false); |
| 172 | expect(Utils.isEmptyString(' test')).toBe(false); |
| 173 | expect(Utils.isEmptyString('test ')).toBe(false); |
| 174 | expect(Utils.isEmptyString(undefined)).toBe(true); |
| 175 | expect(Utils.isEmptyString(null)).toBe(true); |
| 176 | expect(Utils.isEmptyString(0)).toBe(false); |
| 177 | expect(Utils.isEmptyString({})).toBe(false); |
| 178 | expect(Utils.isEmptyString([])).toBe(false); |
| 179 | expect(Utils.isEmptyString(new Map())).toBe(false); |
| 180 | expect(Utils.isEmptyString(new Set())).toBe(false); |
| 181 | expect(Utils.isEmptyString(new WeakMap())).toBe(false); |
| 182 | expect(Utils.isEmptyString(new WeakSet())).toBe(false); |
| 183 | }); |
| 184 | |
| 185 | it('Verify isNotEmptyString()', () => { |
| 186 | expect(Utils.isNotEmptyString('')).toBe(false); |
| 187 | expect(Utils.isNotEmptyString(' ')).toBe(false); |
| 188 | expect(Utils.isNotEmptyString(' ')).toBe(false); |
| 189 | expect(Utils.isNotEmptyString('test')).toBe(true); |
| 190 | expect(Utils.isNotEmptyString(' test')).toBe(true); |
| 191 | expect(Utils.isNotEmptyString('test ')).toBe(true); |
| 192 | expect(Utils.isNotEmptyString(undefined)).toBe(false); |
| 193 | expect(Utils.isNotEmptyString(null)).toBe(false); |
| 194 | expect(Utils.isNotEmptyString(0)).toBe(false); |
| 195 | expect(Utils.isNotEmptyString({})).toBe(false); |
| 196 | expect(Utils.isNotEmptyString([])).toBe(false); |
| 197 | expect(Utils.isNotEmptyString(new Map())).toBe(false); |
| 198 | expect(Utils.isNotEmptyString(new Set())).toBe(false); |
| 199 | expect(Utils.isNotEmptyString(new WeakMap())).toBe(false); |
| 200 | expect(Utils.isNotEmptyString(new WeakSet())).toBe(false); |
| 201 | }); |
| 202 | |
| 203 | it('Verify isUndefined()', () => { |
| 204 | expect(Utils.isUndefined(undefined)).toBe(true); |
| 205 | expect(Utils.isUndefined(null)).toBe(false); |
| 206 | expect(Utils.isUndefined('')).toBe(false); |
| 207 | expect(Utils.isUndefined(0)).toBe(false); |
| 208 | expect(Utils.isUndefined({})).toBe(false); |
| 209 | expect(Utils.isUndefined([])).toBe(false); |
| 210 | expect(Utils.isUndefined(new Map())).toBe(false); |
| 211 | expect(Utils.isUndefined(new Set())).toBe(false); |
| 212 | expect(Utils.isUndefined(new WeakMap())).toBe(false); |
| 213 | expect(Utils.isUndefined(new WeakSet())).toBe(false); |
| 214 | }); |
| 215 | |
| 216 | it('Verify isNullOrUndefined()', () => { |
| 217 | expect(Utils.isNullOrUndefined(undefined)).toBe(true); |
| 218 | expect(Utils.isNullOrUndefined(null)).toBe(true); |
| 219 | expect(Utils.isNullOrUndefined('')).toBe(false); |
| 220 | expect(Utils.isNullOrUndefined(0)).toBe(false); |
| 221 | expect(Utils.isNullOrUndefined({})).toBe(false); |
| 222 | expect(Utils.isNullOrUndefined([])).toBe(false); |
| 223 | expect(Utils.isNullOrUndefined(new Map())).toBe(false); |
| 224 | expect(Utils.isNullOrUndefined(new Set())).toBe(false); |
| 225 | expect(Utils.isNullOrUndefined(new WeakMap())).toBe(false); |
| 226 | expect(Utils.isNullOrUndefined(new WeakSet())).toBe(false); |
| 227 | }); |
| 228 | |
| 229 | it('Verify isEmptyArray()', () => { |
| 230 | expect(Utils.isEmptyArray([])).toBe(true); |
| 231 | expect(Utils.isEmptyArray([1, 2])).toBe(false); |
| 232 | expect(Utils.isEmptyArray(['1', '2'])).toBe(false); |
| 233 | expect(Utils.isEmptyArray(undefined)).toBe(false); |
| 234 | expect(Utils.isEmptyArray(null)).toBe(false); |
| 235 | expect(Utils.isEmptyArray('')).toBe(false); |
| 236 | expect(Utils.isEmptyArray('test')).toBe(false); |
| 237 | expect(Utils.isEmptyArray(0)).toBe(false); |
| 238 | expect(Utils.isEmptyArray({})).toBe(false); |
| 239 | expect(Utils.isEmptyArray(new Map())).toBe(false); |
| 240 | expect(Utils.isEmptyArray(new Set())).toBe(false); |
| 241 | expect(Utils.isEmptyArray(new WeakMap())).toBe(false); |
| 242 | expect(Utils.isEmptyArray(new WeakSet())).toBe(false); |
| 243 | }); |
| 244 | |
| 245 | it('Verify isNotEmptyArray()', () => { |
| 246 | expect(Utils.isNotEmptyArray([])).toBe(false); |
| 247 | expect(Utils.isNotEmptyArray([1, 2])).toBe(true); |
| 248 | expect(Utils.isNotEmptyArray(['1', '2'])).toBe(true); |
| 249 | expect(Utils.isNotEmptyArray(undefined)).toBe(false); |
| 250 | expect(Utils.isNotEmptyArray(null)).toBe(false); |
| 251 | expect(Utils.isNotEmptyArray('')).toBe(false); |
| 252 | expect(Utils.isNotEmptyArray('test')).toBe(false); |
| 253 | expect(Utils.isNotEmptyArray(0)).toBe(false); |
| 254 | expect(Utils.isNotEmptyArray({})).toBe(false); |
| 255 | expect(Utils.isNotEmptyArray(new Map())).toBe(false); |
| 256 | expect(Utils.isNotEmptyArray(new Set())).toBe(false); |
| 257 | expect(Utils.isNotEmptyArray(new WeakMap())).toBe(false); |
| 258 | expect(Utils.isNotEmptyArray(new WeakSet())).toBe(false); |
| 259 | }); |
| 260 | |
| 261 | it('Verify isEmptyObject()', () => { |
| 262 | expect(Utils.isEmptyObject({})).toBe(true); |
| 263 | expect(Utils.isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
| 264 | expect(Utils.isEmptyObject(undefined)).toBe(false); |
| 265 | expect(Utils.isEmptyObject(null)).toBe(false); |
| 266 | expect(Utils.isEmptyObject(new Map())).toBe(false); |
| 267 | expect(Utils.isEmptyObject(new Set())).toBe(false); |
| 268 | expect(Utils.isEmptyObject(new WeakMap())).toBe(false); |
| 269 | expect(Utils.isEmptyObject(new WeakSet())).toBe(false); |
| 270 | }); |
| 271 | }); |