lundi 28 novembre 2016

Testing React PropTypes with sinon

As an example I used Make React PropType warnings throw errors with enzyme.js + sinon.js + mocha.js.

I have a React component with one required prop:

class Pagination extends Component {
    render() {
        return (
            ... render some stuff
        );
    }
}

Pagination.propTypes = {
    total: PropTypes.number.isRequired
};

And this is test for it:

describe('(Component) Pagination', () => {
      before(() => {
         sinon.stub(console, 'error', (warning) => { throw new Error(warning) })
      })
      after(() => { console.error.restore() })

      it('render fails without props', () => {
          shallow(<Pagination />);
      });

      it('render fails without props2', () => {
        shallow(<Pagination />);
      });
    });

After running that tests first one crashes, but second - not. Tests are similar. I think that the problem is that React throws warning messages only once. How to avoid this?

I want to have 2 tests: one that will be crashed when no props is set, and second works fine with props.

Aucun commentaire:

Enregistrer un commentaire