TypeError:rules.map不是一个函数-React js

时间:2019-02-05 21:51:10

标签: javascript reactjs unit-testing

尝试针对以下内容运行单元测试: 这是一段代码:

    renderAdvancedColumns = () => {
let rules = this.props.selectedFormJSON ? this.props.selectedFormJSON.FORM_RULE : [];
let inner = [];

if(rules.length !== 0) {
  inner = rules.map((rule, i) => { //LINE GIVING ERROR
    return (
      <div className={i===0? 'settings-field first-form-field' : 'settings-field'}>
      <div className='settings-label'>
        {rule.Event}
        <div 
          className={this.props.selectedFormJSON.FORM_RULE[i].FORM_OPERATION.HttpMethod !== 'POST' && 
            this.props.selectedFormJSON.FORM_RULE[i].FORM_OPERATION.HttpMethod !== 'PATCH' ? 
            'tran-button specify-body-button disabled' : 'tran-button specify-body-button'} 
          disabled={this.props.selectedFormJSON.FORM_RULE[i].FORM_OPERATION.HttpMethod === 'GET'}
          onClick={()=>{this.setState({specifyBodyModalOpen: true})}}>
          Specify Body
        </div>
      </div>
      <input className='settings-input' value={this.props.selectedFormJSON.FORM_RULE[i].FORM_OPERATION.Endpoint || ''} onChange={(e) => {this.updateFORMOPERATION(i, 'Endpoint', e.target.value)}} />

我通常会启动测试文件,并确保所有道具均正确渲染。但是不确定为什么会阻止它: inner = rules.map((rule,i)=> {

这是我声明this.props的方式

const baseProps = {
 selectedFormJSON :{
  Subform:{},
 SubformFilter:{},
 OverlayMapper:{},
 Name:{},
 Form_UID:{},
 FORM_RULE:{
 FORM_OPERATION:{
 Endpoint:{},
 HttpMethod:{},
 },
},


  Describe('FormSettings ', () => {
 let wrapper;
 let tree;

 it('should render using all of the props', () => {  
tree = renderer.create(<BrowserRouter><FormSettings {...baseProps} /> 
 </BrowserRouter>)
let treeJson = tree.toJSON()
expect(treeJson).toMatchSnapshot();
tree.unmount()
});

1 个答案:

答案 0 :(得分:1)

selectedFormJSON.FORM_RULE是一个对象

您不能在对象上使用地图。