Testing for mouse wheel events

心不动则不痛 提交于 2019-12-22 04:56:06

问题


I've set up a simple function to handle mouse wheel events on a menu component I built. The component works fine, I'm trying to write a unit test around it and that is giving me an issue.

component handler:

handleWheel: function (event) {
        (event.deltaY < 0 ) ? this.moveUp() : this.moveDown();

        return false;
}

this.moveUp() / this.moveDown() simply sets the state of firstIndex

The issue is that when I try to write a test for this functionality I can't get it to work. I'm almost 100% certain it has to do with the eventDetails object i'm passing in, but I don't know how to format it correctly.

// set firstIndex = 0
TestUtils.Simulate.scroll(menu, {deltaY: 52});
expect(menu.state.firstIndex).to.equal(1);
// error: expected 0 to be 1

Does anyone know how to correctly format the TestUtils.Simulate.Scroll() / know of a better way to test onWheel() ?


回答1:


If the event handler is for onWheel you should use Simulate.wheel.

There's a 1:1 mapping of events to Simulate methods. Remove the "on" and lowercase the first letter.

onScroll   ->   Simulate.scroll
onKeyDown  ->   Simulate.keyDown
onWheel    ->   Simulate.wheel


来源:https://stackoverflow.com/questions/26717499/testing-for-mouse-wheel-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!