Skip to content

Commit

Permalink
fix(core): Fix fixture.detectChanges with autoDetect disabled and zon…
Browse files Browse the repository at this point in the history
…eless

When disabling autodetect (not recommeneded) with zoneless,
`fixture.detectChanges` would previously not refresh the fixture's component.
  • Loading branch information
atscott committed Aug 26, 2024
1 parent dc41d18 commit 6ba0329
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/core/test/component_fixture_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,20 @@ describe('ComponentFixture with zoneless', () => {
// still throws if checkNoChanges is not disabled
expect(() => fixture.detectChanges()).toThrowError(/ExpressionChanged/);
});

it('runs change detection when autoDetect is false', () => {
@Component({
template: '{{thing()}}',
standalone: true,
})
class App {
thing = signal(1);
}

const fixture = TestBed.createComponent(App);
fixture.autoDetectChanges(false);
fixture.componentInstance.thing.set(2);
fixture.detectChanges();
expect(fixture.nativeElement.innerText).toBe('2');
});
});
9 changes: 8 additions & 1 deletion packages/core/testing/src/component_fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ export class ComponentFixture<T> {
}

if (this.zonelessEnabled) {
this._appRef.tick();
try {
this._testAppRef.externalTestViews.add(this.componentRef.hostView);
this._appRef.tick();
} finally {
if (!this.autoDetect) {
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
}
}
} else {
// Run the change detection inside the NgZone so that any async tasks as part of the change
// detection are captured by the zone and can be waited for in isStable.
Expand Down

0 comments on commit 6ba0329

Please sign in to comment.