angular - 如何在 Angular 2 测试中测试下拉切换操作?

标签 angular unit-testing typescript karma-jasmine ngx-bootstrap

我正在尝试测试下拉切换功能。但我无法让它发挥作用。我还将 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>

测试规范:

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*/
});

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

最佳答案

我已经设法解决了这个问题。这只是一个愚蠢的错误。列表中的数据由一个异步进程填充。所以我应该在单击下拉按钮后使用 fakeAsynctick() 。在数据填充到列表之前, View 已更新。这导致了问题。这是固定代码:

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]);
}));

关于angular - 如何在 Angular 2 测试中测试下拉切换操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46712932/

相关文章:

angular - 预期空格缩进 - Codacy

unit-testing - 如何测试以确保函数被调用?

javascript - Nightwatch - 使用执行来填充表格数据单元格

html - 我无法为 Material 菜单提供样式类(Angular 2+)

css - 分布式中的 Angular 6 多个 css 文件

javascript - 如何正确使用路由器 socket

c# - 无法在 NUnit 中加载程序集

c# - 集成测试只是使用更少模拟的单元测试吗?

angular - 如何从 Angular - ng2 文件上传打开输入类型 ="file"对话框

Angular 5 : Module not found: Error: Can't resolve '@angular/forms/src/validators'