X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Futils%2FUtils.test.ts;h=0ed7d6b67a54eea7d96aff3ed2a40c2675a99413;hb=8f9060ba0f4e2e22053ceb34f357bb8a1263d889;hp=bef31dd97eaf0512db552841410be0c4def5841d;hpb=1c1147036bb68c12685b2e182f910689646b3b11;p=e-mobility-charging-stations-simulator.git diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index bef31dd9..0ed7d6b6 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -1,7 +1,9 @@ +import { version } from 'node:process' import { describe, it } from 'node:test' import { hoursToMilliseconds, hoursToSeconds } from 'date-fns' import { expect } from 'expect' +import { satisfies } from 'semver' import { Constants } from '../../src/utils/Constants.js' import { @@ -18,6 +20,7 @@ import { getRandomInteger, hasOwnProp, isArraySorted, + isAsyncFunction, isEmptyArray, isEmptyObject, isEmptyString, @@ -248,6 +251,82 @@ await describe('Utils test suite', async () => { expect(isObject(new WeakSet())).toBe(true) }) + await it('Verify isAsyncFunction()', () => { + expect(isAsyncFunction(null)).toBe(false) + expect(isAsyncFunction(undefined)).toBe(false) + expect(isAsyncFunction(true)).toBe(false) + expect(isAsyncFunction(false)).toBe(false) + expect(isAsyncFunction(0)).toBe(false) + expect(isAsyncFunction('')).toBe(false) + expect(isAsyncFunction([])).toBe(false) + expect(isAsyncFunction(new Date())).toBe(false) + // eslint-disable-next-line prefer-regex-literals + expect(isAsyncFunction(new RegExp('[a-z]', 'i'))).toBe(false) + expect(isAsyncFunction(new Error())).toBe(false) + expect(isAsyncFunction(new Map())).toBe(false) + expect(isAsyncFunction(new Set())).toBe(false) + expect(isAsyncFunction(new WeakMap())).toBe(false) + expect(isAsyncFunction(new WeakSet())).toBe(false) + expect(isAsyncFunction(new Int8Array())).toBe(false) + expect(isAsyncFunction(new Uint8Array())).toBe(false) + expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false) + expect(isAsyncFunction(new Int16Array())).toBe(false) + expect(isAsyncFunction(new Uint16Array())).toBe(false) + expect(isAsyncFunction(new Int32Array())).toBe(false) + expect(isAsyncFunction(new Uint32Array())).toBe(false) + expect(isAsyncFunction(new Float32Array())).toBe(false) + expect(isAsyncFunction(new Float64Array())).toBe(false) + expect(isAsyncFunction(new BigInt64Array())).toBe(false) + expect(isAsyncFunction(new BigUint64Array())).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(new Promise(() => {}))).toBe(false) + expect(isAsyncFunction(new WeakRef({}))).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false) + expect(isAsyncFunction(new ArrayBuffer(16))).toBe(false) + expect(isAsyncFunction(new SharedArrayBuffer(16))).toBe(false) + expect(isAsyncFunction(new DataView(new ArrayBuffer(16)))).toBe(false) + expect(isAsyncFunction({})).toBe(false) + expect(isAsyncFunction({ a: 1 })).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(() => {})).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(function () {})).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(function named () {})).toBe(false) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(async () => {})).toBe(true) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(async function () {})).toBe(true) + // eslint-disable-next-line @typescript-eslint/no-empty-function + expect(isAsyncFunction(async function named () {})).toBe(true) + class TestClass { + // eslint-disable-next-line @typescript-eslint/no-empty-function + public testSync (): void {} + // eslint-disable-next-line @typescript-eslint/no-empty-function + public async testAsync (): Promise {} + // eslint-disable-next-line @typescript-eslint/no-empty-function + public testArrowSync = (): void => {} + // eslint-disable-next-line @typescript-eslint/no-empty-function + public testArrowAsync = async (): Promise => {} + // eslint-disable-next-line @typescript-eslint/no-empty-function + public static testStaticSync (): void {} + // eslint-disable-next-line @typescript-eslint/no-empty-function + public static async testStaticAsync (): Promise {} + } + const testClass = new TestClass() + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(isAsyncFunction(testClass.testSync)).toBe(false) + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(isAsyncFunction(testClass.testAsync)).toBe(true) + expect(isAsyncFunction(testClass.testArrowSync)).toBe(false) + expect(isAsyncFunction(testClass.testArrowAsync)).toBe(true) + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(isAsyncFunction(TestClass.testStaticSync)).toBe(false) + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(isAsyncFunction(TestClass.testStaticAsync)).toBe(true) + }) + await it('Verify clone()', () => { const obj = { 1: 1 } expect(clone(obj)).toStrictEqual(obj) @@ -264,17 +343,20 @@ await describe('Utils test suite', async () => { const date = new Date() expect(clone(date)).toStrictEqual(date) expect(clone(date) === date).toBe(false) + if (satisfies(version, '>=21.0.0')) { + const url = new URL('https://domain.tld') + expect(() => clone(url)).toThrowError(new Error('Cannot clone object of unsupported type.')) + } const map = new Map([['1', '2']]) - expect(clone(map)).toStrictEqual({}) + expect(clone(map)).toStrictEqual(map) + expect(clone(map) === map).toBe(false) const set = new Set(['1']) - expect(clone(set)).toStrictEqual({}) - // The URL object seems to have not enumerable properties - const url = new URL('https://domain.tld') - expect(clone(url)).toStrictEqual({}) + expect(clone(set)).toStrictEqual(set) + expect(clone(set) === set).toBe(false) const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]) - expect(clone(weakMap)).toStrictEqual({}) + expect(() => clone(weakMap)).toThrowError(new Error('# could not be cloned.')) const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]) - expect(clone(weakSet)).toStrictEqual({}) + expect(() => clone(weakSet)).toThrowError(new Error('# could not be cloned.')) }) await it('Verify hasOwnProp()', () => {