因此,我一直在为Angular应用程序进行单元测试。我正在使用Angular Material,并且我有一个使用拖放CDK cdk drag-drop API的组件。
html代码如下所示
<mat-card class="interventionCard">
<div cdkDropListGroup class="container">
<div cdkDropList
[cdkDropListData]="interventionsTodo" (cdkDropListDropped)="drop($event)" class="dragContainer">
<div class="dragInter" *ngFor="let i of interventionsTodo" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
<a class="resetInv" (click)="reset()"><div class="dragInter"><img src="../assets/images/simulation-intervention/noIntervention.svg" class="interventionIc" alt=""></div></a><!---->
</div>
<div class="dropContainer">
<div cdkDropList [cdkDropListData]="interventionsDoneNow" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDoneNow" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus5" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus5" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc"></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus10" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus10" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus15" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus15" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus20" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus20" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus25" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus25" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
<div class="connectorL"></div>
<div cdkDropList [cdkDropListData]="interventionsDonePlus30" (cdkDropListDropped)="drop($event)" class="dropZone">
<div class="dragInterDropped" *ngFor="let i of interventionsDonePlus30" cdkDrag><img src="{{i.imgSrc}}" class="interventionIc" alt=""></div>
</div>
</div>
很好,现在我有cdkDropListDroppet事件,该事件调用函数drop()
drop(event: CdkDragDrop<{ imgSrc: string }[], any>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
if(event.container.data === this.interventionsTodo){
this.deleteSpecificItemFromTimeline(event.previousContainer.data[0]);
}else if(event.container.data.length <1){
copyArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}
}
}
这很好用,现在可以对角度应用程序(茉莉花/业力)进行单元测试
并且我正在尝试调度CdkDragDrop事件,以便我可以验证我的drop()
函数的功能。
因此,在角度文档中有一小章涉及组件DOM测试,其中提到必须在要触发事件的位置获取htmlElement,我这样做是这样的:angular docs
let Dbg = fixture.debugElement;
let htE = Dbg.nativeElement.querySelector(".dragContainer");
现在我具有要在其上触发事件的元素,因此完整的代码如下所示
let Dbg = fixture.debugElement;
let htE = Dbg.nativeElement.querySelector(".dragContainer");
htE.addEventListener('CdkDragDrop', (ev:any) => {
component.drop(ev);
});
let bla = htE.dispatchEvent(new Event('cdkDropListDropped'));
所以bla返回true,这表示(如果我没记错的话)表示事件已被触发,但似乎什么也没发生
fixture.detectchanges()
自动发生。
我试图调试它以查看发生了什么,但是listener函数从未被触发,我四处寻找找到答案,但似乎无法在这种事件测试中找到任何东西。
答案 0 :(得分:0)
您可以尝试使用一些模拟事件来测试def __init__(self,list_names,list_values):
[setattr(self,name,value) for name,value in zip(list_names,list_values)]
方法,而不用尝试从模板中触发事件。我这样做是通过使用通用的DragDropEventFactory。
summary-table-side-bar.component.spec.ts说明了相应的单元测试的外观。