Я пишу Angular 2 unit test. У меня есть подкомпонент @ViewChild
, который мне нужно распознать после инициализации компонента. В этом случае это компонент Timepicker
из библиотеки ng2-bootstrap, хотя специфика не должна иметь значения. После я detectChanges()
экземпляр подкомпонента по-прежнему undefined.
Псевдо-код:
@Component({
template: `
<form>
<timepicker
#timepickerChild
[(ngModel)]="myDate">
</timepicker>
</form>
`
})
export class ExampleComponent implements OnInit {
@ViewChild('timepickerChild') timepickerChild: TimepickerComponent;
public myDate = new Date();
}
// Spec
describe('Example Test', () => {
let exampleComponent: ExampleComponent;
let fixture: ComponentFixture<ExampleComponent>;
beforeEach(() => {
TestBed.configureTestingModel({
// ... whatever needs to be configured
});
fixture = TestBed.createComponent(ExampleComponent);
});
it('should recognize a timepicker'. async(() => {
fixture.detectChanges();
const timepickerChild: Timepicker = fixture.componentInstance.timepickerChild;
console.log('timepickerChild', timepickerChild)
}));
});
Псевдокод работает так, как ожидается, пока не дойдете до консольного журнала. timepickerChild
- undefined. Почему это происходит?