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 {
.....
checkSize(src, width, height){
let imageObj = new Image();
imageObj.onload = (evt) => {
return (evt.target.width >= width && evt.target.height >= height)
? true : false;
}
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.checkSize(img300_300,200,200)).toEqual(true);
expect(image.checkSize(img300_300,300,300)).toEqual(true);
expect(image.checkSize(img300_300,300,200)).toEqual(false);
expect(image.checkSize(img300_300,200,300)).toEqual(false);
expect(image.checkSize(img300_300,400,400)).toEqual(false);
});
After Running the test, I got this error :
Expected value to equal: true Received: undefined
I knew the problem is 'onload' is a callback after it got the image.
The question is:
How can I test this without using Promise like this
Do I misunderstand something? Or all I have to do is using promise?
thank you.
Aucun commentaire:
Enregistrer un commentaire