ngx引导引导菜单未在Angular4+;中打开;单元测试

ngx引导引导菜单未在Angular4+;中打开;单元测试,angular,drop-down-menu,ngx-bootstrap,Angular,Drop Down Menu,Ngx Bootstrap,下拉菜单在应用程序中打开良好,但在单元测试中不起作用。我不确定我是否错过了什么,但我已经看了很长时间,我没有发现任何用户错误。我还在github上提交了一个问题。 这里是stackblitz链接: 运行应用程序 //bootstrap();//因果报应 platformBrowserDynamic().bootstrapModule(AppModule);//运行应用程序 因果报应 bootstrap();//因果报应 //platformBrowserDynamic().bootstrap

下拉菜单在应用程序中打开良好,但在单元测试中不起作用。我不确定我是否错过了什么,但我已经看了很长时间,我没有发现任何用户错误。我还在github上提交了一个问题。

这里是stackblitz链接:

运行应用程序

//bootstrap();//因果报应
platformBrowserDynamic().bootstrapModule(AppModule);//运行应用程序
因果报应

bootstrap();//因果报应
//platformBrowserDynamic().bootstrapModule(AppModule);//运行应用程序

有人能确认这是错误还是用户错误吗?提前感谢

我认为您遇到了一些问题,因为事件循环是如何在DOM节点上手动执行click方法的

Jake Archibald(谷歌的开发者代言人)在2018年JSConf亚洲大会上就事件循环做了精彩的演讲。我强烈建议你观看整件事,但我相信你正在看。本质上,由于您是手动触发这个click事件,因此需要在事件循环中作为单独的任务执行测试断言

您可以在测试中使用
setTimeout(…)
(并使用
done
方法)或在角度测试中使用
fixture.whenStable()方法的首选方法来完成此操作。更改此选项将允许您的测试通过:

fit('应打开下拉菜单',()=>{
常量h2:HTMLElement=fixture.nativeElement.querySelector('button#button basic');
h2.单击();
fixture.whenStable()然后(()=>{
fixture.detectChanges();
expect(fixture.nativeElement.queryselectoral('li a.dropdown-item').length).toEqual(4);
});
});

我无法让您的Stackblitz正常工作,但为了防止其他人遇到此问题,如果您已动态生成列表项(使用ngFor),则需要在勾号()后添加另一个detectChanges()

 it('should show 8 items (fakeasync)', fakeAsync(() => {
    const h2: HTMLElement = fixture.nativeElement.querySelector('button#button-basic');
    h2.click();
    fixture.detectChanges();
    tick(); 
    fixture.detectChanges();
 expect(fixture.nativeElement.querySelector('[dropdown]').classList).toContain('open');
    expect(fixture.nativeElement.querySelectorAll('li a.dropdown-item').length).toEqual(8)
  }));