diff --git a/packages/injectable/core/src/dependency-injection-container/createContainer.js b/packages/injectable/core/src/dependency-injection-container/createContainer.js index 0e8cd1cd..fce6d0a6 100644 --- a/packages/injectable/core/src/dependency-injection-container/createContainer.js +++ b/packages/injectable/core/src/dependency-injection-container/createContainer.js @@ -49,6 +49,7 @@ export default (containerId, { detectCycles = true } = {}) => { getRelatedInjectables, getInject: () => decoratedPrivateInject, setDependee, + getNamespacedId, }); const nonDecoratedPrivateInjectMany = diff --git a/packages/injectable/core/src/dependency-injection-container/privateInjectManyFor.js b/packages/injectable/core/src/dependency-injection-container/privateInjectManyFor.js index d2769d5e..67b0c19e 100644 --- a/packages/injectable/core/src/dependency-injection-container/privateInjectManyFor.js +++ b/packages/injectable/core/src/dependency-injection-container/privateInjectManyFor.js @@ -6,6 +6,7 @@ export const privateInjectManyFor = getRelatedInjectables, getInject, setDependee, + getNamespacedId, }) => ({ withMeta }) => ( @@ -34,16 +35,18 @@ export const privateInjectManyFor = return instance; } + const namespacedId = getNamespacedId(injectable); + if (!isPromise(instance)) { return { instance, - meta: { id: injectable.id }, + meta: { id: namespacedId }, }; } return instance.then(awaitedInstance => ({ instance: awaitedInstance, - meta: { id: injectable.id }, + meta: { id: namespacedId }, })); }); diff --git a/packages/injectable/core/src/scenarios/injection-with-meta-data.test.js b/packages/injectable/core/src/scenarios/injection-with-meta-data.test.js index 853f4381..d00478df 100644 --- a/packages/injectable/core/src/scenarios/injection-with-meta-data.test.js +++ b/packages/injectable/core/src/scenarios/injection-with-meta-data.test.js @@ -29,6 +29,84 @@ describe('injection with meta data', () => { ]); }); + it('given scope, when injecting many sync injectable with meta data, does so', () => { + const someInjectionToken = getInjectionToken({ + id: 'some-injection-token-id', + }); + + const registerInScopeInjectable = getInjectable({ + id: 'some-scope', + + instantiate: di => injectable => { + di.register(injectable); + }, + + scope: true, + }); + + const someInjectable = getInjectable({ + id: 'some-injectable', + instantiate: () => 'some-instance', + injectionToken: someInjectionToken, + }); + + const di = createContainer('irrelevant'); + + di.register(registerInScopeInjectable); + + const registerInScope = di.inject(registerInScopeInjectable); + + registerInScope(someInjectable); + + const actual = di.injectManyWithMeta(someInjectionToken); + + expect(actual).toEqual([ + { + instance: 'some-instance', + meta: { id: 'some-scope:some-injectable' }, + }, + ]); + }); + + it('given scope, when injecting many async injectable with meta data, does so', async () => { + const someInjectionToken = getInjectionToken({ + id: 'some-injection-token-id', + }); + + const registerInScopeInjectable = getInjectable({ + id: 'some-scope', + + instantiate: di => injectable => { + di.register(injectable); + }, + + scope: true, + }); + + const someInjectable = getInjectable({ + id: 'some-injectable', + instantiate: async () => 'some-instance', + injectionToken: someInjectionToken, + }); + + const di = createContainer('irrelevant'); + + di.register(registerInScopeInjectable); + + const registerInScope = di.inject(registerInScopeInjectable); + + registerInScope(someInjectable); + + const actual = await di.injectManyWithMeta(someInjectionToken); + + expect(actual).toEqual([ + { + instance: 'some-instance', + meta: { id: 'some-scope:some-injectable' }, + }, + ]); + }); + it('given injecting many async injectable with meta data, does so', async () => { const someInjectionToken = getInjectionToken({ id: 'some-injection-token-id',