有没有办法触发标签的
error
事件,以测试我的onError
回调函数?例如,给定这个组件,
import React from 'react';
/**
* Self removing <img> when the `src` or image
* does not load or is unavailable.
*
* Usage: (tip: it's how you use the <img> tag, basically)
* <Img src={} alt={} ../..>
*/
var Img = React.createClass({
/**
* Force update so `refs` will be available
*/
componentDidMount() {
this.forceUpdate();
},
render() {
// Omit any passed `onError` prop so that
// it is never overridden by mistake
var { onError, ...other } = this.props;
return (
<span ref="container">
<img {...other} onError={this._handleError} />
</span>
)
},
/**
* Remove itself when image is not found
*/
_handleError() {
this.refs.container.getDOMNode().remove();
}
});
export default Img;
以及这个测试套件:
it('should remove itself when an error occurs while loading the image', function() {
// rendered = TestUtils.renderIntoDocument(<Img />);
});