JavaScript的 错误
1) shallow renderer tests for sports-template-standard should render correctly:
TypeError: Cannot read property 'exists' of undefined
at [object Object].dispatchSidebarExists (/src/sports-template-standard.jsx:80:56)
at [object Object].componentWillMount (/src/sports-template-standard.jsx:49:14)
at [object Object].ReactCompositeComponentMixin.mountComponent (\node_modules\react\lib\ReactCompositeComponent.js:210:12)
at [object Object].wrapper [as mountComponent] (\node_modules\react\lib\ReactPerf.js:66:21)
at [object Object].ReactShallowRenderer._render (\node_modules\react\lib\ReactTestUtils.js:362:14)
at [object Object].ReactShallowRenderer.render (\node_modules\react\lib\ReactTestUtils.js:344:8)
at Context.<anonymous> (/test/sports-template-standard-tests.js:31:25)
at callFn (\node_modules\mocha\lib\runnable.js:286:21)
at Test.Runnable.run (\node_modules\mocha\lib\runnable.js:279:7)
at Runner.runTest (\node_modules\mocha\lib\runner.js:421:10)
at \node_modules\mocha\lib\runner.js:528:12
at next (\node_modules\mocha\lib\runner.js:341:14)
at \node_modules\mocha\lib\runner.js:351:7
at next (\node_modules\mocha\lib\runner.js:283:14)
at Immediate._onImmediate (\node_modules\mocha\lib\runner.js:319:5)
测试用例
`javascript`
import {expect} from 'chai';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import initializeJsDom from './test-utils/dom.js';
import {SportsPageClasses} from 'sports-css-grids';
import sportsMockUpStandard from '../src/sports-template-standard';
describe('shallow renderer tests for sports-template-standard ', function() {
let shallowRenderer = TestUtils.createRenderer();
console.log("shallowRenderer" + JSON.stringify(shallowRenderer));
it('should render correctly', () => {
//var rightPanel ="some string";
var layout ="some string";
var hasSidebar = function(){
done();
}
console.log("here1");
//this.props.layout.rightPanel.exists = 'some value';
shallowRenderer.render(<sportsMockUpStandard
headerClass='8887'
layout= {{id: 100, text: 'hello world'}}
sidebar= {[{hasSidebar},{id: 100, text: 'hello world'}]}
title={"Demo"} />);
// shallowRenderer.render(<sportsMockUpStandard
//layout= {{id: 100, text: 'hello world'}} />);
console.log("here2");
});
});
代码段
`javascript`
dispatchSidebarExists(props) {
let hasSidebar = !!props.sidebar;
// infinite loop without this check
if (hasSidebar !== this.props.layout.rightPanel.exists) {
this.props.dispatch(setSportsCornerState({
exists: hasSidebar
}));
}
}
整个代码
export function setSportsCornerState(stateString) { return { type: 'SET_HEADERPANEL', stateString }; } ```
新警告
Warning: Failed propType: Invalid prop `sidebar` supplied to `sportsMockUpStandard`.
// var rightPanel ="some string";
var layout ="some string";
var hasSidebar = function(){
// done();
}
console.log("here1");
// this.props.layout.rightPanel.exists = 'some value';
shallowRenderer.render(<sportsMockUpStandard
headerClass='8887'
layout= {{rightPanel: {exists: hasSidebar}, text: 'hello world'}}
sidebar= {[{hasSidebar},{id: 100, text: 'hello world'}]}
title={"Demo"} dispatch={hasSidebar} />);
答案 0 :(得分:1)
基本上,你的reducer在状态上调用属性exists
,对吧?但它没有找到它?这是因为您已经发送了一个对象,但没有添加有效负载。
你的代码说你像这样派遣对象。
setSportsCornerState(stateString) {
return { type: 'SET_HEADERPANEL', stateString };
}
在这里给出你的代码......
this.props.dispatch(setSportsCornerState({
exists: hasSidebar
}));
....这意味着你调度的对象看起来像这样。
{ type: 'SET_HEADERPANEL', {exists: hasSidebar} }
我实际上不确定在这种情况下如何命名属性,如果你有一个对象和其中没有属性名称的另一个对象。话虽这么说,这就是你的对象应该是什么样子。
{ type: 'SET_HEADERPANEL', payload: {exists: hasSidebar} }
如果要将信息传递给reducer,则需要将其传递给payload属性。您的对象应始终具有有效负载,并且该有效负载应始终为对象。将您的代码更改为以下内容。
export function setSportsCornerState(stateString) {
return { type: 'SET_HEADERPANEL', payload: stateString };
}
将有效负载对象设置为您要传递的信息。然后你的reducer应该在有效负载对象上查找exists
属性。