Skip to content

Commit

Permalink
refactor(core): Permit disabling autoDetect for zoneless fixture (ang…
Browse files Browse the repository at this point in the history
…ular#55494)

The caveat here is that this needs to be done before creating the
fixture. There are some technical issues to overcome with disabling it
after it was already enabled, related to detaching from `ApplicationRef`
without other side effects.

PR Close angular#55494
  • Loading branch information
atscott authored and AndrewKushnir committed Apr 26, 2024
1 parent 91b1f24 commit be17de5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/core/testing/src/component_fixture.ts
Expand Up @@ -171,8 +171,12 @@ export abstract class ComponentFixture<T> {
* `ApplicationRef.isStable`, and `autoDetectChanges` cannot be disabled.
*/
export class ScheduledComponentFixture<T> extends ComponentFixture<T> {
private _autoDetect = inject(ComponentFixtureAutoDetect, {optional: true}) ?? true;

initialize(): void {
this._appRef.attachView(this.componentRef.hostView);
if (this._autoDetect) {
this._appRef.attachView(this.componentRef.hostView);
}
}

override detectChanges(checkNoChanges: boolean = true): void {
Expand All @@ -189,7 +193,12 @@ export class ScheduledComponentFixture<T> extends ComponentFixture<T> {

override autoDetectChanges(autoDetect?: boolean|undefined): void {
if (!autoDetect) {
throw new Error('Cannot disable autoDetect when using the zoneless scheduler.');
throw new Error(
'Cannot disable autoDetect after it has been enabled when using the zoneless scheduler. ' +
'To disable autoDetect, add `{provide: ComponentFixtureAutoDetect, useValue: false}` to the TestBed providers.');
} else if (!this._autoDetect) {
this._autoDetect = autoDetect;
this._appRef.attachView(this.componentRef.hostView);
}
this.detectChanges();
}
Expand Down

0 comments on commit be17de5

Please sign in to comment.