-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): update ClassMetadata with lifecycle
- Loading branch information
1 parent
62ffcc1
commit d7494d9
Showing
11 changed files
with
887 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
276 changes: 276 additions & 0 deletions
276
...ibraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
import { beforeAll, describe, expect, it } from '@jest/globals'; | ||
|
||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { | ||
INJECT_TAG, | ||
MULTI_INJECT_TAG, | ||
NAME_TAG, | ||
NAMED_TAG, | ||
OPTIONAL_TAG, | ||
UNMANAGED_TAG, | ||
} from '../../reflectMetadata/data/keys'; | ||
import { ClassElementMetadata } from '../models/ClassElementMetadata'; | ||
import { ClassElementMetadataKind } from '../models/ClassElementMetadataKind'; | ||
import { LegacyMetadata } from '../models/LegacyMetadata'; | ||
import { MetadataName } from '../models/MetadataName'; | ||
import { MetadataTag } from '../models/MetadataTag'; | ||
import { MetadataTargetName } from '../models/MetadataTargetName'; | ||
import { getClassElementMetadataFromLegacyMetadata } from './getClassElementMetadataFromLegacyMetadata'; | ||
|
||
describe(getClassElementMetadataFromLegacyMetadata.name, () => { | ||
describe('having an empty metadata list', () => { | ||
let metadataListFixture: LegacyMetadata[]; | ||
|
||
beforeAll(() => { | ||
metadataListFixture = []; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
try { | ||
getClassElementMetadataFromLegacyMetadata(metadataListFixture); | ||
} catch (error: unknown) { | ||
result = error; | ||
} | ||
}); | ||
|
||
it('should throw an Error', () => { | ||
const expectedErrorProperties: Partial<Error> = { | ||
message: 'Expected @inject, @multiInject or @unmanaged metadata', | ||
}; | ||
|
||
expect(result).toBeInstanceOf(Error); | ||
expect(result).toStrictEqual( | ||
expect.objectContaining(expectedErrorProperties), | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having a metadata list with unmanaged metadata', () => { | ||
let metadataListFixture: LegacyMetadata[]; | ||
|
||
beforeAll(() => { | ||
metadataListFixture = [ | ||
{ | ||
key: UNMANAGED_TAG, | ||
value: true, | ||
}, | ||
]; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = getClassElementMetadataFromLegacyMetadata(metadataListFixture); | ||
}); | ||
|
||
it('should return ClassElementMetadata', () => { | ||
const expectedClassElementMetadata: ClassElementMetadata = { | ||
kind: ClassElementMetadataKind.unmanaged, | ||
}; | ||
|
||
expect(result).toStrictEqual(expectedClassElementMetadata); | ||
}); | ||
}); | ||
}); | ||
|
||
describe.each<[string, LegacyMetadata]>([ | ||
[ | ||
'inject', | ||
{ | ||
key: INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
[ | ||
'multi inject', | ||
{ | ||
key: MULTI_INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
])( | ||
'having a metadata list with both unmanaged and %s metadata', | ||
(_: string, metadata: LegacyMetadata) => { | ||
let metadataListFixture: LegacyMetadata[]; | ||
|
||
beforeAll(() => { | ||
metadataListFixture = [ | ||
{ | ||
key: UNMANAGED_TAG, | ||
value: true, | ||
}, | ||
metadata, | ||
]; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
try { | ||
getClassElementMetadataFromLegacyMetadata(metadataListFixture); | ||
} catch (error: unknown) { | ||
result = error; | ||
} | ||
}); | ||
|
||
it('should throw an Error', () => { | ||
const expectedErrorProperties: Partial<Error> = { | ||
message: | ||
'Expected a single @inject, @multiInject or @unmanaged metadata', | ||
}; | ||
|
||
expect(result).toBeInstanceOf(Error); | ||
expect(result).toStrictEqual( | ||
expect.objectContaining(expectedErrorProperties), | ||
); | ||
}); | ||
}); | ||
}, | ||
); | ||
|
||
describe.each<[string, ClassElementMetadataKind, LegacyMetadata]>([ | ||
[ | ||
'inject', | ||
ClassElementMetadataKind.singleInjection, | ||
{ | ||
key: INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
[ | ||
'multi inject', | ||
ClassElementMetadataKind.multipleInjection, | ||
{ | ||
key: MULTI_INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
])( | ||
'having a metadata list with % metadata', | ||
( | ||
_: string, | ||
classElementMetadataKind: ClassElementMetadataKind, | ||
metadata: LegacyMetadata, | ||
) => { | ||
let metadataListFixture: LegacyMetadata[]; | ||
|
||
beforeAll(() => { | ||
metadataListFixture = [metadata]; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = | ||
getClassElementMetadataFromLegacyMetadata(metadataListFixture); | ||
}); | ||
|
||
it('should return ClassElementMetadata', () => { | ||
const expectedClassElementMetadata: ClassElementMetadata = { | ||
kind: classElementMetadataKind, | ||
name: undefined, | ||
optional: false, | ||
tags: new Map(), | ||
targetName: undefined, | ||
value: metadata.value as ServiceIdentifier, | ||
}; | ||
|
||
expect(result).toStrictEqual(expectedClassElementMetadata); | ||
}); | ||
}); | ||
}, | ||
); | ||
|
||
describe.each<[string, ClassElementMetadataKind, LegacyMetadata]>([ | ||
[ | ||
'inject', | ||
ClassElementMetadataKind.singleInjection, | ||
{ | ||
key: INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
[ | ||
'multi inject', | ||
ClassElementMetadataKind.multipleInjection, | ||
{ | ||
key: MULTI_INJECT_TAG, | ||
value: Symbol(), | ||
}, | ||
], | ||
])( | ||
'having a metadata list with % metadata', | ||
( | ||
_: string, | ||
classElementMetadataKind: ClassElementMetadataKind, | ||
metadata: LegacyMetadata, | ||
) => { | ||
let customTagMetadataFixture: LegacyMetadata; | ||
let nameMetadataFixture: LegacyMetadata; | ||
let optionalMetadataFixture: LegacyMetadata; | ||
let targetNameMetadataFixture: LegacyMetadata; | ||
let metadataListFixture: LegacyMetadata[]; | ||
|
||
beforeAll(() => { | ||
customTagMetadataFixture = { | ||
key: 'customTag', | ||
value: 'customTagValue', | ||
}; | ||
nameMetadataFixture = { | ||
key: NAME_TAG, | ||
value: 'name-fixture', | ||
}; | ||
optionalMetadataFixture = { | ||
key: OPTIONAL_TAG, | ||
value: true, | ||
}; | ||
targetNameMetadataFixture = { | ||
key: NAMED_TAG, | ||
value: 'target-name-fixture', | ||
}; | ||
metadataListFixture = [ | ||
metadata, | ||
customTagMetadataFixture, | ||
nameMetadataFixture, | ||
optionalMetadataFixture, | ||
targetNameMetadataFixture, | ||
]; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = | ||
getClassElementMetadataFromLegacyMetadata(metadataListFixture); | ||
}); | ||
|
||
it('should return ClassElementMetadata', () => { | ||
const expectedClassElementMetadata: ClassElementMetadata = { | ||
kind: classElementMetadataKind, | ||
name: nameMetadataFixture.value as MetadataName, | ||
optional: true, | ||
tags: new Map<MetadataTag, unknown>([ | ||
[ | ||
customTagMetadataFixture.key as MetadataTag, | ||
customTagMetadataFixture.value, | ||
], | ||
]), | ||
targetName: targetNameMetadataFixture.value as MetadataTargetName, | ||
value: metadata.value as ServiceIdentifier, | ||
}; | ||
|
||
expect(result).toStrictEqual(expectedClassElementMetadata); | ||
}); | ||
}); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ainer/libraries/core/src/metadata/calculations/getClassElementMetadataFromNewable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { beforeAll, describe, expect, it } from '@jest/globals'; | ||
|
||
import { Newable } from '@inversifyjs/common'; | ||
|
||
import { ClassElementMetadata } from '../models/ClassElementMetadata'; | ||
import { ClassElementMetadataKind } from '../models/ClassElementMetadataKind'; | ||
import { getClassElementMetadataFromNewable } from './getClassElementMetadataFromNewable'; | ||
|
||
describe(getClassElementMetadataFromNewable.name, () => { | ||
describe('when called', () => { | ||
let typeFixture: Newable; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
typeFixture = class {}; | ||
|
||
result = getClassElementMetadataFromNewable(typeFixture); | ||
}); | ||
|
||
it('should return ClassElementMetadata', () => { | ||
const expected: ClassElementMetadata = { | ||
kind: ClassElementMetadataKind.singleInjection, | ||
name: undefined, | ||
optional: false, | ||
tags: new Map(), | ||
targetName: undefined, | ||
value: typeFixture, | ||
}; | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.