我想测试使用D3的组件,该组件报告mouseMove
事件的主管相对坐标。这个组件在浏览器中按预期工作 - 我有一个简单的回调记录坐标和一个'鼠标移动'事件也被记录:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { mouse, select } from 'd3-selection';
class Overlay extends Component {
_handleMouseMove(coordinates, callback) {
console.log('Mouse moved');
callback(coordinates);
}
componentDidMount() {
this.renderD3Move();
}
componentDidUpdate() {
this.renderD3Move();
}
renderD3Move() {
const handleMouseMove = this._handleMouseMove;
const callback = this.props.callback;
const node = ReactDOM.findDOMNode(this);
select(node).on('mousemove', function handleMouse() {
handleMouseMove(mouse(node), callback);
});
}
render() {
return (
<rect
className="overlay"
height={this.props.height}
width={this.props.width}
/>
);
}
}
export default Overlay;
尽管如此,我还是无法编写一个触发回调的测试,甚至无法将鼠标移到&#39;要记录的消息:
import chai from 'chai';
chai.should();
import React from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';
import Overlay from '.';
describe('<Overlay />', function() {
var height = 50,
width = 50;
it('passes mouse coordinates when mouse moves', function() {
const callback = sinon.spy();
var wrapper = mount(
<Overlay height={height} width={width} callback={callback} />
);
wrapper.find('Overlay').simulate('mousemove');
wrapper.find('rect').simulate('mousemove');
wrapper.find('Overlay').simulate('mouseMove');
wrapper.find('rect').simulate('mouseMove');
callback.called.should.equal(true);
});
}
我的问题是:
相关问题并未完全解决我的问题