-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
async-factory-invoke.spec.ts
56 lines (43 loc) · 1.48 KB
/
async-factory-invoke.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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");
});
});