如何在角度2测试中测试下拉切换操作?

San*_*nju 2 unit-testing typescript karma-jasmine ngx-bootstrap angular

我正在尝试测试下拉切换功能。但是我无法使它正常工作。我也已导入BsDropdownModule到规格文件。注意:我正在使用ngx-bootstrap。

这是我尝试过的:

HTML:

<div class="btnBox" dropdown placement="bottom right">
    <button class="btnIcon dropdown-toggle" dropdownToggle>
    </button>
    <ul class="btnOptionBox dropdown-menu dropdown-menu-right" *dropdownMenu>
       <li class="iconBtn" (click)="someFun()" type="button"><span>Edit</span></li>
       <li class="iconBtn" (click)="someFun1()" type="button"><span>Delete</span></li>
    </ul>
 </div>
Run Code Online (Sandbox Code Playgroud)

测试规格:

it("Should show options when toggle option is clicked",() => {
     fixture.detectChanges();
     let toggleButton = fixture.debugElement.queryAll(By.css('[dropdownToggle]'));
     toggleButton[0].nativeElement.click();
     fixture.detectChanges();
     /*Unable to access li tag directly. That's why I have used it's parent*/
     let list = fixture.debugElement.queryAll(By.css('div.ellipsisBox'));  
     console.log(list[0]); /*Shows the list in the children array*/
     console.log(list[0].children);  /*Doesn't show the list*/
});
Run Code Online (Sandbox Code Playgroud)

有人可以建议正确的方法吗?

San*_*nju 5

我设法解决了这个问题。这只是一个愚蠢的错误。列表中的数据由异步过程填充。因此,我应该使用它fakeAsynctick()然后单击下拉按钮。在将数据填充到列表之前,已更新视图。造成了问题。这是固定代码:

it("Should show options when toggle option is clicked",fakeAsync(() => {
     fixture.detectChanges();
     let toggleButton = fixture.debugElement.queryAll(By.css('[dropdownToggle]'));
     toggleButton[0].nativeElement.click();
     tick();
     fixture.detectChanges();
     let list = fixture.debugElement.queryAll(By.css('ul.btnOptionBox'));  
     console.log(list[0]);
}));
Run Code Online (Sandbox Code Playgroud)