-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathcontainer_test.js
More file actions
696 lines (564 loc) · 22.6 KB
/
container_test.js
File metadata and controls
696 lines (564 loc) · 22.6 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import { getOwner } from '@ember/-internals/owner';
import Service from '@ember/service';
import { Registry } from '..';
import { factory, moduleFor, AbstractTestCase, runTask } from 'internal-test-helpers';
moduleFor(
'Container.lookup',
class extends AbstractTestCase {
['@test lookup returns a fresh instance if singleton: false is passed as an option'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.register('controller:post', PostController);
let postController1 = container.lookup('controller:post');
let postController2 = container.lookup('controller:post', {
singleton: false,
});
let postController3 = container.lookup('controller:post', {
singleton: false,
});
let postController4 = container.lookup('controller:post');
assert.equal(
postController1.toString(),
postController4.toString(),
'Singleton factories looked up normally return the same value'
);
assert.notEqual(
postController1.toString(),
postController2.toString(),
'Singleton factories are not equal to factories looked up with singleton: false'
);
assert.notEqual(
postController2.toString(),
postController3.toString(),
'Two factories looked up with singleton: false are not equal'
);
assert.notEqual(
postController3.toString(),
postController4.toString(),
'A singleton factory looked up after a factory called with singleton: false is not equal'
);
assert.ok(
postController1 instanceof PostController,
'All instances are instances of the registered factory'
);
assert.ok(
postController2 instanceof PostController,
'All instances are instances of the registered factory'
);
assert.ok(
postController3 instanceof PostController,
'All instances are instances of the registered factory'
);
assert.ok(
postController4 instanceof PostController,
'All instances are instances of the registered factory'
);
}
['@test lookup returns a fresh instance if singleton: false is passed as an option to lookup'](
assert
) {
class TestFactory {
constructor(opts) {
Object.assign(this, opts);
}
static create(opts) {
return new this(opts);
}
}
let registry = new Registry();
let container = registry.container();
registry.register('thing:test/obj', TestFactory);
let instance1 = container.lookup('thing:test/obj');
let instance2 = container.lookup('thing:test/obj', {
singleton: false,
});
let instance3 = container.lookup('thing:test/obj', {
singleton: false,
});
let instance4 = container.lookup('thing:test/obj');
assert.ok(
instance1 === instance4,
'factories looked up up without singleton: false are the same instance'
);
assert.ok(
instance1 !== instance2,
'factories looked up with singleton: false are a different instance'
);
assert.ok(
instance2 !== instance3,
'factories looked up with singleton: false are a different instance'
);
assert.ok(
instance3 !== instance4,
'factories looked up after a call to singleton: false is a different instance'
);
assert.ok(
instance1 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance2 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance3 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance4 instanceof TestFactory,
'All instances are instances of the registered factory'
);
}
['@test lookup returns a fresh instance if singleton: false is passed as an option to register'](
assert
) {
class TestFactory {
constructor(opts) {
Object.assign(this, opts);
}
static create(opts) {
return new this(opts);
}
}
let registry = new Registry();
let container = registry.container();
registry.register('thing:test/obj', TestFactory, { singleton: false });
let instance1 = container.lookup('thing:test/obj');
let instance2 = container.lookup('thing:test/obj');
let instance3 = container.lookup('thing:test/obj');
assert.ok(instance1 !== instance2, 'each lookup is a different instance');
assert.ok(instance2 !== instance3, 'each lookup is a different instance');
assert.ok(instance1 !== instance3, 'each lookup is a different instance');
assert.ok(
instance1 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance2 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance3 instanceof TestFactory,
'All instances are instances of the registered factory'
);
}
['@test lookup returns a singleton instance if singleton: true is passed as an option even if registered as singleton: false'](
assert
) {
class TestFactory {
constructor(opts) {
Object.assign(this, opts);
}
static create(opts) {
return new this(opts);
}
}
let registry = new Registry();
let container = registry.container();
registry.register('thing:test/obj', TestFactory, { singleton: false });
let instance1 = container.lookup('thing:test/obj');
let instance2 = container.lookup('thing:test/obj', { singleton: true });
let instance3 = container.lookup('thing:test/obj', { singleton: true });
let instance4 = container.lookup('thing:test/obj');
assert.ok(instance1 !== instance2, 'each lookup is a different instance');
assert.ok(instance2 === instance3, 'each singleton: true lookup is the same instance');
assert.ok(instance3 !== instance4, 'each lookup is a different instance');
assert.ok(instance1 !== instance4, 'each lookup is a different instance');
assert.ok(
instance1 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance2 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance3 instanceof TestFactory,
'All instances are instances of the registered factory'
);
assert.ok(
instance4 instanceof TestFactory,
'All instances are instances of the registered factory'
);
}
}
);
moduleFor(
'Container',
class extends AbstractTestCase {
['@test A registered factory returns the same instance each time'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.register('controller:post', PostController);
let postController = container.lookup('controller:post');
assert.ok(
postController instanceof PostController,
'The lookup is an instance of the factory'
);
assert.equal(postController, container.lookup('controller:post'));
}
['@test A non-singleton instance is never cached'](assert) {
let registry = new Registry();
let container = registry.container();
let PostView = factory();
registry.register('view:post', PostView, { singleton: false });
let postView1 = container.lookup('view:post');
let postView2 = container.lookup('view:post');
assert.ok(postView1 !== postView2, 'Non-singletons are not cached');
}
['@test A non-instantiated property is not instantiated'](assert) {
let registry = new Registry();
let container = registry.container();
let template = function () {};
registry.register('template:foo', template, { instantiate: false });
assert.equal(container.lookup('template:foo'), template);
}
['@test A failed lookup returns undefined'](assert) {
let registry = new Registry();
let container = registry.container();
assert.equal(container.lookup('doesnot:exist'), undefined);
}
['@test An invalid factory throws an error']() {
let registry = new Registry();
let container = registry.container();
registry.register('controller:foo', {});
expectAssertion(() => {
container.lookup('controller:foo');
}, /Failed to create an instance of 'controller:foo'/);
}
['@test The container returns same value each time even if the value is falsey'](assert) {
let registry = new Registry();
let container = registry.container();
registry.register('falsy:value', null, { instantiate: false });
assert.strictEqual(container.lookup('falsy:value'), container.lookup('falsy:value'));
}
['@test The container can use a registry hook to resolve factories lazily'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.resolver = {
resolve(fullName) {
if (fullName === 'controller:post') {
return PostController;
}
},
};
let postController = container.lookup('controller:post');
assert.ok(postController instanceof PostController, 'The correct factory was provided');
}
['@test The container normalizes names before resolving'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.normalizeFullName = function () {
return 'controller:post';
};
registry.register('controller:post', PostController);
let postController = container.lookup('controller:normalized');
assert.ok(postController instanceof PostController, 'Normalizes the name before resolving');
}
['@test The container normalizes names when looking factory up'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
registry.normalizeFullName = function () {
return 'controller:post';
};
registry.register('controller:post', PostController);
let fact = container.factoryFor('controller:normalized');
let factInstance = fact.create();
assert.ok(factInstance instanceof PostController, 'Normalizes the name');
}
['@test Options can be registered that should be applied to a given factory'](assert) {
let registry = new Registry();
let container = registry.container();
let PostView = factory();
registry.resolver = {
resolve(fullName) {
if (fullName === 'view:post') {
return PostView;
}
},
};
registry.options('view:post', { instantiate: true, singleton: false });
let postView1 = container.lookup('view:post');
let postView2 = container.lookup('view:post');
assert.ok(postView1 instanceof PostView, 'The correct factory was provided');
assert.ok(postView2 instanceof PostView, 'The correct factory was provided');
assert.ok(postView1 !== postView2, 'The two lookups are different');
}
['@test Options can be registered that should be applied to all factories for a given type'](
assert
) {
let registry = new Registry();
let container = registry.container();
let PostView = factory();
registry.resolver = {
resolve(fullName) {
if (fullName === 'view:post') {
return PostView;
}
},
};
registry.optionsForType('view', { singleton: false });
let postView1 = container.lookup('view:post');
let postView2 = container.lookup('view:post');
assert.ok(postView1 instanceof PostView, 'The correct factory was provided');
assert.ok(postView2 instanceof PostView, 'The correct factory was provided');
assert.ok(postView1 !== postView2, 'The two lookups are different');
}
['@test Factory resolves are cached'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
let resolveWasCalled = [];
registry.resolve = function (fullName) {
resolveWasCalled.push(fullName);
return PostController;
};
assert.deepEqual(resolveWasCalled, []);
container.factoryFor('controller:post');
assert.deepEqual(resolveWasCalled, ['controller:post']);
container.factoryFor('controller:post');
assert.deepEqual(resolveWasCalled, ['controller:post']);
}
['@test factory for non extendables (MODEL) resolves are cached'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = factory();
let resolveWasCalled = [];
registry.resolve = function (fullName) {
resolveWasCalled.push(fullName);
return PostController;
};
assert.deepEqual(resolveWasCalled, []);
container.factoryFor('model:post');
assert.deepEqual(resolveWasCalled, ['model:post']);
container.factoryFor('model:post');
assert.deepEqual(resolveWasCalled, ['model:post']);
}
['@test factory for non extendables resolves are cached'](assert) {
let registry = new Registry();
let container = registry.container();
let PostController = {};
let resolveWasCalled = [];
registry.resolve = function (fullName) {
resolveWasCalled.push(fullName);
return PostController;
};
assert.deepEqual(resolveWasCalled, []);
container.factoryFor('foo:post');
assert.deepEqual(resolveWasCalled, ['foo:post']);
container.factoryFor('foo:post');
assert.deepEqual(resolveWasCalled, ['foo:post']);
}
[`@test A factory's lazy injections are validated when first instantiated`]() {
let registry = new Registry();
let container = registry.container();
let Apple = factory();
let Orange = factory();
Apple.reopenClass({
_lazyInjections() {
return [{ specifier: 'orange:main' }, { specifier: 'banana:main' }];
},
});
registry.register('apple:main', Apple);
registry.register('orange:main', Orange);
expectAssertion(() => {
container.lookup('apple:main');
}, /Attempting to inject an unknown injection: 'banana:main'/);
}
['@test Lazy injection validations are cached'](assert) {
if (!import.meta.env?.DEV) {
assert.expect(0);
return;
}
assert.expect(1);
let registry = new Registry();
let container = registry.container();
let Apple = factory();
let Orange = factory();
Apple.reopenClass({
_lazyInjections: () => {
assert.ok(true, 'should call lazy injection method');
return [{ specifier: 'orange:main' }];
},
});
registry.register('apple:main', Apple);
registry.register('orange:main', Orange);
container.lookup('apple:main');
container.lookup('apple:main');
}
['@test An object with its owner pre-set should be returned from ownerInjection'](assert) {
let owner = {};
let registry = new Registry();
let container = registry.container({ owner });
let result = container.ownerInjection();
assert.equal(getOwner(result), owner, 'owner is properly included');
}
['@test ownerInjection should be usable to create a service for testing'](assert) {
assert.expect(0);
let owner = {};
let registry = new Registry();
let container = registry.container({ owner });
let result = container.ownerInjection();
Service.create(result);
}
['@test #factoryFor class is registered class'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
let factoryManager = container.factoryFor('component:foo-bar');
assert.deepEqual(factoryManager.class, Component, 'No double extend');
}
['@test #factoryFor must supply a fullname']() {
let registry = new Registry();
let container = registry.container();
expectAssertion(() => {
container.factoryFor('chad-bar');
}, /fullName must be a proper full name/);
}
['@test #factoryFor returns a factory manager'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
let factoryManager = container.factoryFor('component:foo-bar');
assert.ok(factoryManager.create);
assert.ok(factoryManager.class);
}
['@test #factoryFor returns a cached factory manager for the same type'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
registry.register('component:baz-bar', Component);
let factoryManager1 = container.factoryFor('component:foo-bar');
let factoryManager2 = container.factoryFor('component:foo-bar');
let factoryManager3 = container.factoryFor('component:baz-bar');
assert.equal(factoryManager1, factoryManager2, 'cache hit');
assert.notEqual(factoryManager1, factoryManager3, 'cache miss');
}
['@test #factoryFor class returns the factory function'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
let factoryManager = container.factoryFor('component:foo-bar');
assert.deepEqual(factoryManager.class, Component, 'No double extend');
}
['@test #factoryFor instance have a common parent'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
let factoryManager1 = container.factoryFor('component:foo-bar');
let factoryManager2 = container.factoryFor('component:foo-bar');
let instance1 = factoryManager1.create({ foo: 'foo' });
let instance2 = factoryManager2.create({ bar: 'bar' });
assert.deepEqual(instance1.constructor, instance2.constructor);
}
['@test can properly reset cache'](assert) {
let registry = new Registry();
let container = registry.container();
let Component = factory();
registry.register('component:foo-bar', Component);
let factory1 = container.factoryFor('component:foo-bar');
let factory2 = container.factoryFor('component:foo-bar');
let instance1 = container.lookup('component:foo-bar');
let instance2 = container.lookup('component:foo-bar');
assert.equal(instance1, instance2);
assert.equal(factory1, factory2);
container.reset();
let factory3 = container.factoryFor('component:foo-bar');
let instance3 = container.lookup('component:foo-bar');
assert.notEqual(instance1, instance3);
assert.notEqual(factory1, factory3);
}
[`@test assert when calling lookup after destroy on a container`](assert) {
let registry = new Registry();
let container = registry.container();
registry.register('service:foo', factory());
let instance = container.lookup('service:foo');
assert.ok(instance, 'precond lookup successful');
runTask(() => {
container.destroy();
container.finalizeDestroy();
});
assert.throws(() => {
container.lookup('service:foo');
}, "Cannot call `.lookup('service:foo')` after the owner has been destroyed");
}
[`@test assert when calling factoryFor after destroy on a container`](assert) {
let registry = new Registry();
let container = registry.container();
registry.register('service:foo', factory());
let instance = container.lookup('service:foo');
assert.ok(instance, 'precond lookup successful');
runTask(() => {
container.destroy();
container.finalizeDestroy();
});
assert.throws(() => {
container.factoryFor('service:foo');
}, "Cannot call `.factoryFor('service:foo')` after the owner has been destroyed");
}
// this is skipped until templates and the glimmer environment do not require `OWNER` to be
// passed in as constructor args
['@skip #factoryFor does not add properties to the object being instantiated'](assert) {
let registry = new Registry();
let container = registry.container();
class Component {
static create(options) {
let instance = new this();
Object.assign(instance, options);
return instance;
}
}
registry.register('component:foo-bar', Component);
let componentFactory = container.factoryFor('component:foo-bar');
let instance = componentFactory.create();
// note: _guid and isDestroyed are being set in the `factory` constructor
// not via registry/container shenanigans
assert.deepEqual(Object.keys(instance), []);
}
'@test instantiating via container.lookup during destruction enqueues destruction'(assert) {
let registry = new Registry();
let container = registry.container();
let otherInstance;
class Service extends factory() {
destroy() {
otherInstance = container.lookup('service:other');
assert.ok(otherInstance.isDestroyed, 'service:other was destroyed');
}
}
registry.register('service:foo', Service);
registry.register('service:other', factory());
let instance = container.lookup('service:foo');
assert.ok(instance, 'precond lookup successful');
runTask(() => {
container.destroy();
container.finalizeDestroy();
});
}
'@test instantiating via container.factoryFor().create() after destruction throws an error'(
assert
) {
let registry = new Registry();
let container = registry.container();
registry.register('service:foo', factory());
registry.register('service:other', factory());
let Factory = container.factoryFor('service:other');
runTask(() => {
container.destroy();
container.finalizeDestroy();
});
assert.throws(() => {
Factory.create();
}, /Cannot create new instances after the owner has been destroyed \(you attempted to create service:other\)/);
}
}
);