I'm trying to mock a custom function with jest but I'm having problems with it.
This is my function:
export const resizeImage = (file, fileName, callback) => {
const MAX_WIDTH = avatarImage.maxWidth;
const MAX_HEIGHT = avatarImage.maxHeight;
const img = document.createElement('img');
img.src = window.URL.createObjectURL(file);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
img.onload = () => {
const sizes = internalResizeImage(img, MAX_WIDTH, MAX_HEIGHT);
canvas.width = sizes.width;
canvas.height = sizes.height;
ctx.drawImage(img, 0, 0, sizes.width, sizes.height);
return callback(dataURItoFile(canvas.toDataURL(), fileName));
};
};
I called like this:
resizeImage(acceptedFiles[0], this.props.user.id, (res) => {
//dostuff
});
In my test I'm mocking it like this:
let mockResizeImage = jest.fn();
jest.mock('../../utilities/imageUtils', () => ({
resizeImage: () => mockResizeImage
}));
I want mockResizeImage to be a callback and then in my test change the returning values:
it('should call handleDrop and accept files', () => {
//mockResizeImage.mockReturnValue('something');
const instance = shallow(mockComponent()).instance();
const acceptFilesMock = ['test'];
instance.handleDrop(acceptFilesMock);
expect(clickSpy).toHaveBeenCalledTimes(1);
});
If it were a promise, all good, but it's a callback and I don't know what I'm doing wrong.
Thanks.