首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用filesaver.js响应浅渲染单元测试

用filesaver.js响应浅渲染单元测试
EN

Stack Overflow用户
提问于 2017-03-29 13:23:29
回答 1查看 1K关注 0票数 0

我想测试一个在其中一个方法中使用filesaver.js的组件,但是当我运行测试时,会得到以下错误:

代码语言:javascript
复制
TypeError: Cannot read property 'document' of undefined

      at node_modules\filesaver.js\FileSaver.js:22:15
      at Object.<anonymous> (node_modules\filesaver.js\FileSaver.js:241:2)
      at Object.<anonymous> (src\components\MyComponent.js:3:44)

我的测试看起来是:

代码语言:javascript
复制
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from '../MyComponent';

describe('<MyComponent /> test suite', function () {

   it('Smoke Test (Renders without crashing', () => {
       shallow(<MyComponent />)
   })
})

该组件如下所示:

代码语言:javascript
复制
import React, { PropTypes } from 'react';

import { saveAs } from 'filesaver.js';

class MyComponent extends React.Component {
    constructor(props) {
        super(props)
    }

    handleFileDownload = (attachment) => {
        saveAs(attachment.blob, attachment.file_name)
    }

    render() {
        return (
            <button className="btn btn-default" aria-label="Download" onClick={() => this.handleFileDownload(this.props.attachment)}>
                <i className="icon icon-save" style={{ margin: '0' }} />
            </button>
    }
}

export default MyComponent;

我如何能够在组件中使用filesaver.js编写一个浅表测试,或者是否有一种方法来模拟文件保护程序的导入,因为这是发生错误的地方。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 08:40:41

很显然我没有使用正确的文件保护程序库。我使用的是这个filesaver.js项目,而不是最初的、维护良好的从亮灰中提取filesaver.js。在后面的库中,一切都工作得很好,而且浅层单元测试也是可能的,不会出现任何问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43094820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档