【发布时间】:2017-06-22 09:53:12
【问题描述】:
我有以下功能进行单元测试。我在组件中采用了带有视图子元素的文本框元素,在测试中我需要测试在调用setTimeout() 之后我的文本框是否获得焦点。
@ViewChild('searchInput') searchInput: ElementRef;
function A(show) {
const self = this;
if (show) {
this.xyz= true;
setTimeout(function () {
self.searchInput.nativeElement.focus();
}, 0);
} else {
self.xyz= false;
self.abc = '';
}
}
这是我正在尝试的测试用例:
it('textbox get focus toggleSearch', async(() => {
let el: DebugElement;
component.toggleSearch(true);
el = fixture.debugElement.query(By.css('#search-input-theme'));
let native: HTMLElement = el.nativeElement;
spyOn(native,'focus');
fixture.whenStable().then(() => {
expect(native.focus).toHaveBeenCalled();
});
}));
【问题讨论】: