title: Testing a button with a method callback in a functional component
body: <p>I am trying to test a react component using Enzyme. I am not able to test the click on the IconButton component and the function doesn't get called when i simulate a click.</p>
  <p>This is how IconButton is defined on an external component.</p>
  <pre><code>var IconButton = function (props) {
      return (React.createElement(IconButton$1, { color: 'default', onClick: props.onClick, disabled: props.disabled, size: props.size, onMouseDown: props.onMouseDown }, props.children));
  };export{Button,IconButton};
  
  </code></pre>
  <p>This is how I am using it in my app.</p>
  <pre><code>import React, {useState, useEffect} from 'react';
  import { Drawer } from '@material-ui/core';
  import ExpandLessIcon from '@material-ui/icons/ExpandLess';
  import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
  import { IconButton } from '@mycomponent/button';
  
  export default function Component1 {
    const classes = useStyles();
    const [open, setOpen] = useState(true);
  
    const handleClick = function (event) {
      if (event) {
        setOpen(!open);
      }
      else {
        return;
      }
    };
  
    return (
      &lt;Drawer&gt;
        &lt;div className=&quot;classname1&quot;&gt;
            &lt;IconButton onClick={(e) =&gt; handleClick(e)} className={classes.button, &quot;iconBtn&quot;}&gt;
              {open ? &lt;ExpandLessIcon data-test=&quot;lessIcon&quot; /&gt; : &lt;ExpandMoreIcon data-test=&quot;moreIcon&quot; /&gt;}
            &lt;/IconButton&gt;
        &lt;/div&gt;
      &lt;/Drawer&gt;
    );
  }
  </code></pre>
  <p>Here is my test for simulating the click on the Icon Button. I also tried another way to check that the handleClick was getting called but it still fails.</p>
  <pre><code>    const wrapper = shallow(&lt;Component1 /&gt;);
  
      it('Test the button click', () =&gt; {
          expect(wrapper.containsMatchingElement(&lt;ExpandMoreIcon /&gt;)).toBeTruthy()
          const element = wrapper.find(&quot;.iconBtn&quot;)
          const mockEvent = {target: {}};
          element.simulate('click', mockEvent)
          expect(wrapper.containsMatchingElement(&lt;ExpandLessIcon /&gt;)).toBeTruthy()
      })
  </code></pre>