当前位置 : 主页 > 网页制作 > React >

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

来源:互联网 收集:自由互联 发布时间:2021-06-15
我正在尝试测试我的react-dnd的实现,并且在我的一个drop函数中,我正在使用monitor.getInitialClientOffset()函数来获取偏移量,并且我想将此方法存根以返回特定的偏移量然后我可以断言,但我无法
我正在尝试测试我的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函数中,这些值将用于您正在使用的任何类型的数学中.

网友评论