I am writing a jest unit test to display error message when user click file upload button when image width and height ratio is 3 :1.
Here is method I used to check image ratio:
export const isValidRatio = async (file: File) => {
return createImageBitmap(file).then((bitmap) => {
return bitmap.width / bitmap.height === 3;
});
};
In the Jest , I have following way to mock File
const blob = new Blob(['test blog'], { type: 'image/png' });
const testFile = new File([blob], 'testFile01.jpeg', { type: 'image/jpeg' });
It works well to test if file size above certain limitation, however, I have been struggle a lot to test width & height ratio.
When I run the test , I got this error:
ReferenceError: createImageBitmap is not defined
I am thinking to use jest.spy to mock this function, but do not know how exactly to do it.
Does anyone know what is a good approach to mock an image width & height?