8000 fixup! fix(ivy): component destroy hook called twice when configured … · angular/angular@8ee55fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ee55fe

Browse files
committed
fixup! fix(ivy): component destroy hook called twice when configured as provider
1 parent bfe690b commit 8ee55fe

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/core/test/render3/component_spec.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,29 +706,44 @@ describe('view destruction', () => {
706706
});
707707

708708
it('should invoke onDestroy only once when a component is registered as a provider', () => {
709-
const testToken = new InjectionToken<ComponentWithOnDestroy>('testToken');
709+
const testToken = new InjectionToken<ParentWithOnDestroy>('testToken');
710710
let destroyCalls = 0;
711711

712-
class ComponentWithOnDestroy {
712+
class ParentWithOnDestroy {
713713
static ngComponentDef = defineComponent({
714714
selectors: [['comp-with-on-destroy']],
715-
type: ComponentWithOnDestroy,
715+
type: ParentWithOnDestroy,
716716
consts: 0,
717717
vars: 0,
718-
factory: () => new ComponentWithOnDestroy(),
718+
factory: () => new ParentWithOnDestroy(),
719719
template: (rf: any, ctx: any) => {},
720-
features: [ProvidersFeature([{provide: testToken, useExisting: ComponentWithOnDestroy}])]
720+
features: [ProvidersFeature([{provide: testToken, useExisting: ParentWithOnDestroy}])]
721721
});
722722

723723
ngOnDestroy() { destroyCalls++; }
724724
}
725725

726+
class ChildComponent {
727+
static ngComponentDef = defineComponent({
728+
selectors: [['child']],
729+
type: ChildComponent,
730+
consts: 0,
731+
vars: 0,
732+
factory: () => new ChildComponent(directiv B33E eInject(testToken)),
733+
template: (rf: any, ctx: any) => {},
734+
});
735+
736+
// We need to inject the parent so the provider is instantiated.
737+
constructor(_parent: ParentWithOnDestroy) {}
738+
}
739+
726740
const FixtureComponent = createComponent('test-app', (rf: RenderFlags, ctx: any) => {
727741
if (rf & RenderFlags.Create) {
728-
element(0, 'comp-with-on-destroy');
729-
directiveInject(testToken); // Inject the token so the provider gets instantiated.
742+
elementStart(0, 'comp-with-on-destroy');
743+
element(1, 'child');
744+
elementEnd();
730745
}
731-
}, 1, 0, [ComponentWithOnDestroy], [], null, [], []);
746+
}, 2, 0, [ParentWithOnDestroy, ChildComponent], [], null, [], []);
732747

733748
const fixture = new ComponentFixture(FixtureComponent);
734749
fixture.update();

0 commit comments

Comments
 (0)
0