-
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: Introduce variation point for targeted decoration of "instantiate"
- Loading branch information
Showing
3 changed files
with
138 additions
and
17 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
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
96 changes: 96 additions & 0 deletions
96
...injectable/src/dependency-injection-container/createContainer.targeted-decoration.test.js
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,96 @@ | ||
import getDi from '../test-utils/getDiForUnitTesting'; | ||
import getInjectable from '../getInjectable/getInjectable'; | ||
import { decorationInjectionToken } from './createContainer'; | ||
import getInjectionToken from '../getInjectionToken/getInjectionToken'; | ||
|
||
describe('createContainer.targeted-decoration', () => { | ||
it('given decorator targeting child, when parent is injected, decorates instance and instantiation parameter of only child', () => { | ||
const decoratorInjectable = getInjectable({ | ||
id: 'some-child-decorator', | ||
injectionToken: decorationInjectionToken, | ||
|
||
instantiate: () => ({ | ||
decorate: instantiateToBeDecorated => (di, instantiationParameter) => | ||
`decorated-instance(${instantiateToBeDecorated( | ||
di, | ||
`decorated-parameter(${instantiationParameter})`, | ||
)})`, | ||
|
||
target: childInjectable, | ||
}), | ||
}); | ||
|
||
const childInjectable = getInjectable({ | ||
id: 'some-child-injectable', | ||
|
||
instantiate: (di, instantiationParameter) => | ||
`child(${instantiationParameter})`, | ||
}); | ||
|
||
const parentInjectable = getInjectable({ | ||
id: 'some-parent-injectable', | ||
|
||
instantiate: (di, instantiationParameter) => { | ||
const childInstance = di.inject(childInjectable, 'child-parameter'); | ||
|
||
return `parent(${instantiationParameter}) -> ${childInstance}`; | ||
}, | ||
}); | ||
|
||
const di = getDi(parentInjectable, childInjectable, decoratorInjectable); | ||
|
||
const actual = di.inject(parentInjectable, 'parent-parameter'); | ||
|
||
expect(actual).toBe( | ||
'parent(parent-parameter) -> decorated-instance(child(decorated-parameter(child-parameter)))', | ||
); | ||
}); | ||
|
||
it('given decorator targeting an injection token and child implementing the token, when parent is injected, decorates instance and instantiation parameter of only the child', () => { | ||
const someInjectionTokenForTargetedDecoration = getInjectionToken({ | ||
id: 'some-injection-token-for-targeted-decoration', | ||
}); | ||
|
||
const decoratorInjectable = getInjectable({ | ||
id: 'some-injection-token-decorator', | ||
injectionToken: decorationInjectionToken, | ||
|
||
instantiate: () => ({ | ||
decorate: instantiateToBeDecorated => (di, instantiationParameter) => | ||
`decorated-instance(${instantiateToBeDecorated( | ||
di, | ||
`decorated-parameter(${instantiationParameter})`, | ||
)})`, | ||
|
||
target: someInjectionTokenForTargetedDecoration, | ||
}), | ||
}); | ||
|
||
const childInjectable = getInjectable({ | ||
id: 'some-child-injectable', | ||
|
||
instantiate: (di, instantiationParameter) => | ||
`child(${instantiationParameter})`, | ||
|
||
injectionToken: someInjectionTokenForTargetedDecoration, | ||
}); | ||
|
||
const parentInjectable = getInjectable({ | ||
id: 'some-parent-injectable', | ||
|
||
instantiate: (di, instantiationParameter) => { | ||
const childInstance = di.inject(childInjectable, 'child-parameter'); | ||
|
||
return `parent(${instantiationParameter}) -> ${childInstance}`; | ||
}, | ||
}); | ||
|
||
const di = getDi(parentInjectable, childInjectable, decoratorInjectable); | ||
|
||
const actual = di.inject(parentInjectable, 'parent-parameter'); | ||
|
||
expect(actual).toBe( | ||
'parent(parent-parameter) -> decorated-instance(child(decorated-parameter(child-parameter)))', | ||
); | ||
}); | ||
}); |