0

render() に表示される状態オブジェクトの値を持つコンポーネントをテストしようとしています。

問題のコンポーネントを単純化して、再現のためにこの単純なテスト コンポーネントにしました。React 0.12.2 を使用しています。

getIntitialState'sへの呼び出しで「レポート」にデータを入力していgetStateFromStores()ます。テストでは、この値は空であり、エラーにつながっていると思います。

が定義されているかどうかを確認する条件付きチェックは確かに機能しますが、状態を介して入力this.state.reportされた a に出力されたすべての変数に条件を設定する必要があるのは少し難しいようです。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()を作成することも検討しましたが、もっと良い方法があるはずです。

これをテストする方法についてのアイデアはありますか?

4

1 に答える 1

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();
  });

});
于 2015-02-27T02:27:38.123 に答える