angular - Modificando un componente definido en TestBed.overrideComponent
Estamos usando
TestBed.overrideComponent(CoolComponent, {
set: {
template: '<div id="fake-component">i am the fake component</div>',
selector: 'our-cool-component',
inputs: [ 'model' ]
}
})
anular un componente.
El componente tiene un ViewChild que configuramos en nuestro método ngOnInit
@Component({
selector: 'our-cool-component',
templateUrl: 'cool.component.html'
})
export class CoolComponent implements OnInit, OnDestroy {
@Input() model: SomeModel
@ViewChild(CoolChildComponent) coolChildComponent;
ngOnInit() {
this.coolChildComponent.doStuff();
}
}
El CoolComponent a su vez vive en un componente Wrapper.
Cuando llamamos a fixture.detectChanges() en el fixture Wrapper, esto intenta construir el CoolComponent, pero muere inmediatamente cuando llama a doStuff() porque CoolChildComponent está indefinido.
¿Existe una forma de acceder al CoolComponent para simular su CoolChildComponent? No parece que podamos obtenerlo desde el Wrapper porque solo se hace referencia a él a través de la plantilla, no como una propiedad del componente.
Respuesta #1
ngOnInit() {
this.coolChildComponent.doStuff();
}
debería ser
ngAfterViewInit() {
this.coolChildComponent.doStuff();
}
porque
La vista del hijo se establece antes de que se llame al callback ngAfterViewInit.
