title: How to test handler with jest?
body: <p>I'm trying to mock an event handler from the arcgis js library using jest, but am having trouble getting a reference to the handler and triggering the callback function. I decided to mock the event handler using node's built-in <code>events</code> module but when I emit the event, the callback is never entered.</p>
  <p>foo.ts</p>
  <pre><code>  createSomething(
      onComplete: fooCompleteCallback
    ): void {
      if (this.isReady()) {
        const handler = foo.addEventHandler((event) =&gt; {
          if (event.state === EVENT_COMPLETE) {
            handler.remove();
            onComplete(event.graphic);
          }
        });
        foo.create();
      }
    }
  </code></pre>
  <p>foo.test.ts</p>
  <pre><code>import events from 'events';
  
  class foo {
    eventEmitter = new events.EventEmitter();
    
    addEventHandler = () =&gt; this.eventEmitter.on('complete', (callback) =&gt; callback({state: 'complete'}));
    isReady = jest.fn().mockImplementation(() =&gt; true);
    create = () =&gt; this.eventEmitter.emit('complete');
  }
  export default new foo();
  </code></pre>