如何在测试 react-dnd 时存根监视器?
How do I stub the monitor while testing react-dnd?
我正在尝试测试我的 react-dnd 实现,在我的一个 drop 函数中,我正在使用 monitor.getInitialClientOffset()
函数来获取偏移量,我想存根这个方法到 return 我可以断言的特定偏移量,但我无法弄清楚。在我的测试中,我使用
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 函数中,这些值将用于您正在使用的任何类型的数学运算。
我正在尝试测试我的 react-dnd 实现,在我的一个 drop 函数中,我正在使用 monitor.getInitialClientOffset()
函数来获取偏移量,我想存根这个方法到 return 我可以断言的特定偏移量,但我无法弄清楚。在我的测试中,我使用
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 函数中,这些值将用于您正在使用的任何类型的数学运算。