Repository navigation
Expand file tree
/
Copy pathinjectorSpec.js
More file actions
398 lines (301 loc) · 13.2 KB
/
Copy pathinjectorSpec.js
File metadata and controls
398 lines (301 loc) · 13.2 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
describe("Injector", function() {
var injector;
beforeEach(function() {
injector = new window.injector.Injector();
});
it("Has a defined injector", function() {
expect(injector).not.toBeNull();
});
it("Injects a basic type by variable name", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var someObject = {
someValue: 'inject'
};
injector.injectInto(someObject);
expect(someObject.someValue).toBe(someValue);
});
it("Injects a newly created type by variable name", function() {
var SomeClass = function() {
console.log('SomeClass -> SomeClass');
this.hello = "Hello World";
};
injector.map('someValue').toType(SomeClass);
var someObject = {
someValue: 'inject'
};
injector.injectInto(someObject);
expect(someObject.someValue.hello).toBe("Hello World");
});
it("Injects a specific type that doesn't have anything to do with the variable name", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var someObject = {
otherValue: 'inject:someValue'
};
injector.injectInto(someObject);
expect(someObject.otherValue).toBe(someValue);
});
it("injects an object by name", function() {
var someValue1 = "Hello World 1";
var someValue2 = "Hello World 2";
injector.map('someValue', 'one').toValue(someValue1);
injector.map('someValue', 'two').toValue(someValue2);
var someObject1 = {
someValue: 'inject(name="one")'
};
injector.injectInto(someObject1);
var someObject2 = {
someValue: 'inject(name="two")'
};
injector.injectInto(someObject2);
expect(someObject1.someValue).toBe(someValue1);
expect(someObject2.someValue).toBe(someValue2);
});
it("Injects a named specific type that doesn't have anything to do with the variable name", function() {
var someValue1 = "Hello World 1";
var someValue2 = "Hello World 2";
injector.map('someValue', 'one').toValue(someValue1);
injector.map('someValue', 'two').toValue(someValue2);
var someObject = {
otherValue1: 'inject(name="one"):someValue',
otherValue2: 'inject(name="two"):someValue'
};
injector.injectInto(someObject);
expect(someObject.otherValue1).toBe(someValue1);
expect(someObject.otherValue2).toBe(someValue2);
});
it("Calls post constructs", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var someObject = {
postConstructs: ['onPostConstruct'],
counter: 0,
someValue: 'inject',
onPostConstruct: function() {
this.counter++;
console.log('someObject -> onPostConstruct', this, this.counter);
}
};
injector.injectInto(someObject);
expect(someObject.counter).toBe(1);
});
it("Calls post constructs and injects on a newly created instance", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var MyClass = function () {};
MyClass.prototype = {
postConstructs: ['onPostConstruct'],
counter: 0,
someValue: 'inject',
onPostConstruct: function() {
this.counter++;
console.log('someObject -> onPostConstruct', this, this.counter);
}
};
var someObject = new MyClass();
injector.injectInto(someObject);
expect(someObject.counter).toBe(1);
expect(someObject.someValue).toBe(someValue);
});
it("returns an instance", function() {
var someObject = function(){};
someObject.prototype = { testVar: 'test'};
injector.map('someObject').toType(someObject);
var someCreatedObject1 = injector.getInstance('someObject');
expect(someCreatedObject1.testVar).toEqual('test');
});
it("returns two unique instances", function() {
var someObject = function(){};
someObject.prototype = { testVar: 'test'};
injector.map('someObject').toType(someObject);
var someCreatedObject1 = injector.getInstance('someObject');
var someCreatedObject2 = injector.getInstance('someObject');
someCreatedObject2.testVar = 'hello world';
expect(someCreatedObject1.testVar).not.toEqual(someCreatedObject2.testVar);
});
it("returns the same singleton instance", function() {
var someObject = function(){};
someObject.prototype = { testVar: 'test'};
injector.map('someObject').toSingleton(someObject);
var someCreatedObject1 = injector.getInstance('someObject');
var someCreatedObject2 = injector.getInstance('someObject');
someCreatedObject2.testVar = 'hello world';
expect(someCreatedObject1.testVar).toEqual(someCreatedObject2.testVar);
});
it("returns a specific error when there is no mapping", function() {
expect(function(){injector.getInstance('someObject')}).toThrow(new Error('Cannot return instance "someObject" because no mapping has been found'));
expect(function(){injector.getInstance('someObject', 'someName')}).toThrow(new Error('Cannot return instance "someObject by name someName" because no mapping has been found'));
});
it("can unmap mappings by type", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.getInstance('someValue')).toBe(someValue);
injector.unmap('someValue');
expect(function(){injector.getInstance('someValue')}).toThrow(new Error('Cannot return instance "someValue" because no mapping has been found'));
});
it("can unmap mappings by type and name", function() {
var someValue = "Hello World";
injector.map('someValue', 'myName').toValue(someValue);
expect(injector.getInstance('someValue', 'myName')).toBe(someValue);
injector.unmap('someValue', 'myName');
expect(function(){injector.getInstance('someValue', 'myName')}).toThrow(new Error('Cannot return instance "someValue by name myName" because no mapping has been found'));
});
it("registers itself by the injector", function() {
expect(injector.getInstance('injector')).toBe(injector);
});
it("can teardown itself (aka. unmapAll)", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.getInstance('someValue')).toBe(someValue);
injector.map('someValue2').toValue(someValue);
expect(injector.getInstance('someValue2')).toBe(someValue);
injector.teardown();
expect(function() {injector.getInstance('someValue')}).toThrow(new Error('Cannot return instance "someValue" because no mapping has been found'));
expect(function() {injector.getInstance('someValue2')}).toThrow(new Error('Cannot return instance "someValue2" because no mapping has been found'));
});
it("injects into returned instance when mapped with toType", function() {
var someValue = 'someValueToInject';
var SomeObject = function() {
this.someValue = 'inject';
};
injector.map('someValue').toValue(someValue);
injector.map('SomeObject').toType(SomeObject);
expect(injector.getInstance('SomeObject').someValue).toBe(someValue);
});
it("injects into returned instance when mapped with toSingleton", function() {
var someValue = 'someValueToInject';
var SomeObject = function() {
this.someValue = 'inject';
};
injector.map('someValue').toValue(someValue);
injector.map('SomeObject').toSingleton(SomeObject);
expect(injector.getInstance('SomeObject').someValue).toBe(someValue);
});
it("instantiates the singleton on demand", function() {
var someValue = 'someValueToInject';
var SomeObject = function() {
this.someValue = 'inject';
};
injector.map('SomeObject').toSingleton(SomeObject);
injector.map('someValue').toValue(someValue);
expect(injector.getInstance('SomeObject').someValue).toBe(someValue);
});
describe("childInjector", function() {
it("defaults to null when it was not instantiated by a parent", function() {
expect(injector.getParentInjector()).toBeNull();
});
it("can create a childInjector which references to its parent", function() {
var childInjector = injector.createChildInjector();
expect(childInjector).not.toBeNull();
expect(childInjector.getParentInjector()).toBe(injector);
expect(childInjector).not.toBe(injector);
});
it("has no parentInjector when it is the top parent", function() {
expect(injector.getParentInjector()).toBeNull();
var childInjector = injector.createChildInjector();
expect(injector.getParentInjector()).toBeNull();
});
it("can set the parentInjector", function() {
var parentInjector = new window.injector.Injector();
injector.setParentInjector(parentInjector);
expect(injector.getParentInjector()).toBe(parentInjector);
});
it("throws an error when trying to set a parentInjector which is not an injector (or null)", function() {
var parentInjector = {};
expect(function() {injector.setParentInjector(parentInjector)}).toThrow(new Error('Cannot set the parentInjector because it is not an injector'));
expect(injector.getParentInjector()).toBeNull();
});
it("can nullify the parentInjector", function() {
var parentInjector = new window.injector.Injector();
injector.setParentInjector(parentInjector);
expect(function() {injector.setParentInjector(null)}).not.toThrow();
expect(injector.getParentInjector()).toBeNull();
});
it("validates mappings on a child that stem from its parent as if it were its own mappings", function() {
var childInjector = injector.createChildInjector();
expect(injector.hasMapping('someValue')).toBe(false);
expect(childInjector.hasMapping('someValue')).toBe(false);
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.hasMapping('someValue')).toBe(true);
expect(childInjector.hasMapping('someValue')).toBe(true);
});
it("hides mappings from its parent", function() {
var childInjector = injector.createChildInjector();
var someValue = "Hello World";
childInjector.map('someValue').toValue(someValue);
expect(childInjector.hasMapping('someValue')).toBe(true);
expect(injector.hasMapping('someValue')).toBe(false);
});
it("returns the instance that was mapped on the parent", function() {
var childInjector = injector.createChildInjector();
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.getInstance('someValue')).toBe(someValue);
expect(childInjector.getInstance('someValue')).toBe(someValue);
});
it("throws an error when the parent tries to get access to a mapping that was only mapped on the childInjector", function() {
var childInjector = injector.createChildInjector();
var someValue = "Hello World";
childInjector.map('someValue').toValue(someValue);
expect(childInjector.getInstance('someValue')).toBe(someValue);
expect(function() {injector.getInstance('someValue')}).toThrow(new Error('Cannot return instance "someValue" because no mapping has been found'));
});
it("can create multiple child injectors", function() {
var injectorChild1 = injector.createChildInjector(),
injectorChild2 = injector.createChildInjector(),
injector1Child = injectorChild1.createChildInjector();
expect(injector.getParentInjector()).toBeNull();
expect(injectorChild1.getParentInjector()).toBe(injector);
expect(injectorChild2.getParentInjector()).toBe(injector);
expect(injector1Child.getParentInjector()).toBe(injectorChild1);
});
it("can access mappings from a parent multiple levels up", function() {
var injectorChild1 = injector.createChildInjector(),
injector1Child = injectorChild1.createChildInjector();
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var otherValue = "Hello child!";
injectorChild1.map('otherValue').toValue(otherValue);
expect(injector.getInstance('someValue')).toBe(someValue);
expect(injectorChild1.getInstance('someValue')).toBe(someValue);
expect(injector1Child.getInstance('someValue')).toBe(someValue);
expect(injectorChild1.getInstance('otherValue')).toBe(otherValue);
expect(injector1Child.getInstance('otherValue')).toBe(otherValue);
});
it("can create mappings for keys that already exist on the parent", function() {
var injectorChild = injector.createChildInjector();
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
var otherValue = "Hello child!";
expect(function() {
injectorChild.map('someValue').toValue(otherValue);
}).not.toThrow(new Error('Already has mapping for someValue'));
});
it("force maps itself as the injector", function() {
var injectorChild = injector.createChildInjector();
expect(injector).not.toBe(injectorChild);
expect(injector.getInstance('injector')).toBe(injector);
expect(injectorChild.getInstance('injector')).toBe(injectorChild);
});
it("can verify if the injector has a mapping", function() {
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.hasMapping('someValue')).toBeTruthy();
});
it("can verify if the injector has a mapping on the parent injector", function() {
var injectorChild = injector.createChildInjector();
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injectorChild.hasMapping('someValue')).toBeTruthy();
});
it("can verify if the injector has a direct mapping", function() {
var injectorChild = injector.createChildInjector();
var someValue = "Hello World";
injector.map('someValue').toValue(someValue);
expect(injector.hasDirectMapping('someValue')).toBeTruthy();
expect(injectorChild.hasDirectMapping('someValue')).toBeFalsy();
});
});
});