mercredi 25 juillet 2018

Testing react components with PropTypes

If I have a react component with PropTypes defined? what is the best way to test this to ensure that the props are provided without relying on the warnings from the prop-types package?

For example if I have a component like this:

const Button = ({ text }) => <button>{text ? text : 'default button text'}</button>

Button.propTypes = {
  text: PropTypes.string.isRequired
}

I may want to test it to make sure the default button text is rendered if I provide no props but in my text I get the default warning:

console.error node_modules\fbjs\lib\warning.js:33
    Warning: Failed prop type: The prop `text` is marked as required in `Button`, but its value is `undefined`.

Part of my says, the PropTypes mean we don't really need to test this. Part of my says we should test for both the warning and the default text as separate test cases.

Any thoughts and opinions on this would be appreciated.

Aucun commentaire:

Enregistrer un commentaire