-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathModuleContainer.test.ts
More file actions
217 lines (175 loc) · 5.46 KB
/
ModuleContainer.test.ts
File metadata and controls
217 lines (175 loc) · 5.46 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import "reflect-metadata";
import { container as tsyringeContainer, inject, injectable } from "tsyringe";
import {
ConfigurableModule,
NoConfig,
} from "../../src/config/ConfigurableModule";
import {
ModuleContainer,
ModulesRecord,
} from "../../src/config/ModuleContainer";
import { TypedClass } from "../../src/types";
import { DependencyFactory, expectDefined } from "../../src";
import { injectAlias } from "../../src/config/injectAlias";
// module container will accept modules that extend this type
class BaseTestModule<Config> extends ConfigurableModule<Config> {}
type TestModulesRecord = ModulesRecord<TypedClass<BaseTestModule<unknown>>>;
interface TestModuleConfig {
testConfigProperty: number;
testConfigProperty2?: number;
testConfigProperty3?: number;
}
@injectable()
@injectAlias(["child-alias", "multi-alias"])
class ChildModule extends BaseTestModule<NoConfig> {
public constructor(@inject("TestModule") public readonly testModule: any) {
super();
}
x() {
return "dependency factory works";
}
}
@injectAlias(["base-alias", "multi-alias"])
class TestModule
extends BaseTestModule<TestModuleConfig>
implements DependencyFactory
{
public dependencies() {
return {
dependencyModule1: {
useClass: ChildModule,
},
};
}
}
class NoConfigModule extends BaseTestModule<NoConfig> {}
interface OtherTestModuleConfig {
otherTestConfigProperty: number;
}
class OtherTestModule extends BaseTestModule<OtherTestModuleConfig> {
public x() {
return "";
}
}
/**
* Showcases a wrongly typed/defined module as
* per the TestModuleContainer requirements
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class WrongTestModule {}
class TestModuleContainer<
Modules extends TestModulesRecord,
> extends ModuleContainer<Modules> {
public get dependencyContainer() {
return this.container;
}
}
describe("moduleContainer", () => {
let container: TestModuleContainer<{
TestModule: typeof TestModule;
OtherTestModule: typeof OtherTestModule;
NoConfigModule: typeof NoConfigModule;
}>;
const testConfigProperty = 0;
beforeEach(() => {
container = new TestModuleContainer({
TestModule,
OtherTestModule,
// this module would not be assignable to TestModuleContainer
// WrongTestModule,
NoConfigModule,
});
});
it("should resolve dependency factory dependencies correctly", () => {
container.configure({
TestModule: {
testConfigProperty,
},
OtherTestModule: {
otherTestConfigProperty: testConfigProperty,
},
});
container.create(() => tsyringeContainer.createChildContainer());
// Unfortunately we still need this so that the dependencies are registered
container.resolve("TestModule");
const dm = container.resolve("DependencyModule1");
expect(dm.x()).toBe("dependency factory works");
expect(dm.testModule).toBeDefined();
});
it("should throw on resolution, if config was not provided", () => {
expect.assertions(1);
container.create(() => tsyringeContainer.createChildContainer());
expect(() => {
container.resolve("TestModule");
}).toThrow();
});
it("should resolve the registered module with the provided config", () => {
expect.assertions(1);
container.configure({
TestModule: {
testConfigProperty,
},
OtherTestModule: {
otherTestConfigProperty: testConfigProperty,
},
});
container.create(() => tsyringeContainer.createChildContainer());
const testModule = container.resolve("TestModule");
expect(testModule.config.testConfigProperty).toBe(testConfigProperty);
const dependency = container.resolve("DependencyModule1");
dependency.x();
});
it("should stack configurations correctly", () => {
container.configure({
TestModule: {
testConfigProperty: 1,
},
OtherTestModule: {
otherTestConfigProperty: 4,
},
});
container.configurePartial({
TestModule: {
testConfigProperty2: 2,
},
});
container.configurePartial({
TestModule: {
testConfigProperty: 3,
},
});
container.create(() => tsyringeContainer.createChildContainer());
const { config } = container.resolve("TestModule");
expect(config.testConfigProperty).toBe(3);
expect(config.testConfigProperty2).toBe(2);
expect(config.testConfigProperty3).toBe(undefined);
});
it("should resolve dependencies correctly via alias", () => {
container.configure({
TestModule: {
testConfigProperty,
},
OtherTestModule: {
otherTestConfigProperty: testConfigProperty,
},
});
container.create(() => tsyringeContainer.createChildContainer());
// Unfortunately we still need this so that the dependencies are registered
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const m1 = container.resolve("base-alias" as any);
const m2 = container.resolve("TestModule");
expectDefined(m1);
// Check if its the same reference
expect(m1).toBe(m2);
const dm1 = container.resolve("child-alias" as any) as ChildModule;
const dm2 = container.resolve("DependencyModule1");
expect(dm1.x()).toBe("dependency factory works");
expect(dm1.testModule).toBeDefined();
expect(dm1).toBe(dm2);
const multi =
container.dependencyContainer.resolveAll<BaseTestModule<unknown>>(
"multi-alias"
);
expect(multi).toHaveLength(2);
});
});