Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test(core): add test validating that component destroy does not depen…
…d on the injector

While developing this feature, I attempted to inject a service during component destruction (to call `SharedStylesHost.prototype.removeHost`), however this broke some Material use cases which seem to destroy the injector before the component. Whether this constraint was intended or not, I believe we currently live in a world where component destruction cannot depend on its injection, so I'm enforcing this in a test to be clear about that constraint.

This is not intended to imply that this is a _good_ constraint or that we _want_ this to be the case, but it does represent the reality we currently exist in.
  • Loading branch information
dgp1130 committed Mar 28, 2026
commit 17f2628fc6801bdfa44e6482d6903a98cb543b61
40 changes: 40 additions & 0 deletions packages/core/test/acceptance/view_container_ref_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,46 @@ describe('ViewContainerRef', () => {
});
});

describe('destroy', () => {
// NOTE: We may not necessarily _want_ this to be the case, but it does seem to be necessary
// for some amount of Angular code out there. We may want to consider lifting this constraint.
it('should not throw if the injector is destroyed before a view is removed', () => {
@Component({
template: 'View Content',
standalone: false,
})
class DynamicComponent {}

@Component({
template: '<ng-container #vcr></ng-container>',
standalone: false,
})
class App {
@ViewChild('vcr', {read: ViewContainerRef, static: true})
vcr!: ViewContainerRef;
}

TestBed.configureTestingModule({declarations: [App, DynamicComponent]});

const envInjector = createEnvironmentInjector([], TestBed.inject(EnvironmentInjector));
const fixture = TestBed.createComponent(App);
fixture.detectChanges();

const componentRef = fixture.componentInstance.vcr.createComponent(DynamicComponent, {
environmentInjector: envInjector,
});
fixture.detectChanges();

// Destroy the environment injector first
envInjector.destroy();

// Should be able to destroy the component afterwards.
expect(() => {
componentRef.destroy();
}).not.toThrow();
});
});

describe('length', () => {
it('should return the number of embedded views', () => {
TestBed.configureTestingModule({declarations: [EmbeddedViewInsertionComp, VCRefDirective]});
Expand Down
Loading