reactjs - 如何在 FileReader.onload 中使用 toHaveBeenCalled()
问题描述
我有加载图像并通过 props.handle 函数将图像数据发送到父组件的组件我如何在 fileReader.onload 中模拟或调用 props.handle 也许需要使用异步,但我不知道如何。
尝试重复这个问题的代码How do I test `image.onload` using jest in the context of redux actions(或其他在操作中分配的回调) 但它没有帮助
ChildComponent.js:
class ChildComponent extends React.PureComponent {
constructor(props) {
super(props);
}
handleFileLoad = event => {
event.preventDefault();
const reader = new FileReader();
reader.onload = () => {
const data = reader.result;
this.props.parentHandleFunction(data)
}
reader.readAsDataURL(event.target.files[0]);
}
}
ChildComponent.propTypes = {
parentHandleFunction: PropTypes.func.isRequired,
};
ChildComponent.test.js:
describe('<ChildComponent />', () => {
let renderComponent;
let changeSpy;
beforeEach(() => {
changeSpy = jest.fn(value => value);
renderComponent = shallow(
<ChildComponent parentHandleFunction={changeSpy}/>,
);
});
it('should call handle file change', () => {
const childComponent = shallow(
renderComponent.find(ChildComponent).getElement(),
);
const file = new Blob(['image'], { type: 'image/jpeg' });
loadButton.find('input').simulate('change', {
preventDefault: () => {},
target: {
files: [file],
},
});
expect(changeSpy).toHaveBeenCalled();
});
})
测试显示错误:“预期的模拟函数已被调用,但未被调用。”
更新
我通过在单独的函数中削减 onload 逻辑来解决我的问题
## ChildComponent.js ##
class ChildComponent extends React.PureComponent {
constructor(props) {
super(props);
}
loadImage = data => {
const imageObject = {
url: data,
};
this.props.parentHandleFunction(
imageObject,
);
}
handleFileLoad = event => {
event.preventDefault();
const reader = new FileReader();
reader.onload = async () => this.loadImage(reader.result);
reader.readAsDataURL(event.target.files[0]);
}
}
ChildComponent.propTypes = {
parentHandleFunction: PropTypes.func.isRequired,
};
ChildComponent.test.js:
it('should call change spy function', () => {
renderComponent.instance().loadImage('mockImage');
renderComponent.update();
renderComponent.instance().forceUpdate();
expect(changeSpy).toHaveBeenCalled();
});
解决方案
推荐阅读
- vb.net - VB.net 单元格值返回 DBNull
- ansible - json_query 过滤列表中具有值的字典列表
- laravel - Google Authentification Laravel Socialite 在询问“个人资料”范围时给出 403 错误
- html - 响应式视口中的引导工具提示定位出乎意料
- python - 函数变量的变量命名约定
- apache-camel - 作为 AggregationDefinition 的一部分,groupExchanges() 方法是否仍然存在于 apache Camel 中?
- r - ggplot 中的列数与闪亮输入不同
- flutter - 无法访问没有内容类型的请求的正文字段 - Flutter
- angular - TinyMCE 中的 images_upload_handler 不适用于 Angular 8
- python-3.x - 如何优化代码?和平方差代码?