作者:renminxilu662 | 来源:互联网 | 2023-02-01 23:02
我正在阅读单元测试时遇到一些困难,这个单元测试正在读取一些用*ngIf从DOM中添加/删除的html.
这是DOM:
Expandable
这是在第一个div的click事件中调用的组件函数:
private showDet() {
console.log('show called');
this.showHideDet = !this.showHideDet;
}
最后这是规范:
it('should show the header when the expandable elem is clicked', async(() =>
{
const fixture = TestBed.createComponent(DetailsComponent);
const compiled = fixture.debugElement.nativeElement;
let detPresent: boolean = false;
for (let node of compiled.querySelectorAll('.detailacc')) {
node.click();
}
setTimeout(() => {
console.log(compiled.querySelectorAll('.detailHeader'));
for (let node of compiled.querySelectorAll('.detailHeader')) {
if (node.textCOntent=== 'Details header') {
detPresent = true;
break;
}
}
expect(detPresent).toBeFalsy();
}, 0);
}));
正如你所看到的那样,我已经包装了代码,这些代码搜索那些仅在setTimeout中*ngIf为真时显示的DOM元素,如下所示如何检查ngIf是否生效但我仍然没有得到任何结果.
此外,如果您想知道是否在该测试中调用了click,那么因为组件函数中的console.log在Karma控制台中显示.setTimeout中的console.log显示了一个NodeList(0),基本上没有找到类.detailHeader的节点.