-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assert-injector): add
assertInjector
closes #15 This PR adds a utility function `assertInjector` that abstracts `assertInInjectionContext` and guarantees an `Injector` after the utility is invoked ```ts function injectDummy(injector?: Injector) { injector = assertInjector(injectDummy, injector); // ^ injector is guaranteed to be an Injector runInInjectionContext(injector, () => { // always run in a particular Injector's context }) } ```
- Loading branch information
Showing
7 changed files
with
63 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ngxtension/assert-injector | ||
|
||
Secondary entry point of `ngxtension`. It can be used by importing from `ngxtension/assert-injector`. |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
libs/ngxtension/assert-injector/src/assert-injector.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,39 @@ | ||
import { Injector, runInInjectionContext } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { createInjectionToken } from 'libs/ngxtension/create-injection-token/src/create-injection-token'; | ||
import { assertInjector } from './assert-injector'; | ||
|
||
describe(assertInjector.name, () => { | ||
const [injectNumber, provideNumber] = createInjectionToken(() => 1); | ||
|
||
function injectDummy(injector?: Injector) { | ||
injector = assertInjector(injectDummy, injector); | ||
return runInInjectionContext(injector, () => injectNumber()); | ||
} | ||
|
||
it('given no custom injector, when run in injection context, then return value', () => { | ||
TestBed.runInInjectionContext(() => { | ||
const value = injectDummy(); | ||
expect(value).toEqual(1); | ||
}); | ||
}); | ||
|
||
it('given no custom injector, when run outside injection context, then throw', () => { | ||
expect(() => injectDummy()).toThrowError( | ||
/injectDummy\(\) can only be used within an injection context/i | ||
); | ||
}); | ||
|
||
it('given a custom injector, when run in that injector context without providing number, then throw', () => { | ||
expect(() => injectDummy(Injector.create({ providers: [] }))).toThrowError( | ||
/No provider for InjectionToken/i | ||
); | ||
}); | ||
|
||
it('given a custom injector, when run in that injector context and providing number, then return value', () => { | ||
const value = injectDummy( | ||
Injector.create({ providers: [provideNumber(2)] }) | ||
); | ||
expect(value).toEqual(2); | ||
}); | ||
}); |
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,9 @@ | ||
import { Injector, assertInInjectionContext, inject } from '@angular/core'; | ||
|
||
export function assertInjector( | ||
fn: Function, | ||
injector: Injector | undefined | null | ||
): Injector { | ||
!injector && assertInInjectionContext(fn); | ||
return injector ?? inject(Injector); | ||
} |
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 @@ | ||
export const greeting = 'Hello World!'; |
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