使用多个对象迭代数组

时间:2016-03-17 02:55:37

标签: javascript jquery html reactjs mocha

在这里格式化...在堆栈中尝试但是没有那么好...... https://gist.github.com/js08/22654f6cd2f160aa2e0a

  • 我是mocha测试的新手..
  • 现在它使用数组中的单个对象。
  • 如何使其适用于数组中的多个对象。
  • 我不知道如何迭代它
  • 在下面提供我的代码
  • 数组中的单个对象:

    shallowRenderer.render(<SportsList  disclosures ={[ 'id': 'foo', 'text': 'car racing' ]} />);
    
  • 数组中的多个对象:

shallowRenderer.render(<SportsList  disclosures ={[ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]} />);

import {expect} from 'chai';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import SportsList from '../../src/components/footer/disclosure-list.jsx';
import initializeJsDom from '../test-utils/dom.js';
import {getGridLayoutClasses} from 'sports-css-grids';


describe('shallow renderer tests for disclosure-list ', function() {
    let shallowRenderer = TestUtils.createRenderer();


    it('should render correctly', () => {

        shallowRenderer.render(<SportsList  disclosures ={[ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]} />);
        //shallowRenderer.render(<SportsList  disclosures ={[ 'id': 'foo', 'text': 'car racing' ]} />);
        let sportsOutputElement = shallowRenderer.getRenderOutput();

        let sportsTitle = sportsOutputElement.props.children[0].key;

        let text = sportsOutputElement.props.children[0].props.dangerouslySetInnerHTML.__html;

       //result succeful for single value 
        expect(sportsTitle).equal('100');
       //result succeful for single value
        expect(text).equal('car racing');
    });



 });

json structure
renderedElement - multiple arrays-- -- > 

{
  "type": "div",
  "key": null,
  "ref": null,
  "props": {
    "className": "pageFooter-disclosures",
    "children": [{
      "type": "p",
      "key": "100",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello world"
        }
      },
      "_owner": null,
      "_store": {}
    }, {
      "type": "p",
      "key": "10",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello vworld"
        }
      },
      "_owner": null,
      "_store": {}
    }, {
      "type": "p",
      "key": "100",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello world"
        }
      },
      "_owner": null,
      "_store": {}
    }]
  },
  "_owner": null,
  "_store": {}
}√
should render correctly(3 ms)

1 个答案:

答案 0 :(得分:0)

使用来自expect的{​​{1}},您可以使用eql来测试对象的深度相等性

chai