测试react-dnd时如何在监视器上存根?

Ben*_*are 5 testing reactjs react-dnd

我正在尝试测试我的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();
Run Code Online (Sandbox Code Playgroud)

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

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

Ben*_*are 5

因此,事实证明您可以访问测试后端正在使用的监视器,然后在其上添加如下方法:

  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,
    };
  });
Run Code Online (Sandbox Code Playgroud)

然后在放置函数中,这些是将在您使用的任何数学运算中使用的值。