我如何在Angular中为@HostListener('document:click',[$ event])

问题描述 投票:0回答:2

查看代码并提供清晰的说明。

        @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);
                }
             }
           }
         }
angular unit-testing document
2个回答
0
投票

欢迎您加入。

您可以在测试中创建事件:

const event = new KeyboardEvent('keydown', {
   key: 'ArrowDown',
   altKey: true
});

document.dispatchEvent(event);
fixture.detectChanges();

0
投票

类似:

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
   });
© www.soinside.com 2019 - 2024. All rights reserved.