title: testing a typescript decorator
body: <p>I have a simple decorator to trigger either stopPropagation() or preventDefault() when certain conditions are met. I've tested this in my app and I'm sure the decorator is working properly. However I couldn't test the decorator, whether the aforementioned methods are triggered.</p>
  
  <p>When executing the test I get this error:</p>
  
  <pre><code> Error: Expected spy stopPropagation to have been called.
  </code></pre>
  
  <p><strong>core.decorators.ts</strong></p>
  
  <pre><code>export function eventModifier( stopPropagation = false, preventDefault?: boolean ) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
      const originalMethod = descriptor.value;
      descriptor.value = function() {
        const context = this;
        const mouseEvent: MouseEvent = Array.from( arguments ).find( event =&gt; event instanceof MouseEvent );
  
        if ( stopPropagation ) {
          mouseEvent.stopPropagation();
        }
  
        if ( preventDefault ) {
          mouseEvent.preventDefault();
        }
  
        originalMethod.apply( context, arguments );
      };
  
      return descriptor;
    };
  }
  </code></pre>
  
  <p><strong>core.decorators.spec.ts</strong></p>
  
  <pre><code>import { eventModifier } from './core.decorators';
  
  describe('eventModifier decorator', () =&gt; {
  
    class TestClass {
  
      @eventModifier( true )
      public click( event: MouseEvent ): void {
      }
  
    }
  
    it('decorator is defined', function() {
      expect( eventModifier ).toBeDefined();
    });
  
    it('stopPropagation() should be called', function() {
      const testClass = new TestClass();
      const ev = new MouseEvent('click')
  
      spyOn( testClass, 'click' );
      spyOn( ev, 'stopPropagation' );
  
      testClass.click( &lt;MouseEvent&gt; ev );
  
      expect( testClass.click ).toHaveBeenCalledWith( ev );
      expect( ev.stopPropagation ).toHaveBeenCalled();
    });
  
  });
  </code></pre>