【发布时间】:2015-03-15 07:18:39
【问题描述】:
有什么方法可以触发img 标签的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 />);
});
【问题讨论】:
标签: javascript reactjs jestjs