0

In a component, I have a function I need to write a test for.

toggleDropdown($event: MouseEvent): void {
    $event.preventDefault();
    $event.stopPropagation();
    this.status.isopen = !this.status.isopen;
  }

How can I "stub" the $event or more precisely MouseEvent (for click case for example) and test the two methods preventDefault() and stopPropagation() have been called properly?

And how to trigger this method including a proper $event?

I struggle defining a proper test case for this method. I tried to spyOn $event but it doesn't work properly as cannot find name $event

BlackHoleGalaxy
  • 9,160
  • 17
  • 59
  • 103
  • I cannot verify it at the moment, but you could try: spyOn(MouseEvent.prototype, 'preventDefault'); in combination with a manually created MouseEvent. I used this with custom DI tests. – Markai Apr 10 '17 at 11:25
  • Do you any cue on how to achieve creating a MouseEvent by hand which is "proper" and relevant? – BlackHoleGalaxy Apr 10 '17 at 11:36
  • 1
    If I understood you correctly, you need something similar to this: http://stackoverflow.com/questions/11529158/how-to-assert-a-spy-is-invoked-with-event-on-click-using-jasmine – Markai Apr 10 '17 at 11:50

1 Answers1

1

Unless you are checking that those two functions have been called, there is no need to. Simply pass new Event('MouseEvent') as an argument.

cachesking
  • 23
  • 5