-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathannotations.spec.js
186 lines (129 loc) · 4.42 KB
/
annotations.spec.js
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
import {
hasAnnotation,
readAnnotations,
Inject,
InjectLazy,
InjectPromise,
Provide,
ProvidePromise
} from '../src/annotations';
describe('hasAnnotation', function() {
it('should return false if fn not annotated', function() {
function foo() {}
class Bar {}
class SomeAnnotation {}
expect(hasAnnotation(foo, SomeAnnotation)).toBe(false);
expect(hasAnnotation(Bar, SomeAnnotation)).toBe(false);
});
it('should return true if the fn has an instance of given annotation', function() {
class SomeAnnotation {}
@SomeAnnotation
function foo() {}
expect(hasAnnotation(foo, SomeAnnotation)).toBe(true);
});
it('should return false if fn does not have given annotation', function() {
class YepAnnotation {}
class NopeAnnotation {}
@YepAnnotation
function foo() {}
expect(hasAnnotation(foo, NopeAnnotation)).toBe(false);
});
});
describe('readAnnotations', function() {
it('should read @Provide', function() {
class Bar {}
@Provide(Bar)
class Foo {}
var annotations = readAnnotations(Foo);
expect(annotations.provide.token).toBe(Bar);
expect(annotations.provide.isPromise).toBe(false);
});
it('should read @ProvidePromise', function() {
class Bar {}
@ProvidePromise(Bar)
class Foo {}
var annotations = readAnnotations(Foo);
expect(annotations.provide.token).toBe(Bar);
expect(annotations.provide.isPromise).toBe(true);
});
it('should read @Inject', function() {
class One {}
class Two {}
@Inject(One, Two)
class Foo {}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(false);
expect(annotations.params[0].isLazy).toBe(false);
expect(annotations.params[1].token).toBe(Two);
expect(annotations.params[1].isPromise).toBe(false);
expect(annotations.params[1].isLazy).toBe(false);
});
it('should read @Inject on a parameter', function() {
class One {}
class Two {}
class Foo {
constructor(@Inject(One) one, @Inject(Two) two) {}
}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(false);
expect(annotations.params[0].isLazy).toBe(false);
expect(annotations.params[1].token).toBe(Two);
expect(annotations.params[1].isPromise).toBe(false);
expect(annotations.params[1].isLazy).toBe(false);
});
it('should read @InjectLazy on a parameter', function() {
class One {}
class Foo {
constructor(@InjectLazy(One) one) {}
}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(false);
expect(annotations.params[0].isLazy).toBe(true);
});
it('should read @InjectPromise on a parameter', function() {
class One {}
class Foo {
constructor(@InjectPromise(One) one) {}
}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(true);
expect(annotations.params[0].isLazy).toBe(false);
});
it('should read type annotations', function() {
class One {}
class Two {}
class Foo {
constructor(one: One, two: Two) {}
}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(false);
expect(annotations.params[0].isLazy).toBe(false);
expect(annotations.params[1].token).toBe(Two);
expect(annotations.params[1].isPromise).toBe(false);
expect(annotations.params[1].isLazy).toBe(false);
});
it('should read stacked @Inject{Lazy, Promise} annotations', function() {
class One {}
class Two {}
class Three {}
@Inject(One)
@InjectLazy(Two)
@InjectPromise(Three)
class Foo {}
var annotations = readAnnotations(Foo);
expect(annotations.params[0].token).toBe(One);
expect(annotations.params[0].isPromise).toBe(false);
expect(annotations.params[0].isLazy).toBe(false);
expect(annotations.params[1].token).toBe(Two);
expect(annotations.params[1].isPromise).toBe(false);
expect(annotations.params[1].isLazy).toBe(true);
expect(annotations.params[2].token).toBe(Three);
expect(annotations.params[2].isPromise).toBe(true);
expect(annotations.params[2].isLazy).toBe(false);
});
});