From 46a3fec4ced9c49203eaa32210152364da331083 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 27 Mar 2024 10:33:14 +0100 Subject: [PATCH] test: add more test to use async provider --- .../di/src/common/decorators/inject.spec.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/di/src/common/decorators/inject.spec.ts b/packages/di/src/common/decorators/inject.spec.ts index 1a201e8637c..08e2b5892ef 100644 --- a/packages/di/src/common/decorators/inject.spec.ts +++ b/packages/di/src/common/decorators/inject.spec.ts @@ -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({ + 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); + const parent2 = await injector.invoke(Parent2); + + expect(parent1.test).toBeInstanceOf(Test); + expect(parent2.test).toBeInstanceOf(Test); + }); it("should inject service with the given type", async () => { // GIVEN @Injectable()