我想用TestUtils.Simulate.mouseMove
on the 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 http://jasmine.github.io/,我想确保我的handleDrag
回调被调用后mouseDown
随后是一个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!
}
}
But the mouseMove
事件没有被正确模拟。我看到 2 个问题
- 我可能需要将事件数据传递给
TestUtils.Simulate.mouseMove
。例如,调用TestUtils.Simulate.mouseDown(dragger.getDOMNode())
did not工作直到我将其更改为TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0})
。我应该传递什么事件数据TestUtils.Simulate.mouseMove
?
- The
document
不属于分离的 DOM https://facebook.github.io/react/docs/test-utils.html#renderintodocument测试组件被渲染到。这可能是另一个原因Simulate.mouseMove
不起作用。我可以在测试中使用什么来代替document
?
我该如何使用TestUtils.Simulate.mouseMove
?