Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 测试组件内部的方法?_Reactjs_Mocha.js - Fatal编程技术网

Reactjs 测试组件内部的方法?

Reactjs 测试组件内部的方法?,reactjs,mocha.js,Reactjs,Mocha.js,我不熟悉TDD和单元测试我的js,并且正在考虑使用mocha/react浅层渲染器来测试我的react组件。我的测试组件如下所示: var React = require('react'); var test = React.createClass({ render: function() { return ( <div>My custom test component ({this.props.testAttribute})&l

我不熟悉TDD和单元测试我的js,并且正在考虑使用mocha/react浅层渲染器来测试我的react组件。我的测试组件如下所示:

var React = require('react');

var test = React.createClass({

    render: function() {

        return (
            <div>My custom test component ({this.props.testAttribute})</div>
        );
    },

    testFunction: function () {
        return true;
    }

});

module.exports = test;
import jsdom from "jsdom";
import TestUtils from "react/lib/ReactTestUtils";

describe("Component Test", function () {

    beforeEach(function () {
        global.document = jsdom.jsdom("<!doctype html><html><body></body></html>");
        global.window = global.document.defaultView;

        this.component = TestUtils.renderIntoDocument(<MyComponent/>);
    });

    afterEach(function () {
        delete global.document;
        delete global.window;
    });


   it("test the function", function () {
        expect(this.component.testFunction()).to.be(true);
    });
});
var React=require('React');
var test=React.createClass({
render:function(){
返回(
我的自定义测试组件({this.props.testAttribute})
);
},
testFunction:function(){
返回true;
}
});
module.exports=测试;

因此,除了浅层渲染和确保组件以我需要的方式进行渲染(我现在可以这样做)之外,我还想测试testFunction(),并确保其正常运行。我该怎么做呢

我认为这取决于testFunction是什么类型的函数

如果它与渲染密切相关,例如它是一个onClickHandler,那么我将结合渲染来测试它。浅层渲染在这里可能不够,您可能需要一个真正的DOM(查看jsDOM和/或酶测试库)

如果函数执行一些与渲染不紧密耦合的计算,那么您可能会考虑将其提取到自己的文件中(想想这里的模块化和关注点分离等方面)。这样,您就可以像其他JavaScript代码一样单独测试它

如果您确实想在函数位于组件内部时单独测试该函数,则需要一个真正的DOM。摩卡咖啡/必饮咖啡的测试如下:

var React = require('react');

var test = React.createClass({

    render: function() {

        return (
            <div>My custom test component ({this.props.testAttribute})</div>
        );
    },

    testFunction: function () {
        return true;
    }

});

module.exports = test;
import jsdom from "jsdom";
import TestUtils from "react/lib/ReactTestUtils";

describe("Component Test", function () {

    beforeEach(function () {
        global.document = jsdom.jsdom("<!doctype html><html><body></body></html>");
        global.window = global.document.defaultView;

        this.component = TestUtils.renderIntoDocument(<MyComponent/>);
    });

    afterEach(function () {
        delete global.document;
        delete global.window;
    });


   it("test the function", function () {
        expect(this.component.testFunction()).to.be(true);
    });
});
从“jsdom”导入jsdom;
从“react/lib/reactestutils”导入TestUtils;
描述(“组件测试”,功能(){
beforeach(函数(){
global.document=jsdom.jsdom(“”);
global.window=global.document.defaultView;
this.component=TestUtils.renderIntoDocument();
});
在每个(函数()之后){
删除global.document;
删除global.window;
});
它(“测试功能”,功能(){
expect(this.component.testFunction()).to.be(true);
});
});

谢谢你,妮可。实际上,我也在考虑同样的想法,将testFunction推到另一个模块中,然后只需要它。这将使测试变得非常简单。现在,我正试图找到正确的测试库组合,使我能够轻松测试像示例组件一样编写的代码,因为我们的许多代码库最初就是这样编写的。感谢gerald的解释。我对此进行了更深入的研究,并添加了一个例子,说明你应该如何实现你想要的。这很有魅力!这太完美了,非常感谢你的帮助!