mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
fix(core): reset cached scope for components that were overridden using TestBed (#52916)
Currently, when a component is overriden using `TestBed.overrideComponent`, Angular retains calculated scope for that component (a set of components and directives used within a component). This may cause stale information to be used in tests in some cases. This commit updates the logic to reset overridden component scope, so it gets re-computed during the next invocation. Resolves #52817. PR Close #52916
This commit is contained in:
parent
f8d5b8464b
commit
ee892ee294
2 changed files with 53 additions and 0 deletions
|
|
@ -175,6 +175,56 @@ describe('TestBed with Standalone types', () => {
|
|||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('should override dependencies of standalone components', () => {
|
||||
@Component({
|
||||
selector: 'dep',
|
||||
standalone: true,
|
||||
template: 'main dep',
|
||||
})
|
||||
class MainDep {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'dep',
|
||||
standalone: true,
|
||||
template: 'mock dep',
|
||||
})
|
||||
class MockDep {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
standalone: true,
|
||||
imports: [MainDep],
|
||||
template: '<dep />',
|
||||
})
|
||||
class AppComponent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({imports: [AppComponent]});
|
||||
|
||||
let fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
|
||||
// No overrides defined, expecting main dependency to be used.
|
||||
expect(fixture.nativeElement.innerHTML).toBe('<dep>main dep</dep>');
|
||||
|
||||
// Emulate an end of a test.
|
||||
TestBed.resetTestingModule();
|
||||
|
||||
// Emulate the start of a next test, make sure previous overrides
|
||||
// are not persisted across tests.
|
||||
TestBed.configureTestingModule({imports: [AppComponent]});
|
||||
TestBed.overrideComponent(AppComponent, {set: {imports: [MockDep]}});
|
||||
|
||||
fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
|
||||
// Main dependency was overridden, expect to see a mock.
|
||||
expect(fixture.nativeElement.innerHTML).toBe('<dep>mock dep</dep>');
|
||||
});
|
||||
|
||||
|
||||
it('should override providers on standalone component itself', () => {
|
||||
const A = new InjectionToken('A');
|
||||
|
||||
|
|
|
|||
|
|
@ -396,6 +396,9 @@ export class TestBedCompiler {
|
|||
}
|
||||
|
||||
this.maybeStoreNgDef(NG_COMP_DEF, declaration);
|
||||
if (USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
|
||||
depsTracker.clearScopeCacheFor(declaration);
|
||||
}
|
||||
compileComponent(declaration, metadata);
|
||||
});
|
||||
this.pendingComponents.clear();
|
||||
|
|
|
|||
Loading…
Reference in a new issue