我正在尝试用笑话来模拟自定义函数,但我遇到了问题。
这是我的功能:
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));
};
};
我这样调用:
resizeImage(acceptedFiles[0], this.props.user.id, (res) => {
//dostuff
});
在我的测试中,我这样嘲笑它:
let mockResizeImage = jest.fn();
jest.mock('../../utilities/imageUtils', () => ({
resizeImage: () => mockResizeImage
}));
我希望 mockResizeImage 成为回调,然后在我的测试中更改返回值:
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);
});
如果这是一个承诺,那一切都很好,但这是一个回调,我不知道我做错了什么。
Thanks.