title: How to simulate/test key press using Jest?
body: <p>I'm trying to simulate/test the keypress event which calls <code>props.onClick</code>. I successfully simulated the <code>onClick</code> with similar testing code, but the keypress doesn't work.  </p>
  
  <p>When I tested it manually by pressing the Enter key, it works as it should, but when I try to write tests for it, it keeps failing and I'm not sure why. I put a logger in the <code>useEffect</code> function, and it gets called during the test, but it fails and gives an error that <code>onClickFnc</code> was never called. </p>
  
  <p>In my button function component, I bind an event listener:</p>
  
  <p><strong>button.js</strong></p>
  
  <pre><code>  useEffect(() =&gt; {
      if (enterEnable) {
        window.addEventListener('keydown', onEnterPress);
        return () =&gt; window.removeEventListener('keydown', onEnterPress);
      }
    });
  
    const onEnterPress = e =&gt; {
      if (e.which == 13) {
        onClickSync(e);
      }
    };
  
    const onClickSync = e =&gt; {
      props.onClick(e);
    };
  </code></pre>
  
  <p><strong>button.test.js</strong></p>
  
  <pre><code>  it('check ButtonLoader enable enter to work', () =&gt; {
  
      const onClickFnc = jest.fn(); 
      const wrapper = shallow(&lt;ButtonLoader {...props} enterEnable={true} onClick={onClickFnc}/&gt;);
      wrapper.find('button').simulate('keypress', {key: 'Enter'})
      expect(onClickFnc).toBeCalled();
    });
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>    expect(jest.fn()).toBeCalled()
  
      Expected number of calls: &gt;= 1
      Received number of calls:    0
  </code></pre>
  
  <p>How can I successfully test this?</p>