大佬教程收集整理的这篇文章主要介绍了我如何用玩笑测试下载 excel 文件?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是用笑话和酶测试反应成分的新手。我有这个例子
@H_538_5@mIDdleware
我对 .then 部分感到震惊,我不知道在整个 fetch 调用之后如何测试
到目前为止我有什么
generateExcelfile = () => {
const {actions,statE} = this.props;
const dateFrom = state.geTin(['config','markeTingQuestionReport','dateFrom']);
const dateto = state.geTin(['config','dateto']);
this.setState({isLoading: true,isLoadingFinished: falsE});
fetch(`${env.MARKETinG_QUESTION_REPORT}?dateFrom=${dateFrom}&dateto=${dateto}`)
.then((resp) => resp.blob())
.then((blob) => {
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(
blob,`MarkeTing Question Report from ${dateFrom} to ${dateto}.xLSX`
);
}
const url = window.URl.createObjectURL(blob);
const templink = document.createElement('a');
templink.style.display = 'none';
templink.href = url;
templink.setAttribute(
'download',`MarkeTing Question Report from ${dateFrom} to ${dateto}.xLSX`
);
if (typeof templink.download === 'undefined') {
templink.setAttribute('target','_blank');
}
document.body.appendChild(templink);
templink.click();
window.URl.revokeObjectURL(url);
toastr.success('Sucessfully generated markeTing question report');
this.setState({isLoading: false,isLoadingFinished: truE});
actions.clearMarkeTingQuestionReportDates();
})
.catch(() =>
toastr.error('An error occurred while generaTing markeTing question report')
);
};
我为我的组件创建了一个实例 - 我为该调用准备了所需的道具,并将它们发送到我的实例。之后我调用我的方法 - describe('<MarkeTingQuestionReportPage />',() => {
beforeEach(() => {
const fecthSpy = jest.spyOn(window,'fetch').mockReturnValue(() =>
Promise.resolve({
blob: () =>
Promise.resolve({
size: 6682,type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',}),})
);
});
test('generateExcelfile',() => {
const props = {
state: fromJs({
config: {
markeTingQuestionReport: {
dateFrom: '2019-01-01',dateto: '2021-03-21',},actions: {
clearMarkeTingQuestionReportDates: jest.fn(),};
const tree = shallowSetup(props);
tree.instance().generateExcelfile();
tree.fecthSpy.toHaveBeenCalled();
});
});
在我的报道中,除了 fetch 调用之外,一切都被释放了。我不知道如何解决这个问题。请帮忙
在使用 jest.spyOn(object,methodName) 渲染组件之前,您需要创建 spy
的 fetch
:
test('generateExcelFile',() => {
const fetchSpy = jest.spyOn(window,"fetch").mockReturnValue(Promise.resolve({
blob: () =>
Promise.resolve({
size: 6682,type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",}),})
);
//...
const tree = shallowSetup(props);
tree.instance().generateExcelFile();
expect(fetchSpy).toHaveBeenCalled();
});
在您的测试中,您可以检查 fecthSpy
是否已被调用
以上是大佬教程为你收集整理的我如何用玩笑测试下载 excel 文件?全部内容,希望文章能够帮你解决我如何用玩笑测试下载 excel 文件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。