Skip to content

Commit

Permalink
feat(assert-injector): add assertInjector
Browse files Browse the repository at this point in the history
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
nartc committed Sep 11, 2023
1 parent 9ad3522 commit fc83606
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions libs/ngxtension/assert-injector/README.md
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`.
5 changes: 5 additions & 0 deletions libs/ngxtension/assert-injector/ng-package.json
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 libs/ngxtension/assert-injector/src/assert-injector.spec.ts
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);
});
});
9 changes: 9 additions & 0 deletions libs/ngxtension/assert-injector/src/assert-injector.ts
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);
}
1 change: 1 addition & 0 deletions libs/ngxtension/assert-injector/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const greeting = 'Hello World!';
4 changes: 3 additions & 1 deletion libs/ngxtension/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"libs/ngxtension/resize/**/*.ts",
"libs/ngxtension/resize/**/*.html",
"libs/ngxtension/create-injection-token/**/*.ts",
"libs/ngxtension/create-injection-token/**/*.html"
"libs/ngxtension/create-injection-token/**/*.html",
"libs/ngxtension/assert-injector/**/*.ts",
"libs/ngxtension/assert-injector/**/*.html"
]
}
},
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"baseUrl": ".",
"paths": {
"ngxtension": ["libs/ngxtension/src/index.ts"],
"ngxtension/assert-injector": [
"libs/ngxtension/assert-injector/src/index.ts"
],
"ngxtension/create-injection-token": [
"libs/ngxtension/create-injection-token/src/index.ts"
],
Expand Down

0 comments on commit fc83606

Please sign in to comment.