मैं एक आवरण घटक पर काम कर रहा हूँ का उपयोग कर प्रतिक्रिया में उदाहरण के तरीकों पर घटक राज्य और जासूसी प्रतिक्रिया करने के लिए। मैं इकाई के लिए मोचा, चाय और सिनोन साथ एंजाइम का उपयोग अपने घटक का परीक्षण करें। परीक्षण यहाँ में, मैं परीक्षण करने के लिए कि कोशिश कर रहा हूँ:परीक्षण परिवर्तन छवियों को सुचारू रूप से लोड करने के लिए एंजाइम
घटक के राज्य जब छवि
लोड होने केघटक पर
onLoad
उदाहरण विधि बुलाया गया था अद्यतन किया जाता है
const wrapper = shallow(); const onLoad = wrapper.find('img').props().onLoad; const onLoadSpy = sinon.spy(onLoad); wrapper.update(); const status = wrapper.state().status; expect(onLoadSpy).to.have.been.called; expect(status).to.equal('LOADED');
मुझे लगता है कि न तो राज्य के लिए अद्यतन एंजाइम या onLoad
जासूस की कॉल गिनती से परिलक्षित होता है अद्यतन किया जाता है।
export default class Image extends Component {
constructor(props) {
super(props);
if (props.src != null && typeof props.src === 'string') {
this.state = {
status: LOADING,
};
} else {
this.state = {
status: PENDING,
};
}
this.onLoad = this.onLoad.bind(this);
}
onLoad() {
this.setState({
status: LOADED,
});
}
render() {
//lots of code above the part we care about
const defaultImageStyle = style({
opacity: 0,
transisition: 'opacity 150ms ease',
});
const loadedImageStyle = style({
opacity: 1,
});
let imageStyle = defaultImageStyle;
if (this.state.status === LOADED) {
imageStyle = merge(defaultImageStyle, loadedImageStyle);
} else {
imageStyle = defaultImageStyle;
}
let image;
if (alt != null) {
image = (<img
className={imageStyle}
src={src}
width={width}
height={height}
alt={alt}
onLoad={this.onLoad}
/>);
} else {
image = (<img
className={imageStyle}
src={src}
width={width}
height={height}
role="presentation"
onLoad={this.onLoad}
/>);
}
let statusIndicator = null;
if (this.state.status === LOADING) {
statusIndicator = (<div className={loadingStyle}></div>);
}
return (<div className={wrapperStyle}>
{statusIndicator}
{image}
</div>);
}}
बेहतर संदर्भ के लिए पूर्ण कोड पर एक नज़र लेने के लिए::
बहुत धन्यवाद आप एक मणि हैं: डी – danday74