diff --git a/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts b/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts new file mode 100644 index 00000000..b015529d --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts @@ -0,0 +1,23 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; + +import { getSelf } from './getSelf'; + +describe(getSelf.name, () => { + let value: unknown; + + beforeAll(() => { + value = Symbol(); + }); + + describe('when called', () => { + let result: unknown; + + beforeAll(() => { + result = getSelf(value); + }); + + it('should return expected value', () => { + expect(result).toBe(value); + }); + }); +}); diff --git a/packages/container/libraries/core/src/common/calculations/getSelf.ts b/packages/container/libraries/core/src/common/calculations/getSelf.ts new file mode 100644 index 00000000..0a0e6434 --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/getSelf.ts @@ -0,0 +1,3 @@ +export function getSelf(self: T): T { + return self; +} diff --git a/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts b/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts new file mode 100644 index 00000000..a3054fdf --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts @@ -0,0 +1,56 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; + +import { isPromise } from './isPromise'; + +describe(isPromise.name, () => { + describe.each<[string, unknown, boolean]>([ + ['null', null, false], + ['a string', 'string-fixture', false], + ['a function with no "then" property', () => undefined, false], + ['an object with no "then" property', {}, false], + [ + 'a function with non function "then" property', + (() => { + const value: (() => void) & { + then?: unknown; + } = () => undefined; + + value.then = 'fixture'; + + return value; + })(), + false, + ], + ['an object with non function "then" property', { then: 'fixture' }, false], + [ + 'a function with function "then" property', + (() => { + const value: (() => void) & { + then?: unknown; + } = () => undefined; + + value.then = () => undefined; + + return value; + })(), + true, + ], + [ + 'an object with function "then" property', + { then: () => undefined }, + true, + ], + ])('having %s', (_: string, value: unknown, expectedResult: boolean) => { + describe('when called', () => { + let result: unknown; + + beforeAll(() => { + result = isPromise(value); + }); + + it('should return expected value', () => { + expect(result).toBe(expectedResult); + }); + }); + }); +}); diff --git a/packages/container/libraries/core/src/common/calculations/isPromise.ts b/packages/container/libraries/core/src/common/calculations/isPromise.ts new file mode 100644 index 00000000..d7b43f82 --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/isPromise.ts @@ -0,0 +1,9 @@ +export function isPromise(object: unknown): object is Promise { + const isObjectOrFunction: boolean = + (typeof object === 'object' && object !== null) || + typeof object === 'function'; + + return ( + isObjectOrFunction && typeof (object as PromiseLike).then === 'function' + ); +}