[Edited] : I have change my code with promise way .
I am writing react with this starter created by facebook, and I'm a newbie about testing.
Now I have a component about image, it has a function to check Image size:
import React, { Component } from 'react';
class ImagePart extends Component {
.....
// check size.
checkSize(src, width, height){
this.loadImg(src)
.then((obj) => {
return (obj.width >= width && obj.height >= height)
? true : false;
})
.catch((msg)=> {
dosomething
});
}
// load image and return a promise.
loadImg(src){
return new Promise((resolve, reject) => {
let imageObj = new Image();
imageObj.onload = (evt) => {
resolve(evt.target);
}
imageObj.error = (err) =>{
reject(err);
}
imageObj.src = src;
})
}
.....
}
And test snippet:
import React from 'react';
import ReactDOM from 'react-dom';
import ImagePart from './ImagePart';
it('checking image size without error', () => {
const image = new ImagePart();
const img300_300 = 'https://someImage.png';
expect(image.loadImg(img300_300).width).resolves.toBe(300);
// ??? test checkSize
});
After Running the test, I got this error :
TypeError: Cannot read property 'toBe' of undefined
The question is:
- How can I test `loadImg in right way ?
- what's the general pattern to test checkSize ?
thank you.