[英]How can I write Unit Tests for this code snippet in Angular for @HostListener('document:click',[$event])
Look into the code and provide an clear explanation.查看代码并提供清晰的解释。
@HostListener('document:click',[$event])
clickout(event){
if(event.target.className!=='some blah some blah'){
if(this.dropdownlist && this.dropdownlist.nativeElement){
if(!this.dropdownlist.nativeElement.contains(event.target)){
this.somehandlingdropdown(true);
}
}
}
}
Welcome to SO.欢迎来到 SO。
You can create event in your test:您可以在测试中创建事件:
const event = new KeyboardEvent('keydown', {
key: 'ArrowDown',
altKey: true
});
document.dispatchEvent(event);
fixture.detectChanges();
You are trying to click dropdown list and check target value which has className attribute value to be 'some string value' and its equivalent native element value您正在尝试单击下拉列表并检查具有 className 属性值为“某个字符串值”的目标值及其等效的本机元素值
Something like:就像是:
it('should test the clickout function', () => {
const event = { target: { className: 'some blah some bah'} };
component.clickout(event);
expect(component.somehandlingdropdown).toBeFalsy(); <-- what ever
your set value here check toBeTrue or toBeFalsy
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.