我正在尝试测试一个组件,该组件具有来自它的渲染()中显示的状态对象的值。
我已将有问题的组件简化为这个简单的测试组件以进行复制。我正在使用 React 0.12.2。
我正在填充我的“报告”以getIntitialState's
调用getStateFromStores()
. 在测试中,虽然这个值是空的,但我认为是导致错误的原因。
当然,检查是否已定义的条件检查会起作用,但是必须对所有通过 state 填充的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()
这个组件,但必须有更好的方法。
关于如何测试这个的任何想法?