为 CdkDragDrop 事件创建虚拟负载

Create dummy payload for CdkDragDrop event

我想对方法进行单元测试,在 angular+jest:

  drop(event: CdkDragDrop<string[]>) {
    if (event.previousContainer === event.container) {
      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
    } else {
      let clonedData: any = JSON.parse(JSON.stringify(event.item.data));
      clonedData.paramId = this.IDGenerator() + '_' + event.currentIndex;
      this.formCtrls.splice(event.currentIndex, 0, clonedData)
    }
    this.selectedControl = this.formCtrls[event.currentIndex];
  }

是否可以为 CdkDragDrop<string[]> 创建一个虚拟有效载荷,以便我可以使用给定的有效载荷调用此方法。

您可以使用 Type Assertions 将您的输入参数转换为您想要的任何内容,例如:

const myEvent = {'foo': 'bar'} as CdkDragDrop<string[]>;
myComp.drop(myEvent);

这样做,您的测试应该可以编译。