Skip to content

Commit

Permalink
test: add more test to use async provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Mar 27, 2024
1 parent 3d2b085 commit 46a3fec
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/di/src/common/decorators/inject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,45 @@ describe("@Inject()", () => {
expect(instance).toBeInstanceOf(Test);
expect(instance.test).toBeInstanceOf(InjectorService);
});
it("should inject service w/ async factory", async () => {
// GIVEN
class Test {
constructor(public type: string) {}
}

const TokenAsync = Symbol.for("MyService");

registerProvider<Test>({
provide: TokenAsync,
type: "test:async",
deps: [],
useAsyncFactory() {
return Promise.resolve(new Test("async"));
}
});

@Injectable()
class Parent1 {
@Inject(TokenAsync)
test: Test;
}

@Injectable()
class Parent2 {
@Inject(TokenAsync)
test: Test;
}

const injector = new InjectorService();

await injector.load();

const parent1 = await injector.invoke<Parent1>(Parent1);
const parent2 = await injector.invoke<Parent2>(Parent2);

expect(parent1.test).toBeInstanceOf(Test);
expect(parent2.test).toBeInstanceOf(Test);
});
it("should inject service with the given type", async () => {
// GIVEN
@Injectable()
Expand Down

0 comments on commit 46a3fec

Please sign in to comment.