gpt4 book ai didi

angular - 如何检查我的元素是否已在 Angular 4 的单元测试中聚焦?

转载 作者:太空狗 更新时间:2023-10-29 17:25:07 25 4
gpt4 key购买 nike

我有以下单元测试功能。我在组件中采用了带有 View 子项的文本框元素,在测试中我需要测试在调用 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();
});
}));

最佳答案

也许是这样的:

const input = de.query(By.css("your_field_selector")).nativeElement;
const focusElement = de.query(By.css(":focus")).nativeElement;
expect(focusElement).toBe(input);

路易斯

关于angular - 如何检查我的元素是否已在 Angular 4 的单元测试中聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44696018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com