1
|
|
|
import { flushPromises } from '@test/utils/testUtils'; |
2
|
|
|
import { InjectBean } from '@/core/decorators/InjectBean'; |
3
|
|
|
|
4
|
|
|
jest.mock('@/core', () => ({ |
5
|
|
|
logger: { |
6
|
|
|
info: jest.fn(), |
7
|
|
|
debug: jest.fn(), |
8
|
|
|
error: jest.fn(), |
9
|
|
|
}, |
10
|
|
|
})); |
11
|
|
|
|
12
|
|
|
describe('InjectBean.ts', () => { |
13
|
|
|
beforeEach(() => { |
14
|
|
|
jest.resetAllMocks(); |
15
|
|
|
}); |
16
|
|
|
|
17
|
|
|
it('injects a dependency', async () => { |
18
|
|
|
// GIVEN |
19
|
|
|
class TypeA {} |
20
|
|
|
const T: any = TypeA; |
21
|
|
|
T.instance = new TypeA(); |
22
|
|
|
|
23
|
|
|
// WHEN |
24
|
|
|
class Class { |
25
|
|
|
@InjectBean(TypeA) |
26
|
|
|
private dep!: TypeA; |
27
|
|
|
|
28
|
|
|
getDep() { |
29
|
|
|
return this.dep; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
const instance = new Class(); |
33
|
|
|
await flushPromises(); |
34
|
|
|
|
35
|
|
|
// THEN |
36
|
|
|
expect(instance.getDep()).toBe(T.instance); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
it('injects multiple dependencies', async () => { |
40
|
|
|
// GIVEN |
41
|
|
|
class TypeA {} |
42
|
|
|
const TA: any = TypeA; |
43
|
|
|
TA.instance = new TypeA(); |
44
|
|
|
|
45
|
|
|
class TypeB {} |
46
|
|
|
const TB: any = TypeB; |
47
|
|
|
TB.instance = new TypeB(); |
48
|
|
|
|
49
|
|
|
// WHEN |
50
|
|
|
class Class { |
51
|
|
|
@InjectBean(TypeA) |
52
|
|
|
dep1!: TypeA; |
53
|
|
|
|
54
|
|
|
@InjectBean(TypeB) |
55
|
|
|
dep2!: TypeB; |
56
|
|
|
|
57
|
|
|
getDeps() { |
58
|
|
|
return [this.dep1, this.dep2]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
const instance = new Class(); |
62
|
|
|
await flushPromises(); |
63
|
|
|
|
64
|
|
|
// THEN |
65
|
|
|
const [dep1, dep2] = instance.getDeps(); |
66
|
|
|
expect(dep1).toBe(TA.instance); |
67
|
|
|
expect(dep2).toBe(TB.instance); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
it('throws an error if trying to inject a non injectable dependency', async () => { |
71
|
|
|
// GIVEN |
72
|
|
|
class TypeA {} |
73
|
|
|
let instance; |
74
|
|
|
|
75
|
|
|
// WHEN |
76
|
|
|
expect(() => { |
77
|
|
|
class Class { |
78
|
|
|
@InjectBean(TypeA) |
79
|
|
|
dep!: TypeA; |
80
|
|
|
} |
81
|
|
|
instance = new Class(); |
82
|
|
|
}).toThrow(new Error('Cannot get instance from TypeA. Make sure that TypeA has @Bean as class decorator')); |
83
|
|
|
expect(instance).toBe(undefined); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
it('throws an error if trying to inject without specify a type', async () => { |
87
|
|
|
// GIVEN |
88
|
|
|
class TypeA {} |
89
|
|
|
let instance; |
90
|
|
|
|
91
|
|
|
// WHEN |
92
|
|
|
expect(() => { |
93
|
|
|
class Class { |
94
|
|
|
@InjectBean(null) |
95
|
|
|
dep!: TypeA; |
96
|
|
|
} |
97
|
|
|
instance = new Class(); |
98
|
|
|
}).toThrow(new Error('Please specify the type of Bean. Example: @InjectBean(BeanClass)')); |
99
|
|
|
expect(instance).toBe(undefined); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
it('throws an error when dependency is not found', async () => { |
103
|
|
|
// GIVEN |
104
|
|
|
class TypeA {} |
105
|
|
|
class Class { |
106
|
|
|
@InjectBean(TypeA) |
107
|
|
|
dep!: TypeA; |
108
|
|
|
|
109
|
|
|
getDep() { |
110
|
|
|
return this.dep; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// WHEN |
115
|
|
|
let dep; |
116
|
|
|
expect(() => { |
117
|
|
|
const instance = new Class(); |
118
|
|
|
dep = instance.getDep(); |
119
|
|
|
}).toThrow(new Error('Cannot get instance from TypeA. Make sure that TypeA has @Bean as class decorator')); |
120
|
|
|
|
121
|
|
|
// THEN |
122
|
|
|
expect(dep).toBe(undefined); |
123
|
|
|
}); |
124
|
|
|
}); |
125
|
|
|
|