我正在尝试测试我的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函数中,这些值将用于您正在使用的任何类型的数学中.