【问题标题】:Cannot read property of state object when testing React component that renders state variable in render测试在渲染中呈现状态变量的 React 组件时无法读取状态对象的属性
【发布时间】:2015-04-29 07:21:20
【问题描述】:

我正在尝试测试一个组件,该组件具有来自它的 render() 中显示的状态对象的值。

我已将有问题的组件简化为这个简单的测试组件以进行复制。我正在使用 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(),但必须有更好的方法。

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

【问题讨论】:

    标签: reactjs jestjs


    【解决方案1】:

    我最终通过像这样模拟 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();
      });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多