@@ -11,6 +11,7 @@ import {
11
11
afterRender ,
12
12
ClassProvider ,
13
13
Component ,
14
+ createEnvironmentInjector ,
14
15
Directive ,
15
16
ElementRef ,
16
17
inject ,
@@ -43,6 +44,8 @@ import {
43
44
InjectorProfilerEventType ,
44
45
ProviderConfiguredEvent ,
45
46
setInjectorProfiler ,
47
+ injectorProfiler ,
48
+ InjectorProfilerContext ,
46
49
} from '../../src/render3/debug/injector_profiler' ;
47
50
import { getNodeInjectorLView , NodeInjector } from '../../src/render3/di' ;
48
51
import {
@@ -103,7 +106,7 @@ describe('setProfiler', () => {
103
106
} ) ;
104
107
} ) ;
105
108
106
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
109
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
107
110
108
111
it ( 'should emit DI events when a component contains a provider and injects it' , ( ) => {
109
112
class MyService { }
@@ -382,6 +385,70 @@ describe('setProfiler', () => {
382
385
} ) ;
383
386
} ) ;
384
387
388
+ describe ( 'profiler activation and removal' , ( ) => {
389
+ class SomeClass { }
390
+
391
+ const fakeContext : InjectorProfilerContext = {
392
+ injector : Injector . create ( { providers : [ ] } ) ,
393
+ token : SomeClass ,
394
+ } ;
395
+
396
+ const fakeEvent : InjectorCreatedInstanceEvent = {
397
+ type : InjectorProfilerEventType . InstanceCreatedByInjector ,
398
+ context : fakeContext ,
399
+ instance : { value : new SomeClass ( ) } ,
400
+ } ;
401
+
402
+ it ( 'should allow adding and removing multiple profilers' , ( ) => {
403
+ const events : string [ ] = [ ] ;
404
+ const r1 = setInjectorProfiler ( ( e ) => events . push ( 'P1: ' + e . type ) ) ;
405
+ const r2 = setInjectorProfiler ( ( e ) => events . push ( 'P2: ' + e . type ) ) ;
406
+
407
+ injectorProfiler ( fakeEvent ) ;
408
+ expect ( events ) . toEqual ( [ 'P1: 1' , 'P2: 1' ] ) ;
409
+
410
+ r1 ( ) ;
411
+ injectorProfiler ( fakeEvent ) ;
412
+ expect ( events ) . toEqual ( [ 'P1: 1' , 'P2: 1' , 'P2: 1' ] ) ;
413
+
414
+ r2 ( ) ;
415
+ injectorProfiler ( fakeEvent ) ;
416
+ expect ( events ) . toEqual ( [ 'P1: 1' , 'P2: 1' , 'P2: 1' ] ) ;
417
+ } ) ;
418
+
419
+ it ( 'should not add / remove the same profiler twice' , ( ) => {
420
+ const events : string [ ] = [ ] ;
421
+ const p1 = ( e : InjectorProfilerEvent ) => events . push ( 'P1: ' + e . type ) ;
422
+ const r1 = setInjectorProfiler ( p1 ) ;
423
+ const r2 = setInjectorProfiler ( p1 ) ;
424
+
425
+ injectorProfiler ( fakeEvent ) ;
426
+ expect ( events ) . toEqual ( [ 'P1: 1' ] ) ;
427
+
428
+ r1 ( ) ;
429
+ injectorProfiler ( fakeEvent ) ;
430
+ expect ( events ) . toEqual ( [ 'P1: 1' ] ) ;
431
+
432
+ // subsequent removals should be noop
433
+ r1 ( ) ;
434
+ r2 ( ) ;
435
+ } ) ;
436
+
437
+ it ( 'should clear all profilers when passing null' , ( ) => {
438
+ const events : string [ ] = [ ] ;
439
+ setInjectorProfiler ( ( e ) => events . push ( 'P1: ' + e . type ) ) ;
440
+ setInjectorProfiler ( ( e ) => events . push ( 'P2: ' + e . type ) ) ;
441
+
442
+ injectorProfiler ( fakeEvent ) ;
443
+ expect ( events ) . toEqual ( [ 'P1: 1' , 'P2: 1' ] ) ;
444
+
445
+ // clear all profilers
446
+ setInjectorProfiler ( null ) ;
447
+ injectorProfiler ( fakeEvent ) ;
448
+ expect ( events ) . toEqual ( [ 'P1: 1' , 'P2: 1' ] ) ;
449
+ } ) ;
450
+ } ) ;
451
+
385
452
describe ( 'getInjectorMetadata' , ( ) => {
386
453
it ( 'should be able to determine injector type and name' , fakeAsync ( ( ) => {
387
454
class MyServiceA { }
@@ -487,7 +554,7 @@ describe('getInjectorMetadata', () => {
487
554
488
555
describe ( 'getInjectorProviders' , ( ) => {
489
556
beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
490
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
557
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
491
558
492
559
it ( 'should be able to get the providers from a components injector' , ( ) => {
493
560
class MyService { }
@@ -953,7 +1020,7 @@ describe('getInjectorProviders', () => {
953
1020
954
1021
describe ( 'getDependenciesFromInjectable' , ( ) => {
955
1022
beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
956
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
1023
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
957
1024
958
1025
it ( 'should be able to determine which injector dependencies come from' , fakeAsync ( ( ) => {
959
1026
class MyService { }
@@ -1244,7 +1311,7 @@ describe('getDependenciesFromInjectable', () => {
1244
1311
1245
1312
describe ( 'getInjectorResolutionPath' , ( ) => {
1246
1313
beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
1247
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
1314
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
1248
1315
1249
1316
it ( 'should be able to inspect injector hierarchy structure' , fakeAsync ( ( ) => {
1250
1317
class MyServiceA { }
0 commit comments