refactor(ui): cleanup CSS styles namespace
[e-mobility-charging-stations-simulator.git] / tests / utils / Utils.test.ts
CommitLineData
8f801a93 1import { version } from 'node:process'
66a7748d 2import { describe, it } from 'node:test'
6c43b441 3
66a7748d
JB
4import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'
5import { expect } from 'expect'
8f801a93 6import { satisfies } from 'semver'
a2111e8d 7
66a7748d 8import { Constants } from '../../src/utils/Constants.js'
9bf0ef23 9import {
40615072 10 clone,
9bf0ef23
JB
11 convertToBoolean,
12 convertToDate,
13 convertToFloat,
14 convertToInt,
1553f6df 15 extractTimeSeriesValues,
be4c6702 16 formatDurationMilliSeconds,
8053c842 17 formatDurationSeconds,
9bf0ef23
JB
18 generateUUID,
19 getRandomFloat,
20 getRandomInteger,
21 hasOwnProp,
80c58041 22 isArraySorted,
bcf95df1 23 isAsyncFunction,
9bf0ef23
JB
24 isEmptyArray,
25 isEmptyObject,
26 isEmptyString,
9bf0ef23
JB
27 isNotEmptyArray,
28 isNotEmptyString,
9bf0ef23 29 isObject,
5dc7c990 30 isValidDate,
5adf6ca4
JB
31 max,
32 min,
5f742aac 33 once,
9bf0ef23
JB
34 roundTo,
35 secureRandom,
36 sleep,
66a7748d
JB
37 validateUUID
38} from '../../src/utils/Utils.js'
a2111e8d 39
6c43b441
JB
40await describe('Utils test suite', async () => {
41 await it('Verify generateUUID()/validateUUID()', () => {
66a7748d
JB
42 const uuid = generateUUID()
43 expect(uuid).toBeDefined()
44 expect(uuid.length).toEqual(36)
45 expect(validateUUID(uuid)).toBe(true)
46 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true)
47 expect(validateUUID('')).toBe(false)
878e026c 48 // Shall invalidate Nil UUID
66a7748d
JB
49 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false)
50 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false)
51 })
8bd02502 52
6c43b441 53 await it('Verify sleep()', async () => {
66a7748d
JB
54 const start = performance.now()
55 await sleep(1000)
56 const stop = performance.now()
57 expect(stop - start).toBeGreaterThanOrEqual(1000)
58 })
45999aab 59
6c43b441 60 await it('Verify formatDurationMilliSeconds()', () => {
66a7748d
JB
61 expect(formatDurationMilliSeconds(0)).toBe('0 seconds')
62 expect(formatDurationMilliSeconds(900)).toBe('0 seconds')
63 expect(formatDurationMilliSeconds(1000)).toBe('1 second')
64 expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours')
65 })
be4c6702 66
6c43b441 67 await it('Verify formatDurationSeconds()', () => {
66a7748d
JB
68 expect(formatDurationSeconds(0)).toBe('0 seconds')
69 expect(formatDurationSeconds(0.9)).toBe('0 seconds')
70 expect(formatDurationSeconds(1)).toBe('1 second')
71 expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours')
72 })
8053c842 73
40615072 74 await it('Verify isValidDate()', () => {
5dc7c990
JB
75 expect(isValidDate(undefined)).toBe(false)
76 expect(isValidDate(-1)).toBe(true)
77 expect(isValidDate(0)).toBe(true)
78 expect(isValidDate(1)).toBe(true)
79 expect(isValidDate(-0.5)).toBe(true)
80 expect(isValidDate(0.5)).toBe(true)
81 expect(isValidDate(new Date())).toBe(true)
66a7748d 82 })
ac8178a4 83
6c43b441 84 await it('Verify convertToDate()', () => {
66a7748d 85 expect(convertToDate(undefined)).toBe(undefined)
79fd697f 86 expect(convertToDate(null)).toBe(undefined)
66a7748d
JB
87 expect(() => convertToDate('')).toThrow(new Error("Cannot convert to date: ''"))
88 expect(() => convertToDate('00:70:61')).toThrow(new Error("Cannot convert to date: '00:70:61'"))
89 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'))
90 expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z'))
91 const dateStr = '2020-01-01T00:00:00.000Z'
92 let date = convertToDate(dateStr)
93 expect(date).toBeInstanceOf(Date)
94 expect(date).toStrictEqual(new Date(dateStr))
95 date = convertToDate(new Date(dateStr))
96 expect(date).toBeInstanceOf(Date)
97 expect(date).toStrictEqual(new Date(dateStr))
98 })
df645d8f 99
6c43b441 100 await it('Verify convertToInt()', () => {
66a7748d
JB
101 expect(convertToInt(undefined)).toBe(0)
102 expect(convertToInt(null)).toBe(0)
103 expect(convertToInt(0)).toBe(0)
104 const randomInteger = getRandomInteger()
105 expect(convertToInt(randomInteger)).toEqual(randomInteger)
106 expect(convertToInt('-1')).toBe(-1)
107 expect(convertToInt('1')).toBe(1)
108 expect(convertToInt('1.1')).toBe(1)
109 expect(convertToInt('1.9')).toBe(1)
110 expect(convertToInt('1.999')).toBe(1)
111 expect(convertToInt(-1)).toBe(-1)
112 expect(convertToInt(1)).toBe(1)
113 expect(convertToInt(1.1)).toBe(1)
114 expect(convertToInt(1.9)).toBe(1)
115 expect(convertToInt(1.999)).toBe(1)
878e026c 116 expect(() => {
66a7748d
JB
117 convertToInt('NaN')
118 }).toThrow("Cannot convert to integer: 'NaN'")
119 })
df645d8f 120
6c43b441 121 await it('Verify convertToFloat()', () => {
66a7748d
JB
122 expect(convertToFloat(undefined)).toBe(0)
123 expect(convertToFloat(null)).toBe(0)
124 expect(convertToFloat(0)).toBe(0)
125 const randomFloat = getRandomFloat()
126 expect(convertToFloat(randomFloat)).toEqual(randomFloat)
127 expect(convertToFloat('-1')).toBe(-1)
128 expect(convertToFloat('1')).toBe(1)
129 expect(convertToFloat('1.1')).toBe(1.1)
130 expect(convertToFloat('1.9')).toBe(1.9)
131 expect(convertToFloat('1.999')).toBe(1.999)
132 expect(convertToFloat(-1)).toBe(-1)
133 expect(convertToFloat(1)).toBe(1)
134 expect(convertToFloat(1.1)).toBe(1.1)
135 expect(convertToFloat(1.9)).toBe(1.9)
136 expect(convertToFloat(1.999)).toBe(1.999)
878e026c 137 expect(() => {
66a7748d
JB
138 convertToFloat('NaN')
139 }).toThrow("Cannot convert to float: 'NaN'")
140 })
df645d8f 141
6c43b441 142 await it('Verify convertToBoolean()', () => {
66a7748d
JB
143 expect(convertToBoolean(undefined)).toBe(false)
144 expect(convertToBoolean(null)).toBe(false)
145 expect(convertToBoolean('true')).toBe(true)
146 expect(convertToBoolean('false')).toBe(false)
147 expect(convertToBoolean('TRUE')).toBe(true)
148 expect(convertToBoolean('FALSE')).toBe(false)
149 expect(convertToBoolean('1')).toBe(true)
150 expect(convertToBoolean('0')).toBe(false)
151 expect(convertToBoolean(1)).toBe(true)
152 expect(convertToBoolean(0)).toBe(false)
153 expect(convertToBoolean(true)).toBe(true)
154 expect(convertToBoolean(false)).toBe(false)
155 expect(convertToBoolean('')).toBe(false)
156 expect(convertToBoolean('NoNBoolean')).toBe(false)
157 })
df645d8f 158
6c43b441 159 await it('Verify secureRandom()', () => {
66a7748d
JB
160 const random = secureRandom()
161 expect(typeof random === 'number').toBe(true)
162 expect(random).toBeGreaterThanOrEqual(0)
163 expect(random).toBeLessThan(1)
164 })
a2111e8d 165
6c43b441 166 await it('Verify getRandomInteger()', () => {
66a7748d
JB
167 let randomInteger = getRandomInteger()
168 expect(Number.isSafeInteger(randomInteger)).toBe(true)
169 expect(randomInteger).toBeGreaterThanOrEqual(0)
170 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER)
171 expect(randomInteger).not.toEqual(getRandomInteger())
172 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER)
173 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER)
174 expect(randomInteger).toBeLessThanOrEqual(0)
68f56fdf 175 expect(() => getRandomInteger(0, 1)).toThrow(
66a7748d
JB
176 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1'
177 )
68f56fdf 178 expect(() => getRandomInteger(-1)).toThrow(
66a7748d
JB
179 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0'
180 )
68f56fdf 181 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrow(
878e026c
JB
182 `The value of "max" is out of range. It must be <= ${
183 Constants.MAX_RANDOM_INTEGER + 1
66a7748d
JB
184 }. Received 281_474_976_710_656`
185 )
186 randomInteger = getRandomInteger(2, 1)
187 expect(randomInteger).toBeGreaterThanOrEqual(1)
188 expect(randomInteger).toBeLessThanOrEqual(2)
189 const maximum = 2.2
190 const minimum = 1.1
191 randomInteger = getRandomInteger(maximum, minimum)
192 expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum))
193 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum))
194 })
a2111e8d 195
6c43b441 196 await it('Verify roundTo()', () => {
66a7748d
JB
197 expect(roundTo(0, 2)).toBe(0)
198 expect(roundTo(0.5, 0)).toBe(1)
199 expect(roundTo(0.5, 2)).toBe(0.5)
200 expect(roundTo(-0.5, 0)).toBe(-1)
201 expect(roundTo(-0.5, 2)).toBe(-0.5)
202 expect(roundTo(1.005, 0)).toBe(1)
203 expect(roundTo(1.005, 2)).toBe(1.01)
204 expect(roundTo(2.175, 2)).toBe(2.18)
205 expect(roundTo(5.015, 2)).toBe(5.02)
206 expect(roundTo(-1.005, 2)).toBe(-1.01)
207 expect(roundTo(-2.175, 2)).toBe(-2.18)
208 expect(roundTo(-5.015, 2)).toBe(-5.02)
209 })
316d1564 210
6c43b441 211 await it('Verify getRandomFloat()', () => {
66a7748d
JB
212 let randomFloat = getRandomFloat()
213 expect(typeof randomFloat === 'number').toBe(true)
214 expect(randomFloat).toBeGreaterThanOrEqual(0)
215 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE)
216 expect(randomFloat).not.toEqual(getRandomFloat())
217 expect(() => getRandomFloat(0, 1)).toThrow(new RangeError('Invalid interval'))
68f56fdf 218 expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrow(
66a7748d
JB
219 new RangeError('Invalid interval')
220 )
221 randomFloat = getRandomFloat(0, -Number.MAX_VALUE)
222 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE)
223 expect(randomFloat).toBeLessThanOrEqual(0)
224 })
a2111e8d 225
6c43b441 226 await it('Verify extractTimeSeriesValues()', () => {
66a7748d
JB
227 expect(extractTimeSeriesValues([])).toEqual([])
228 expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1])
1553f6df
JB
229 expect(
230 extractTimeSeriesValues([
231 { timestamp: Date.now(), value: 1.1 },
66a7748d
JB
232 { timestamp: Date.now(), value: 2.2 }
233 ])
234 ).toEqual([1.1, 2.2])
235 })
1553f6df 236
6c43b441 237 await it('Verify isObject()', () => {
66a7748d
JB
238 expect(isObject('test')).toBe(false)
239 expect(isObject(undefined)).toBe(false)
240 expect(isObject(null)).toBe(false)
241 expect(isObject(0)).toBe(false)
242 expect(isObject([])).toBe(false)
243 expect(isObject([0, 1])).toBe(false)
244 expect(isObject(['0', '1'])).toBe(false)
245 expect(isObject({})).toBe(true)
246 expect(isObject({ 1: 1 })).toBe(true)
247 expect(isObject({ 1: '1' })).toBe(true)
248 expect(isObject(new Map())).toBe(true)
249 expect(isObject(new Set())).toBe(true)
250 expect(isObject(new WeakMap())).toBe(true)
251 expect(isObject(new WeakSet())).toBe(true)
252 })
a2111e8d 253
bcf95df1
JB
254 await it('Verify isAsyncFunction()', () => {
255 expect(isAsyncFunction(null)).toBe(false)
256 expect(isAsyncFunction(undefined)).toBe(false)
257 expect(isAsyncFunction(true)).toBe(false)
258 expect(isAsyncFunction(false)).toBe(false)
259 expect(isAsyncFunction(0)).toBe(false)
260 expect(isAsyncFunction('')).toBe(false)
261 expect(isAsyncFunction([])).toBe(false)
262 expect(isAsyncFunction(new Date())).toBe(false)
263 // eslint-disable-next-line prefer-regex-literals
264 expect(isAsyncFunction(new RegExp('[a-z]', 'i'))).toBe(false)
265 expect(isAsyncFunction(new Error())).toBe(false)
266 expect(isAsyncFunction(new Map())).toBe(false)
267 expect(isAsyncFunction(new Set())).toBe(false)
268 expect(isAsyncFunction(new WeakMap())).toBe(false)
269 expect(isAsyncFunction(new WeakSet())).toBe(false)
270 expect(isAsyncFunction(new Int8Array())).toBe(false)
271 expect(isAsyncFunction(new Uint8Array())).toBe(false)
272 expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
273 expect(isAsyncFunction(new Int16Array())).toBe(false)
274 expect(isAsyncFunction(new Uint16Array())).toBe(false)
275 expect(isAsyncFunction(new Int32Array())).toBe(false)
276 expect(isAsyncFunction(new Uint32Array())).toBe(false)
277 expect(isAsyncFunction(new Float32Array())).toBe(false)
278 expect(isAsyncFunction(new Float64Array())).toBe(false)
279 expect(isAsyncFunction(new BigInt64Array())).toBe(false)
280 expect(isAsyncFunction(new BigUint64Array())).toBe(false)
281 // eslint-disable-next-line @typescript-eslint/no-empty-function
282 expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
283 expect(isAsyncFunction(new WeakRef({}))).toBe(false)
284 // eslint-disable-next-line @typescript-eslint/no-empty-function
285 expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
286 expect(isAsyncFunction(new ArrayBuffer(16))).toBe(false)
287 expect(isAsyncFunction(new SharedArrayBuffer(16))).toBe(false)
288 expect(isAsyncFunction(new DataView(new ArrayBuffer(16)))).toBe(false)
289 expect(isAsyncFunction({})).toBe(false)
290 expect(isAsyncFunction({ a: 1 })).toBe(false)
291 // eslint-disable-next-line @typescript-eslint/no-empty-function
292 expect(isAsyncFunction(() => {})).toBe(false)
293 // eslint-disable-next-line @typescript-eslint/no-empty-function
294 expect(isAsyncFunction(function () {})).toBe(false)
295 // eslint-disable-next-line @typescript-eslint/no-empty-function
296 expect(isAsyncFunction(function named () {})).toBe(false)
297 // eslint-disable-next-line @typescript-eslint/no-empty-function
298 expect(isAsyncFunction(async () => {})).toBe(true)
299 // eslint-disable-next-line @typescript-eslint/no-empty-function
300 expect(isAsyncFunction(async function () {})).toBe(true)
301 // eslint-disable-next-line @typescript-eslint/no-empty-function
302 expect(isAsyncFunction(async function named () {})).toBe(true)
303 class TestClass {
304 // eslint-disable-next-line @typescript-eslint/no-empty-function
305 public testSync (): void {}
306 // eslint-disable-next-line @typescript-eslint/no-empty-function
307 public async testAsync (): Promise<void> {}
308 // eslint-disable-next-line @typescript-eslint/no-empty-function
309 public testArrowSync = (): void => {}
310 // eslint-disable-next-line @typescript-eslint/no-empty-function
311 public testArrowAsync = async (): Promise<void> => {}
312 // eslint-disable-next-line @typescript-eslint/no-empty-function
313 public static testStaticSync (): void {}
314 // eslint-disable-next-line @typescript-eslint/no-empty-function
315 public static async testStaticAsync (): Promise<void> {}
316 }
317 const testClass = new TestClass()
318 // eslint-disable-next-line @typescript-eslint/unbound-method
319 expect(isAsyncFunction(testClass.testSync)).toBe(false)
320 // eslint-disable-next-line @typescript-eslint/unbound-method
321 expect(isAsyncFunction(testClass.testAsync)).toBe(true)
322 expect(isAsyncFunction(testClass.testArrowSync)).toBe(false)
323 expect(isAsyncFunction(testClass.testArrowAsync)).toBe(true)
324 // eslint-disable-next-line @typescript-eslint/unbound-method
325 expect(isAsyncFunction(TestClass.testStaticSync)).toBe(false)
326 // eslint-disable-next-line @typescript-eslint/unbound-method
327 expect(isAsyncFunction(TestClass.testStaticAsync)).toBe(true)
328 })
329
40615072 330 await it('Verify clone()', () => {
66a7748d 331 const obj = { 1: 1 }
40615072
JB
332 expect(clone(obj)).toStrictEqual(obj)
333 expect(clone(obj) === obj).toBe(false)
66a7748d 334 const nestedObj = { 1: obj, 2: obj }
40615072
JB
335 expect(clone(nestedObj)).toStrictEqual(nestedObj)
336 expect(clone(nestedObj) === nestedObj).toBe(false)
66a7748d 337 const array = [1, 2]
40615072
JB
338 expect(clone(array)).toStrictEqual(array)
339 expect(clone(array) === array).toBe(false)
66a7748d 340 const objArray = [obj, obj]
40615072
JB
341 expect(clone(objArray)).toStrictEqual(objArray)
342 expect(clone(objArray) === objArray).toBe(false)
66a7748d 343 const date = new Date()
40615072
JB
344 expect(clone(date)).toStrictEqual(date)
345 expect(clone(date) === date).toBe(false)
8f801a93
JB
346 if (satisfies(version, '>=21.0.0')) {
347 const url = new URL('https://domain.tld')
348 expect(() => clone(url)).toThrowError(new Error('Cannot clone object of unsupported type.'))
349 }
66a7748d 350 const map = new Map([['1', '2']])
3fad0dec 351 expect(clone(map)).toStrictEqual(map)
cfab8dc9 352 expect(clone(map) === map).toBe(false)
66a7748d 353 const set = new Set(['1'])
3fad0dec 354 expect(clone(set)).toStrictEqual(set)
cfab8dc9 355 expect(clone(set) === set).toBe(false)
66a7748d 356 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]])
8f801a93 357 expect(() => clone(weakMap)).toThrowError(new Error('#<WeakMap> could not be cloned.'))
66a7748d 358 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }])
8f801a93 359 expect(() => clone(weakSet)).toThrowError(new Error('#<WeakSet> could not be cloned.'))
66a7748d 360 })
dd5a1d6c 361
6c43b441 362 await it('Verify hasOwnProp()', () => {
66a7748d
JB
363 expect(hasOwnProp('test', '')).toBe(false)
364 expect(hasOwnProp(undefined, '')).toBe(false)
365 expect(hasOwnProp(null, '')).toBe(false)
366 expect(hasOwnProp([], '')).toBe(false)
367 expect(hasOwnProp({}, '')).toBe(false)
368 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true)
369 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true)
370 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false)
371 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false)
372 expect(hasOwnProp({ 1: '1' }, '1')).toBe(true)
373 expect(hasOwnProp({ 1: '1' }, 1)).toBe(true)
374 expect(hasOwnProp({ 1: '1' }, '2')).toBe(false)
375 expect(hasOwnProp({ 1: '1' }, 2)).toBe(false)
376 })
af29b598 377
6c43b441 378 await it('Verify isEmptyString()', () => {
66a7748d
JB
379 expect(isEmptyString('')).toBe(true)
380 expect(isEmptyString(' ')).toBe(true)
381 expect(isEmptyString(' ')).toBe(true)
382 expect(isEmptyString('test')).toBe(false)
383 expect(isEmptyString(' test')).toBe(false)
384 expect(isEmptyString('test ')).toBe(false)
385 expect(isEmptyString(undefined)).toBe(true)
386 expect(isEmptyString(null)).toBe(true)
387 expect(isEmptyString(0)).toBe(false)
388 expect(isEmptyString({})).toBe(false)
389 expect(isEmptyString([])).toBe(false)
390 expect(isEmptyString(new Map())).toBe(false)
391 expect(isEmptyString(new Set())).toBe(false)
392 expect(isEmptyString(new WeakMap())).toBe(false)
393 expect(isEmptyString(new WeakSet())).toBe(false)
394 })
5a2a53cf 395
6c43b441 396 await it('Verify isNotEmptyString()', () => {
66a7748d
JB
397 expect(isNotEmptyString('')).toBe(false)
398 expect(isNotEmptyString(' ')).toBe(false)
399 expect(isNotEmptyString(' ')).toBe(false)
400 expect(isNotEmptyString('test')).toBe(true)
401 expect(isNotEmptyString(' test')).toBe(true)
402 expect(isNotEmptyString('test ')).toBe(true)
403 expect(isNotEmptyString(undefined)).toBe(false)
404 expect(isNotEmptyString(null)).toBe(false)
405 expect(isNotEmptyString(0)).toBe(false)
406 expect(isNotEmptyString({})).toBe(false)
407 expect(isNotEmptyString([])).toBe(false)
408 expect(isNotEmptyString(new Map())).toBe(false)
409 expect(isNotEmptyString(new Set())).toBe(false)
410 expect(isNotEmptyString(new WeakMap())).toBe(false)
411 expect(isNotEmptyString(new WeakSet())).toBe(false)
412 })
429f8c9d 413
6c43b441 414 await it('Verify isEmptyArray()', () => {
66a7748d
JB
415 expect(isEmptyArray([])).toBe(true)
416 expect(isEmptyArray([1, 2])).toBe(false)
417 expect(isEmptyArray(['1', '2'])).toBe(false)
418 expect(isEmptyArray(undefined)).toBe(false)
419 expect(isEmptyArray(null)).toBe(false)
420 expect(isEmptyArray('')).toBe(false)
421 expect(isEmptyArray('test')).toBe(false)
422 expect(isEmptyArray(0)).toBe(false)
423 expect(isEmptyArray({})).toBe(false)
424 expect(isEmptyArray(new Map())).toBe(false)
425 expect(isEmptyArray(new Set())).toBe(false)
426 expect(isEmptyArray(new WeakMap())).toBe(false)
427 expect(isEmptyArray(new WeakSet())).toBe(false)
428 })
a2111e8d 429
6c43b441 430 await it('Verify isNotEmptyArray()', () => {
66a7748d
JB
431 expect(isNotEmptyArray([])).toBe(false)
432 expect(isNotEmptyArray([1, 2])).toBe(true)
433 expect(isNotEmptyArray(['1', '2'])).toBe(true)
434 expect(isNotEmptyArray(undefined)).toBe(false)
435 expect(isNotEmptyArray(null)).toBe(false)
436 expect(isNotEmptyArray('')).toBe(false)
437 expect(isNotEmptyArray('test')).toBe(false)
438 expect(isNotEmptyArray(0)).toBe(false)
439 expect(isNotEmptyArray({})).toBe(false)
440 expect(isNotEmptyArray(new Map())).toBe(false)
441 expect(isNotEmptyArray(new Set())).toBe(false)
442 expect(isNotEmptyArray(new WeakMap())).toBe(false)
443 expect(isNotEmptyArray(new WeakSet())).toBe(false)
444 })
878e026c 445
6c43b441 446 await it('Verify isEmptyObject()', () => {
66a7748d
JB
447 expect(isEmptyObject({})).toBe(true)
448 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false)
1c114703
JB
449 expect(isEmptyObject([])).toBe(false)
450 expect(isEmptyObject([1, 2])).toBe(false)
66a7748d
JB
451 expect(isEmptyObject(new Map())).toBe(false)
452 expect(isEmptyObject(new Set())).toBe(false)
453 expect(isEmptyObject(new WeakMap())).toBe(false)
454 expect(isEmptyObject(new WeakSet())).toBe(false)
455 })
80c58041 456
6c43b441 457 await it('Verify isArraySorted()', () => {
80c58041
JB
458 expect(
459 isArraySorted([], (a, b) => {
66a7748d
JB
460 return a - b
461 })
462 ).toBe(true)
80c58041
JB
463 expect(
464 isArraySorted([1], (a, b) => {
66a7748d
JB
465 return a - b
466 })
467 ).toBe(true)
468 expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true)
469 expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false)
470 expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false)
471 })
5f742aac 472
6c43b441 473 await it('Verify once()', () => {
66a7748d
JB
474 let called = 0
475 const fn = (): number => ++called
476 const onceFn = once(fn, this)
477 const result1 = onceFn()
478 expect(called).toBe(1)
479 expect(result1).toBe(1)
480 const result2 = onceFn()
481 expect(called).toBe(1)
482 expect(result2).toBe(1)
483 const result3 = onceFn()
484 expect(called).toBe(1)
485 expect(result3).toBe(1)
486 })
5adf6ca4 487
6c43b441 488 await it('Verify min()', () => {
66a7748d
JB
489 expect(min()).toBe(Infinity)
490 expect(min(0, 1)).toBe(0)
491 expect(min(1, 0)).toBe(0)
492 expect(min(0, -1)).toBe(-1)
493 expect(min(-1, 0)).toBe(-1)
494 })
5adf6ca4 495
6c43b441 496 await it('Verify max()', () => {
66a7748d
JB
497 expect(max()).toBe(-Infinity)
498 expect(max(0, 1)).toBe(1)
499 expect(max(1, 0)).toBe(1)
500 expect(max(0, -1)).toBe(0)
501 expect(max(-1, 0)).toBe(0)
502 })
503})