测试嵌套的React组件

时间:2015-05-08 12:21:22

标签: javascript jasmine reactjs jestjs

我正在为我的反应应用程序编写测试。但是当我尝试渲染嵌套组件时,我得到错误TitleBar is not defined并且测试失败。

jest.dontMock('FUW.js');
jest.dontMock('TitleBar.js');

var React = require('react/addons');
var TitleBar = require('../js/components/TitleBar.js');
var FirstUseWindow = require('../js/components/windows/FirstUseWindow.js');
var TestUtils = React.addons.TestUtils;


describe('First use wizard', function(){
    afterEach(function(done){//cleanup the DOM
        React.unmountComponentAtNode(document.body);
        setTimeout(done);
    });

    var FirstUseWindowElement = TestUtils.renderIntoDocument(
        <div>
        <FirstUseWindow />
        </div>
    );
});

FirstUseWindow包含一个导致错误的TitleBar元素。

FUW.js

if (React === undefined) {
    var React = require('react/addons');
}

var FirstUseWindow = React.createClass({
    firstUseComplete:function(){
    },    
    render:function(){
        return(
            <div>
                <TitleBar text="tested" />

            </div>
        );
    }
});

if (module !== undefined) {
    module.exports = FirstUseWindow;
}

TitleBar.js

if (React === undefined) {
    var React = require('react/addons');
}

var TitleBar = React.createClass({
    render:function(){
        return(
            <header className="bar bar-nav">
                <h1 className="title">{this.props.text}</h1>
            </header>
        );
    }
});

if (module != undefined) {
    module.exports = TitleBar;
}

1 个答案:

答案 0 :(得分:2)

标题栏未定义,因为fuw.js中不需要它。