test(ivy): verify ngOnDestroy is called for tree-shakeable providers (#28943)

This test verifies that Ivy's module injector does not suffer from
#28927 to prevent regressing on this behavior going forward.

PR Close #28943
This commit is contained in:
JoostK 2019-02-23 16:23:25 +01:00 committed by Igor Minar
parent 30b04424a3
commit 5c13feebfd
1 changed files with 11 additions and 0 deletions

View File

@ -200,11 +200,14 @@ describe('InjectorDef-based createInjector()', () => {
}); });
} }
let scopedServiceDestroyed = false;
class ScopedService { class ScopedService {
static ngInjectableDef = defineInjectable({ static ngInjectableDef = defineInjectable({
providedIn: Module, providedIn: Module,
factory: () => new ScopedService(), factory: () => new ScopedService(),
}); });
ngOnDestroy(): void { scopedServiceDestroyed = true; }
} }
class WrongScopeService { class WrongScopeService {
@ -323,10 +326,18 @@ describe('InjectorDef-based createInjector()', () => {
it('calls ngOnDestroy on services when destroyed', () => { it('calls ngOnDestroy on services when destroyed', () => {
injector.get(DeepService); injector.get(DeepService);
expect(deepServiceDestroyed).toBe(false, 'DeepService already destroyed');
(injector as R3Injector).destroy(); (injector as R3Injector).destroy();
expect(deepServiceDestroyed).toBe(true, 'DeepService not destroyed'); expect(deepServiceDestroyed).toBe(true, 'DeepService not destroyed');
}); });
it('calls ngOnDestroy on scoped providers', () => {
injector.get(ScopedService);
expect(scopedServiceDestroyed).toBe(false, 'ScopedService already destroyed');
(injector as R3Injector).destroy();
expect(scopedServiceDestroyed).toBe(true, 'ScopedService not destroyed');
});
it('does not allow injection after destroy', () => { it('does not allow injection after destroy', () => {
(injector as R3Injector).destroy(); (injector as R3Injector).destroy();
expect(() => injector.get(DeepService)).toThrowError('Injector has already been destroyed.'); expect(() => injector.get(DeepService)).toThrowError('Injector has already been destroyed.');