Skip to content

Commit 0300dd2

Browse files
atscottalxhub
authored andcommitted
fix(core): Fix fixture.detectChanges with autoDetect disabled and zoneless (#57416)
When disabling autodetect (not recommeneded) with zoneless, `fixture.detectChanges` would previously not refresh the fixture's component. PR Close #57416
1 parent 6a7c5ae commit 0300dd2

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/core/test/component_fixture_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,20 @@ describe('ComponentFixture with zoneless', () => {
581581
// still throws if checkNoChanges is not disabled
582582
expect(() => fixture.detectChanges()).toThrowError(/ExpressionChanged/);
583583
});
584+
585+
it('runs change detection when autoDetect is false', () => {
586+
@Component({
587+
template: '{{thing()}}',
588+
standalone: true,
589+
})
590+
class App {
591+
thing = signal(1);
592+
}
593+
594+
const fixture = TestBed.createComponent(App);
595+
fixture.autoDetectChanges(false);
596+
fixture.componentInstance.thing.set(2);
597+
fixture.detectChanges();
598+
expect(fixture.nativeElement.innerText).toBe('2');
599+
});
584600
});

packages/core/testing/src/component_fixture.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ export class ComponentFixture<T> {
140140
}
141141

142142
if (this.zonelessEnabled) {
143-
this._appRef.tick();
143+
try {
144+
this._testAppRef.externalTestViews.add(this.componentRef.hostView);
145+
this._appRef.tick();
146+
} finally {
147+
if (!this.autoDetect) {
148+
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
149+
}
150+
}
144151
} else {
145152
// Run the change detection inside the NgZone so that any async tasks as part of the change
146153
// detection are captured by the zone and can be waited for in isStable.

0 commit comments

Comments
 (0)