程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我如何用玩笑测试下载 excel 文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我如何用玩笑测试下载 excel 文件??

开发过程中遇到我如何用玩笑测试下载 excel 文件?的问题如何解决?下面主要结合日常开发的经验,给出你关于我如何用玩笑测试下载 excel 文件?的解决方法建议,希望对你解决我如何用玩笑测试下载 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) 渲染组件之前,您需要创建 spyfetch

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 是否已被调用

@H_197_39@
@H_197_39@

大佬总结

以上是大佬教程为你收集整理的我如何用玩笑测试下载 excel 文件?全部内容,希望文章能够帮你解决我如何用玩笑测试下载 excel 文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。