jeudi 5 janvier 2017

How to test inner functions defined on Stateless Components in React with Enzyme

I'm using enzyme to test my react components. I have a stateless component that has an inner function. How can i call and test that inner function?

Here is my component:

const Btn = (props) => {
  const types = ['link', 'plainLink', 'primary', 'secondary', 'danger', 'info'];

  const handleClick = (event) => {
    event.preventDefault();
    props.onClick(event);
  };

  return (
    <button onClick={handleClick} className={classes}>
      <span>{props.children}</span>
    </button>
  );
};

I've tried the following, but get an error saying: TypeError: undefined is not a constructor

const btnComp = shallow(<Btn />);
btnComp.instance().handleClick();

Aucun commentaire:

Enregistrer un commentaire