Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(di): fix asynchronous factory injection when PlatformTest.invoke …
…is used
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 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
56 changes: 56 additions & 0 deletions
56
packages/di/src/node/integration/async-factory-invoke.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,56 @@ | ||
import {Inject, Injectable, registerProvider, DITest} from "../../index"; | ||
|
||
const PrimaryPostgresDb = Symbol.for("PrimaryPostgresDb"); | ||
type PrimaryPostgresDb = {id: string}; | ||
|
||
registerProvider({ | ||
provide: PrimaryPostgresDb, | ||
useAsyncFactory: () => { | ||
return Promise.resolve({ | ||
id: "id" | ||
}); | ||
} | ||
}); | ||
|
||
@Injectable() | ||
export class MyService { | ||
@Inject(PrimaryPostgresDb) | ||
protected dataSource: PrimaryPostgresDb; | ||
|
||
me() { | ||
return this.dataSource.id; | ||
} | ||
} | ||
|
||
describe("AsyncFactory", () => { | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
|
||
it("should inject async factory and return resolve promise value (first test)", () => { | ||
const service = DITest.get<MyService>(MyService); | ||
|
||
const result = service.me(); | ||
expect(result).toEqual("id"); | ||
}); | ||
|
||
it("should inject async factory and return resolve promise value (second test)", () => { | ||
const service = DITest.get<MyService>(MyService); | ||
|
||
const result = service.me(); | ||
expect(result).toEqual("id"); | ||
}); | ||
|
||
it("should inject async factory and return resolve promise value (first test - invoke)", async () => { | ||
const service = await DITest.invoke<MyService>(MyService, []); | ||
|
||
const result = service.me(); | ||
expect(result).toEqual("id"); | ||
}); | ||
|
||
it("should inject async factory and return resolve promise value (second test - invoke)", async () => { | ||
const service = await DITest.invoke<MyService>(MyService, []); | ||
|
||
const result = service.me(); | ||
expect(result).toEqual("id"); | ||
}); | ||
}); |
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,25 @@ | ||
import {Inject, Injectable, registerProvider, DITest} from "../../index"; | ||
|
||
@Injectable() | ||
export class MyService { | ||
id = "id"; | ||
$onInit() { | ||
return new Promise((resolve) => | ||
setTimeout(() => { | ||
this.id = "id2"; | ||
resolve({}); | ||
}, 1) | ||
); | ||
} | ||
} | ||
|
||
describe("OnInit Invoke", () => { | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
|
||
it("should inject async factory and return resolve promise value", async () => { | ||
const service = await DITest.invoke<MyService>(MyService, []); | ||
|
||
expect(service.id).toEqual("id2"); | ||
}); | ||
}); |
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