在测试渲染中呈现状态变量的React组件时,无法读取状态对象的属性

时间:2015-02-26 18:55:21

标签: reactjs jestjs

我试图测试一个组件,该组件具有显示在其渲染()中的状态对象的值。

我已将相关组件简化为这个简单的测试组件以进行复制。我使用React 0.12.2。

我正在填写我的报告"在getIntitialState's致电getStateFromStores()。在测试中虽然这个值是空的,但我认为这会导致错误。

当然有条件检查以确定是否定义this.state.report会起作用,但是对于通过状态填充的render()中打印的所有变量放置条件似乎有点多。

测试组件

var React = require('react');
var AppStore = require('../stores/AppStore');

function getStateFromStores() {
  return {
    report: AppStore.getCurrentReport(),
  };
}

var Test = React.createClass({

  getInitialState: function() {
    return getStateFromStores();
  },

  render: function(){
    return (
       <div>
        // This call to the report.id on state seems to be the issue
        {this.state.report.id}
      </div>
    );
  }

});

module.exports = Test;

测试

jest.dontMock('../Test');
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Test = require('../Test');

describe("Test", function() {

  it("should render Test", function() {
    var test = TestUtils.renderIntoDocument(<Test />);
    expect(test).toBeDefined();
  });

});

理想情况下,我希望在测试中调用renderIntoDocument()之前预先填充组件的状态,因为它是失败的地方。

我收到了这个失败:

● Test › it should render Test
- TypeError: Cannot read property 'id' of undefined
    at React.createClass.render            (/Users/kevinold/_development/app/assets/javascripts/_app/components/Test.jsx:20:26)
    at ReactCompositeComponentMixin._renderValidatedComponent     (/Users/kevinold/_development/node_modules/react/lib/ReactCompositeComponent.js:1260:34)
    at wrapper [as _renderValidatedComponent] (/Users/kevinold/_development/node_modules/react/lib/ReactPerf.js:50:21)
    at ReactCompositeComponentMixin.mountComponent (/Users/kevinold/_development/node_modules/react/lib/ReactCompositeComponent.js:802:14)
    at wrapper [as mountComponent] (/Users/kevinold/_development/node_modules/react/lib/ReactPerf.js:50:21)
    at ReactComponent.Mixin._mountComponentIntoNode (/Users/kevinold/_development/node_modules/react/lib/ReactComponent.js:405:25)
    at ReactReconcileTransaction.Mixin.perform (/Users/kevinold/_development/node_modules/react/lib/Transaction.js:134:20)
    at ReactComponent.Mixin.mountComponentIntoNode (/Users/kevinold/_development/node_modules/react/lib/ReactComponent.js:381:19)
    at Object.ReactMount._renderNewRootComponent (/Users/kevinold/_development/node_modules/react/lib/ReactMount.js:312:25)
    at Object.wrapper [as _renderNewRootComponent] (/Users/kevinold/_development/node_modules/react/lib/ReactPerf.js:50:21)
    at Object.ReactMount.render (/Users/kevinold/_development/node_modules/react/lib/ReactMount.js:381:32)
    at Object.wrapper [as render] (/Users/kevinold/_development/node_modules/react/lib/ReactPerf.js:50:21)
    at Object.ReactTestUtils.renderIntoDocument (/Users/kevinold/_development/node_modules/react/lib/ReactTestUtils.js:48:18)
    at Spec.<anonymous> (/Users/kevinold/_development/app/assets/javascripts/_app/components/__tests__/Test-test.js:9:26)
    at jasmine.Block.execute (/Users/kevinold/_development/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
    at jasmine.Queue.next_   (/Users/kevinold/_development/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
    at null._onTimeout (/Users/kevinold/_development/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)

我不确定如何在我的测试renderIntoDocument()调用之前预先加载此组件的状态,这应该可以解决问题。

我还考虑过尝试模仿getIntitialState()这个组件,但必须有更好的方法。

关于如何测试这个的任何想法?

1 个答案:

答案 0 :(得分:0)

我最终通过嘲笑AppStore.getCurrentReport()方法解决了这个问题:

jest.dontMock('../Test');
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Test = require('../Test');
var AppStore = require('../stores/AppStore');

describe("Test", function() {

  it("should render Test", function() {
    // Mock the return value from the method in the store that populates the value in getInitialState()
    AppStore.getCurrentReport.mockReturnValue({id: 1, title: 'Test Rpt'});

    var test = TestUtils.renderIntoDocument(<Test />);
    expect(test).toBeDefined();
  });

});