我想TestUtils.Simulate.mouseMove
在document
. 我有Dragger
一个mouseMove
向document
. 这是一个不完整的版本:
// Dragger.js
'use strict';
var React = require('react');
export default React.createClass({
propTypes: {
handleDrag: React.PropTypes.func // callback set by parent
},
getInitialState: function() {
return {dragging: false}
},
componentDidUpdate: function(props, state) {
//
if (this.state.dragging && !state.dragging) {
document.addEventListener('mousemove', this.onMouseMove)
} else if (!this.state.dragging && state.dragging) {
document.removeEventListener('mousemove', this.onMouseMove)
}
},
onMouseDown: function(e) {
this.setState({dragging: true})
},
onMouseMove: function(e) {
// Calls back to the parent with the drag
this.props.handleDrag(e);
},
render: function() {
return <div onMouseDown={this.onMouseDown} ></div>
}
});
我正在使用jasmine,我想确保handleDrag
在 a 之后调用我的回调,mouseDown
然后是 a mouseMove
。
// Dragger.spec.js
var React = require('react/addons');
import Dragger from './Dragger';
var TestUtils = React.addons.TestUtils;
describe('Dragger', function() {
it('should call the callback after drag interaction', function() {
// make callback to spy on
var f = {callback: function(e){return}};
// render Dragger
var dragger = TestUtils.renderIntoDocument(<Dragger handleDrag={f.callback} />);
// spy on callback
spyOn(f, 'callback');
// simulate a mouseDown and mouseMove
TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0});
TestUtils.Simulate.mouseMove(document);
expect(f.callback).toHaveBeenCalled(); // FAILS!
}
}
但是该mouseMove
事件没有被正确模拟。我看到 2 个问题
- 我可能需要将事件数据传递给
TestUtils.Simulate.mouseMove
. 例如,在TestUtils.Simulate.mouseDown(dragger.getDOMNode())
我将其更改为TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0})
. 我应该传递什么事件数据TestUtils.Simulate.mouseMove
? document
不是将测试组件渲染到的分离 DOM的一部分。这可能是Simulate.mouseMove
不起作用的另一个原因。我可以在测试中使用什么来代替document
?
我该如何使用TestUtils.Simulate.mouseMove
?