加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在测试react-dnd时存根显示器?

发布时间:2020-12-15 16:16:24 所属栏目:百科 来源:网络整理
导读:我正在尝试测试我的react-dnd的实现,并且在我的一个drop函数中,我正在使用monitor.getInitialClientOffset()函数来获取偏移量,并且我想将此方法存根以返回特定的偏移量然后我可以断言,但我无法弄清楚这一点.在我的测试中,我正在使用 const WrappedContext =
我正在尝试测试我的react-dnd的实现,并且在我的一个drop函数中,我正在使用monitor.getInitialClientOffset()函数来获取偏移量,并且我想将此方法存根以返回特定的偏移量然后我可以断言,但我无法弄清楚这一点.在我的测试中,我正在使用

const WrappedContext = wrapInTestContext(ContextArea);
const page = mount(<WrappedContext />);
const manager = page.get(0).getManager();
const backend = manager.getBackend();

// Couple finds to get the right source and target ids

backend.simulateBeginDrag([sourceId])
backend.simulateHover([targetId])
backend.simulateDrop();
backend.simulateEndDrag();

(这是使用https://gaearon.github.io/react-dnd/docs-testing.html的标准wrapInTestContext)

drop函数从测试后端传递一个监视器,我没有在文档中看到将存根版本传递给任何模拟方法的方法.

解决方法

事实证明,您可以访问测试后端正在使用的监视器,然后在其上存根方法,如下所示:

const manager = page.get(0).getManager();
  const backend = manager.getBackend();
  const monitor = manager.getMonitor();

  sinon.stub(monitor,'getInitialClientOffset',() => {
    return {
      x: 10,y: 20,};
  });

  sinon.stub(monitor,'getDifferenceFromInitialOffset',() => {
    return {
      x: 2,y: 4,};
  });

然后在drop函数中,这些值将用于您正在使用的任何类型的数学中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读