我是茉莉花的新手,正在尝试为自动完成组件的键盘事件编写一些测试用例。以下是详细信息:
功能:
@HostListener('document:click', ['$event', '$event.target'])
public onClick(event: MouseEvent, targetElement: HTMLElement): void {
if (!targetElement) {
return;
}
const clickedInside = this._elementRef.nativeElement.contains(targetElement);
if (!clickedInside) {
this.arrowkeylocation= -1;
this.clickOutside.emit(event);
}
}
以上功能检查是否有人单击了外部文本字段,然后关闭了自动完成下拉菜单
下面的测试用例正在工作,其中inputEl是文本元素:
it('should call clickOutside.emit not called', () => {
spyOn(component.clickOutside, 'emit');
inputEl.triggerEventHandler('keydown',null);
expect(component.clickOutside.emit).not.toHaveBeenCalled();
});
以下测试用例无法正常工作。这里的commitEl是搜索按钮
it('should call clickOutside.emit not called', () => {
spyOn(component.clickOutside, 'emit');
submitEl.triggerEventHandler('keydown',null);
expect(component.clickOutside.emit).toHaveBeenCalled();
});