【问题标题】:Create dummy payload for CdkDragDrop event为 CdkDragDrop 事件创建虚拟负载
【发布时间】:2022-01-06 21:18:54
【问题描述】:

我想用 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&lt;string[]&gt; 创建一个虚拟负载,以便我可以使用给定的负载调用此方法。

【问题讨论】:

  • 你总是可以在你的测试中转换你的参数,例如myComp.drop('foo' as CdkDragDrop&lt;string[]&gt;).
  • @DonJuwe,您能否详细说明一下,如何做到这一点。感谢您的帮助

标签: angular jestjs jasmine


【解决方案1】:

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

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

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

【讨论】:

  • 当然,会试试的,谢谢
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多