66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Component , createComponent , createEnvironmentInjector , ENVIRONMENT_INITIALIZER , EnvironmentInjector , inject , InjectFlags , InjectionToken , INJECTOR , Injector , NgModuleRef , ViewContainerRef } from '@angular/core' ;
9+ import { Component , createComponent , createEnvironmentInjector , DestroyRef , ENVIRONMENT_INITIALIZER , EnvironmentInjector , inject , InjectFlags , InjectionToken , INJECTOR , Injector , NgModuleRef , ViewContainerRef } from '@angular/core' ;
1010import { R3Injector } from '@angular/core/src/di/r3_injector' ;
1111import { RuntimeError , RuntimeErrorCode } from '@angular/core/src/errors' ;
1212import { TestBed } from '@angular/core/testing' ;
@@ -27,6 +27,27 @@ describe('environment injector', () => {
2727 expect ( destroyed ) . toBeTrue ( ) ;
2828 } ) ;
2929
30+ it ( 'should allow unregistration while destroying' , ( ) => {
31+ const destroyedLog : string [ ] = [ ] ;
32+
33+ const parentEnvInjector = TestBed . inject ( EnvironmentInjector ) ;
34+ const envInjector = createEnvironmentInjector ( [ ] , parentEnvInjector ) ;
35+ const destroyRef = envInjector . get ( DestroyRef ) ;
36+
37+ const unregister = destroyRef . onDestroy ( ( ) => {
38+ destroyedLog . push ( 'first' ) ;
39+ unregister ( ) ;
40+ } ) ;
41+ destroyRef . onDestroy ( ( ) => {
42+ destroyedLog . push ( 'second' ) ;
43+ } ) ;
44+
45+ expect ( destroyedLog ) . toEqual ( [ ] ) ;
46+
47+ envInjector . destroy ( ) ;
48+ expect ( destroyedLog ) . toEqual ( [ 'first' , 'second' ] ) ;
49+ } ) ;
50+
3051 it ( 'should see providers from a parent EnvInjector' , ( ) => {
3152 class Service { }
3253
0 commit comments