@@ -10,7 +10,8 @@ import {
10
10
ModulesRecord ,
11
11
} from "../../src/config/ModuleContainer" ;
12
12
import { TypedClass } from "../../src/types" ;
13
- import { DependencyFactory } from "../../src" ;
13
+ import { DependencyFactory , expectDefined } from "../../src" ;
14
+ import { injectAlias } from "../../src/config/injectAlias" ;
14
15
15
16
// module container will accept modules that extend this type
16
17
class BaseTestModule < Config > extends ConfigurableModule < Config > { }
@@ -24,6 +25,7 @@ interface TestModuleConfig {
24
25
}
25
26
26
27
@injectable ( )
28
+ @injectAlias ( [ "child-alias" , "multi-alias" ] )
27
29
class ChildModule extends BaseTestModule < NoConfig > {
28
30
public constructor ( @inject ( "TestModule" ) public readonly testModule : any ) {
29
31
super ( ) ;
@@ -34,6 +36,7 @@ class ChildModule extends BaseTestModule<NoConfig> {
34
36
}
35
37
}
36
38
39
+ @injectAlias ( [ "base-alias" , "multi-alias" ] )
37
40
class TestModule
38
41
extends BaseTestModule < TestModuleConfig >
39
42
implements DependencyFactory
@@ -66,7 +69,11 @@ class WrongTestModule {}
66
69
67
70
class TestModuleContainer <
68
71
Modules extends TestModulesRecord ,
69
- > extends ModuleContainer < Modules > { }
72
+ > extends ModuleContainer < Modules > {
73
+ public get dependencyContainer ( ) {
74
+ return this . container ;
75
+ }
76
+ }
70
77
71
78
describe ( "moduleContainer" , ( ) => {
72
79
let container : TestModuleContainer < {
@@ -169,4 +176,40 @@ describe("moduleContainer", () => {
169
176
expect ( config . testConfigProperty2 ) . toBe ( 2 ) ;
170
177
expect ( config . testConfigProperty3 ) . toBe ( undefined ) ;
171
178
} ) ;
179
+
180
+ it ( "should resolve dependencies correctly via alias" , ( ) => {
181
+ container . configure ( {
182
+ TestModule : {
183
+ testConfigProperty,
184
+ } ,
185
+
186
+ OtherTestModule : {
187
+ otherTestConfigProperty : testConfigProperty ,
188
+ } ,
189
+ } ) ;
190
+
191
+ container . create ( ( ) => tsyringeContainer . createChildContainer ( ) ) ;
192
+
193
+ // Unfortunately we still need this so that the dependencies are registered
194
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
195
+ const m1 = container . resolve ( "base-alias" as any ) ;
196
+ const m2 = container . resolve ( "TestModule" ) ;
197
+
198
+ expectDefined ( m1 ) ;
199
+ // Check if its the same reference
200
+ expect ( m1 ) . toBe ( m2 ) ;
201
+
202
+ const dm1 = container . resolve ( "child-alias" as any ) as ChildModule ;
203
+ const dm2 = container . resolve ( "DependencyModule1" ) ;
204
+
205
+ expect ( dm1 . x ( ) ) . toBe ( "dependency factory works" ) ;
206
+ expect ( dm1 . testModule ) . toBeDefined ( ) ;
207
+ expect ( dm1 ) . toBe ( dm2 ) ;
208
+
209
+ const multi =
210
+ container . dependencyContainer . resolveAll < BaseTestModule < unknown > > (
211
+ "multi-alias"
212
+ ) ;
213
+ expect ( multi ) . toHaveLength ( 2 ) ;
214
+ } ) ;
172
215
} ) ;
0 commit comments