-
Notifications
You must be signed in to change notification settings - Fork 1
/
objarSpec.js
182 lines (146 loc) · 4.89 KB
/
objarSpec.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
var Objar = require('./objar');
describe('Objar', function () {
var objar;
beforeEach(function () {
objar = new Objar();
});
it('should add a service definition', function () {
expect(objar.isDefined('foo')).toBeFalsy();
objar.define('foo', function () {
return 'bar'
});
expect(objar.isDefined('foo')).toBeTruthy();
});
it('should throw an error when adding an invalid service definition', function () {
expect(function () {
objar.define('foo', 'bar')
}).toThrow(new Error('The service "foo" definition must be a function'));
});
it('should throw an error when resolving a non existing service', function () {
expect(function () {
objar.resolve('foo')
}).toThrow(new Error('The service "foo" is not defined'));
});
it('should throw an error when the service definition does not return it', function () {
objar.define('foo', function () {});
expect(function () {
objar.resolve('foo')
}).toThrow(new Error('The definition of the service "foo" must return it'));
});
it('should return the same instance of a service', function () {
objar.define('foo', function () {
return {}
});
var one = objar.resolve('foo');
var second = objar.resolve('foo');
expect(one).toBe(second);
});
it('should resolve a service', function () {
// A
// / \
// v v
// C B
// \ /
// v
// D -> E
var calls = [];
objar.define('E', function () {
calls.push('E');
return 'E';
});
objar.define('A', function () {
calls.push('A');
objar.resolve('B');
objar.resolve('C');
return 'A';
});
objar.define('B', function () {
calls.push('B');
this.resolve('D');
return 'B';
});
objar.define('C', function () {
calls.push('C');
this.resolve('D');
return 'C';
});
objar.define('D', function () {
calls.push('D');
objar.resolve('E');
return 'D';
});
objar.resolve('A');
expect(calls).toEqual(['A', 'B', 'D', 'E', 'C']);
});
it('should detect circular dependencies when resolving a service', function () {
// A -> B
// ^ |
// | v
// D <- C -> E
objar.define('A', function () {
this.resolve('B');
return 'A';
});
objar.define('B', function () {
this.resolve('C');
return 'B';
});
objar.define('E', function () {
return 'E';
});
objar.define('C', function () {
objar.resolve('D');
objar.resolve('E');
return 'C';
});
objar.define('D', function () {
this.resolve('A');
return 'D';
});
expect(function () {
objar.resolve('A');
}).toThrow(new Error('Circular dependency detected: A -> B -> C -> D -> A'));
expect(function () {
objar.resolve('B');
}).toThrow(new Error('Circular dependency detected: A -> B -> C -> D -> A'));
expect(function () {
objar.resolve('C');
}).toThrow(new Error('Circular dependency detected: A -> B -> C -> D -> A'));
expect(function () {
objar.resolve('D');
}).toThrow(new Error('Circular dependency detected: A -> B -> C -> D -> A'));
expect(objar.resolve('E')).toEqual('E');
});
it('should throw an error when invoking a badly annotated function', function () {
expect(function () {
objar.invoke(['a'])
}).toThrow(new Error('Objar.invoke() expects an annotated function'));
expect(function () {
objar.invoke(['a', 'b'])
}).toThrow(new Error('Objar.invoke() expects an annotated function'));
});
it('should throw an error when an annotated function needs an unexisting dependency', function () {
expect(function () {
objar.invoke(['a', function () {}]);
}).toThrow(new Error('The service "a" is not defined'));
});
it('should invoke an annotated function with the dependencies injected', function () {
// A -> B -> C
objar.define('A', function () {
this.resolve('B');
return 'A';
});
objar.define('B', function () {
this.resolve('C');
return 'B';
});
objar.define('C', function () {
return 'C';
});
objar.invoke(['A', 'B', 'C', function (x, y, z) {
expect(x).toEqual('A');
expect(y).toEqual('B');
expect(z).toEqual('C');
}]);
});
});