-
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 early-override to permit override of injectable befor…
…e registered
- Loading branch information
Showing
6 changed files
with
183 additions
and
40 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
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
38 changes: 38 additions & 0 deletions
38
packages/injectable/core/src/dependency-injection-container/early-override.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,38 @@ | ||
import isInjectionToken from '../getInjectionToken/isInjectionToken'; | ||
|
||
export const earlyOverrideFor = | ||
({ getRelatedInjectables, alreadyInjected, overridingInjectables }) => | ||
(alias, instantiateStub) => { | ||
const relatedInjectables = getRelatedInjectables(alias); | ||
|
||
if (relatedInjectables.length > 1) { | ||
throw new Error( | ||
`Tried to override single implementation of injection token "${ | ||
alias.id | ||
}", but found multiple registered implementations: "${relatedInjectables | ||
.map(x => x.id) | ||
.join('", "')}".`, | ||
); | ||
} | ||
|
||
if (relatedInjectables.length === 0 && isInjectionToken(alias)) { | ||
throw new Error( | ||
`Tried to early-override an injection token "${alias.id}", but that is currently not supported.`, | ||
); | ||
} | ||
|
||
if (alreadyInjected.has(alias)) { | ||
throw new Error( | ||
`Tried to override injectable "${alias.id}", but it was already injected.`, | ||
); | ||
} | ||
|
||
const originalInjectable = relatedInjectables[0] || alias; | ||
|
||
overridingInjectables.set(originalInjectable, { | ||
...originalInjectable, | ||
overriddenInjectable: originalInjectable, | ||
causesSideEffects: false, | ||
instantiate: instantiateStub, | ||
}); | ||
}; |
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
107 changes: 107 additions & 0 deletions
107
packages/injectable/core/src/scenarios/early-override.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,107 @@ | ||
import getInjectable from '../getInjectable/getInjectable'; | ||
import createContainer from '../dependency-injection-container/createContainer'; | ||
import getInjectionToken from '../getInjectionToken/getInjectionToken'; | ||
|
||
describe('early-override', () => { | ||
describe('given a still non-registered injectable', () => { | ||
let di; | ||
let someInjectable; | ||
let someInjectionToken; | ||
|
||
beforeEach(() => { | ||
di = createContainer('irrelevant'); | ||
|
||
someInjectionToken = getInjectionToken({ id: 'some-token-id' }); | ||
}); | ||
|
||
it('when early-overriding an injection token, throws', () => { | ||
expect(() => { | ||
di.earlyOverride(someInjectionToken, () => 'irrevant'); | ||
}).toThrow( | ||
'Tried to early-override an injection token "some-token-id", but that is currently not supported.', | ||
); | ||
}); | ||
|
||
describe('when early-overriding it using injectable', () => { | ||
beforeEach(() => { | ||
someInjectable = getInjectable({ | ||
id: 'some-injectable', | ||
instantiate: () => 'some-instance', | ||
injectionToken: someInjectionToken, | ||
}); | ||
|
||
di.earlyOverride(someInjectable, () => 'some-overridden-instance'); | ||
}); | ||
|
||
describe('given the injectable is still non-registered', () => { | ||
it('when injected, throws as normal', () => { | ||
expect(() => { | ||
di.inject(someInjectable); | ||
}).toThrow( | ||
'Tried to inject non-registered injectable "irrelevant" -> "some-injectable".', | ||
); | ||
}); | ||
|
||
it('when injected using token, throws as normal', () => { | ||
expect(() => { | ||
di.inject(someInjectionToken); | ||
}).toThrow( | ||
'Tried to inject non-registered injectable "irrelevant" -> "some-token-id".', | ||
); | ||
}); | ||
}); | ||
|
||
describe('given the injectable is registered', () => { | ||
beforeEach(() => { | ||
di.register(someInjectable); | ||
}); | ||
|
||
it('when injected, returns the overridden instance', () => { | ||
const actual = di.inject(someInjectable); | ||
|
||
expect(actual).toBe('some-overridden-instance'); | ||
}); | ||
|
||
it('when injected using token, returns the overridden instance', () => { | ||
const actual = di.inject(someInjectionToken); | ||
|
||
expect(actual).toBe('some-overridden-instance'); | ||
}); | ||
|
||
it('when injecting many using token, returns the overridden instance', () => { | ||
const actual = di.injectMany(someInjectionToken); | ||
|
||
expect(actual).toEqual(['some-overridden-instance']); | ||
}); | ||
|
||
it('given injected, when the early-overridden again, throws', () => { | ||
di.inject(someInjectionToken); | ||
|
||
expect(() => { | ||
di.earlyOverride(someInjectable); | ||
}).toThrow( | ||
'Tried to override injectable "some-injectable", but it was already injected.', | ||
); | ||
}); | ||
|
||
describe('given unoverridden', () => { | ||
beforeEach(() => { | ||
di.unoverride(someInjectable); | ||
}); | ||
|
||
it('when injected, returns the original instance', () => { | ||
const actual = di.inject(someInjectable); | ||
|
||
expect(actual).toBe('some-instance'); | ||
}); | ||
|
||
it('when injected using token, returns the original instance', () => { | ||
const actual = di.inject(someInjectionToken); | ||
|
||
expect(actual).toBe('some-instance'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |