我想在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>
}
});我使用的是茉莉,我希望确保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!
}
}但是mouseMove事件没有被正确地模拟。我看到了两个问题
TestUtils.Simulate.mouseMove。例如,调用TestUtils.Simulate.mouseDown(dragger.getDOMNode())使不工作,直到我将它更改为TestUtils.Simulate.mouseDown(dragger.getDOMNode(), {button: 0})。应该将哪些事件数据传递给TestUtils.Simulate.mouseMovedocument不是将测试组件呈现到的分离DOM的一部分。这可能是Simulate.mouseMove不能工作的另一个原因。在测试中我可以用什么代替document?如何使用TestUtils.Simulate.mouseMove?
发布于 2016-09-07 17:08:41
在用酶法和react的TestUtils尝试了几个小时之后,我终于找到了在纯JS中创建和分派事件的方法,它在我的jest测试中工作如下
it('calls handler on mouseDown on element, mouseMove on document', () => {
const handler = jest.fn();
const props = {
foo: {
uid: '1',
resizable: true,
},
resizeHandler,
};
const instance = mount(<Header {...props} />);
const resizer = instance.find('.resizer');
const top = window.document.documentElement; // target the documentElement
resizer.simulate('mouseDown', { preventDefault: () => true }); // uses enzyme to simulate this event, adding listener to documentElement on mousemove
const mouseMove = new Event('mousemove'); // creates a new event
top.dispatchEvent(mouseMove); // dispatches it
const mouseUp = new Event('mouseup');
top.dispatchEvent(mouseUp);
expect(resizeHandler).toBeCalled(); // the passed in handler is called on mousemove
});基本上,您可以找到带有document.documentElement的window.document.documentElement,并像任何其他element一样从它中分派事件。
发布于 2016-08-05 17:16:41
这是一个旧的帖子,但我看到还没有张贴的解决方案,我遇到它,因为我正在写一个类似的组件。我认为问题在于,您关注的是错误的事件,您应该使用onDrag来拖动检测,下面是适合我的代码的改编版本:
// Dragger.js
import React from 'react';
export default React.createClass({
propTypes: {
handleDrag: React.PropTypes.func // callback set by parent
},
getInitialState: function() {
return {dragging: false}
},
onDragStart: function(e) {
// Calls back to the parent with the drag
this.setState({ dragging: true });
this.props.handleDrag(e);
},
onDragEnd: function() {
this.setState({ dragging: false });
},
render: function() {
return <div onDragStart={this.onDragStart} onDragEnd={this.onDragEnd}></div>;
}
});和
// Dragger.spec.js
import React from 'react';
import Dragger from '../src/Dragger';
import {
renderIntoDocument,
scryRenderedDOMComponentsWithTag,
Simulate
} from 'react-addons-test-utils';
import { expect } from 'chai';
describe('Dragger', function() {
it('should call the callback after drag interaction', function() {
// spy on callback
var called = false;
// make callback to spy on
function callback() {
called = true;
};
// render Dragger
var dragger = renderIntoDocument(<Dragger handleDrag={callback} />);
// simulate a dragStart and dragEnd
const element = scryRenderedDOMComponentsWithTag(dragger, 'div')[0];
Simulate.dragStart(element);
Simulate.dragEnd(element);
expect(called).to.equal(true);
});
});https://stackoverflow.com/questions/31055738
复制相似问题