@@ -43,6 +43,8 @@ import {
43
43
InjectorProfilerEventType ,
44
44
ProviderConfiguredEvent ,
45
45
setInjectorProfiler ,
46
+ injectorProfiler ,
47
+ InjectorProfilerContext ,
46
48
} from '../../src/render3/debug/injector_profiler' ;
47
49
import { getNodeInjectorLView , NodeInjector } from '../../src/render3/di' ;
48
50
import {
@@ -75,6 +77,7 @@ describe('setProfiler', () => {
75
77
createEvents = [ ] ;
76
78
providerConfiguredEvents = [ ] ;
77
79
80
+ setInjectorProfiler ( null ) ;
78
81
setInjectorProfiler ( ( injectorProfilerEvent : InjectorProfilerEvent ) => {
79
82
const { type} = injectorProfilerEvent ;
80
83
if ( type === InjectorProfilerEventType . Inject ) {
@@ -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,7 +385,77 @@ 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' , ( ) => {
453
+ beforeEach ( ( ) => {
454
+ setInjectorProfiler ( null ) ;
455
+ setupFrameworkInjectorProfiler ( ) ;
456
+ } ) ;
457
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
458
+
386
459
it ( 'should be able to determine injector type and name' , fakeAsync ( ( ) => {
387
460
class MyServiceA { }
388
461
@NgModule ( { providers : [ MyServiceA ] } )
@@ -486,8 +559,11 @@ describe('getInjectorMetadata', () => {
486
559
} ) ;
487
560
488
561
describe ( 'getInjectorProviders' , ( ) => {
489
- beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
490
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
562
+ beforeEach ( ( ) => {
563
+ setInjectorProfiler ( null ) ;
564
+ setupFrameworkInjectorProfiler ( ) ;
565
+ } ) ;
566
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
491
567
492
568
it ( 'should be able to get the providers from a components injector' , ( ) => {
493
569
class MyService { }
@@ -952,8 +1028,11 @@ describe('getInjectorProviders', () => {
952
1028
} ) ;
953
1029
954
1030
describe ( 'getDependenciesFromInjectable' , ( ) => {
955
- beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
956
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
1031
+ beforeEach ( ( ) => {
1032
+ setInjectorProfiler ( null ) ;
1033
+ setupFrameworkInjectorProfiler ( ) ;
1034
+ } ) ;
1035
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
957
1036
958
1037
it ( 'should be able to determine which injector dependencies come from' , fakeAsync ( ( ) => {
959
1038
class MyService { }
@@ -1243,8 +1322,11 @@ describe('getDependenciesFromInjectable', () => {
1243
1322
} ) ;
1244
1323
1245
1324
describe ( 'getInjectorResolutionPath' , ( ) => {
1246
- beforeEach ( ( ) => setupFrameworkInjectorProfiler ( ) ) ;
1247
- afterAll ( ( ) => setInjectorProfiler ( null ) ) ;
1325
+ beforeEach ( ( ) => {
1326
+ setInjectorProfiler ( null ) ;
1327
+ setupFrameworkInjectorProfiler ( ) ;
1328
+ } ) ;
1329
+ afterEach ( ( ) => setInjectorProfiler ( null ) ) ;
1248
1330
1249
1331
it ( 'should be able to inspect injector hierarchy structure' , fakeAsync ( ( ) => {
1250
1332
class MyServiceA { }
0 commit comments