-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjectorHierarchy.test.ts
More file actions
79 lines (64 loc) · 2.22 KB
/
injectorHierarchy.test.ts
File metadata and controls
79 lines (64 loc) · 2.22 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { beforeEach, describe, expect, test } from '@jest/globals';
import { Inject, Injectable, Injector, InjectorError } from "../src";
describe("injector hierarchy", () => {
let testInjector : Injector;
beforeEach(() => {
testInjector = new Injector("test");
});
test("child inherits parent", () => {
testInjector = new Injector("test", Injector.root); // inherits it
const TOKEN1 = Injectable(Symbol("TOKEN1"), {
value: "aval",
injector: Injector.root
});
testInjector.runInContext(() => {
class WithDependencies {
constructor(
@Inject.Param(TOKEN1) token1: string
) {
expect(token1).toBeDefined();
expect(token1).toBe("aval");
}
}
expect(testInjector.createInstance(WithDependencies)).toBeDefined();
});
});
test("child doesn't inherit parent", () => {
testInjector.runInContext(() => {
const TOKEN1 = Injectable(Symbol("TOKEN1"), {
value: "aval",
injector: Injector.root
});
const TOKEN2 = Injectable(Symbol("TOKEN1"), {
value: "aval",
}); // will use injector from context - testInjector
class WithDependencies {
constructor(
@Inject.Param(TOKEN1) token1: string,
@Inject.Param(TOKEN2) token2: string
) {
expect(token1).toBeDefined();
expect(token1).toBe("aval");
expect(token2).toBeDefined();
expect(token2).toBe("aval");
}
}
expect(() => testInjector.createInstance(WithDependencies)).toThrow(InjectorError);
});
});
test("overwrite token value in child", () => {
testInjector = new Injector("test", Injector.root); // inherits it
const TOKEN = Symbol("TOKEN");
Injectable(TOKEN, {
value: "rootVal",
injector: Injector.root
});
Injectable(TOKEN, {
value: "childVal",
injector: testInjector
});
expect(Inject(TOKEN, Injector.root)).toBe("rootVal");
expect(Inject(TOKEN, testInjector)).toBe("childVal");
expect(testInjector.runInContext(() => Inject(TOKEN))).toBe("childVal");
});
});