mardi 13 septembre 2016

React - Testing component propTypes - expecting a failure

I'm trying to test a component and I've been looking at the documentation and I don't have a good reference on how to test their failure.

I would like to test the scenario in which a component is not passed required properties, and hence it would fail to mount.

My current test:

jest.dontMock('./Carousel.react.js');
jest.dontMock('react');
var React = require('react');
var ReactTestUtils = require('react-addons-test-utils');
var Carousel = require('./Carousel.react.js');

/* 
Carousel propTypes
propTypes: {
  choices: React.PropTypes.array.isRequired, // an array of image URLs
  choice: React.PropTypes.string
},
*/

describe('Carousel', function () {

  it('should fail when images array is not provided', function () {
    var failed = false;
    try {
      var CarouselDom = ReactTestUtils.renderIntoDocument(<Carousel/>);
      var wrapperDiv = ReactTestUtils.scryRenderedDOMComponentsWithTag(CarouselDom, 'div');
    } catch(e) {
      failed = true;
    }
    expect(failed).toBe(true);
  });

});

Now, while the test succeeds, this doesn't FEEL like the right way to test component failing to mount. What is the correct way?

Aucun commentaire:

Enregistrer un commentaire