diff --git a/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.spec.ts b/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.spec.ts new file mode 100644 index 00000000..1b311f0a --- /dev/null +++ b/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.spec.ts @@ -0,0 +1,58 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; + +import 'reflect-metadata'; + +import { pendingClassMetadataCountReflectKey } from '../../reflectMetadata/data/pendingClassMetadataCountReflectKey'; +import { isPendingClassMetadata } from './isPendingClassMetadata'; + +describe(isPendingClassMetadata.name, () => { + describe('having no metadata', () => { + describe('when called', () => { + let result: unknown; + + beforeAll(() => { + result = isPendingClassMetadata(class {}); + }); + + it('should return false', () => { + expect(result).toBe(false); + }); + }); + }); + + describe('having non zero number as metadata', () => { + describe('when called', () => { + class Foo {} + + let result: unknown; + + beforeAll(() => { + Reflect.defineMetadata(pendingClassMetadataCountReflectKey, 1, Foo); + + result = isPendingClassMetadata(Foo); + }); + + it('should return true', () => { + expect(result).toBe(true); + }); + }); + }); + + describe('having zero number as metadata', () => { + describe('when called', () => { + class Foo {} + + let result: unknown; + + beforeAll(() => { + Reflect.defineMetadata(pendingClassMetadataCountReflectKey, 0, Foo); + + result = isPendingClassMetadata(Foo); + }); + + it('should return false', () => { + expect(result).toBe(false); + }); + }); + }); +}); diff --git a/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.ts b/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.ts new file mode 100644 index 00000000..ff034e35 --- /dev/null +++ b/packages/container/libraries/core/src/metadata/calculations/isPendingClassMetadata.ts @@ -0,0 +1,15 @@ +import { Newable } from '@inversifyjs/common'; +import { getReflectMetadata } from '@inversifyjs/reflect-metadata-utils'; + +import { pendingClassMetadataCountReflectKey } from '../../reflectMetadata/data/pendingClassMetadataCountReflectKey'; + +export function isPendingClassMetadata(type: Newable): boolean { + const pendingClassMetadataCount: number | undefined = getReflectMetadata( + type, + pendingClassMetadataCountReflectKey, + ); + + return ( + pendingClassMetadataCount !== undefined && pendingClassMetadataCount !== 0 + ); +}