BlackHoleGalaxy
BlackHoleGalaxy

Reputation: 9662

Angular 2 testing $event: MouseEvent preventDefault and stopPropagation hasBeenCalled

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

Upvotes: 0

Views: 2713

Answers (1)

cachesking
cachesking

Reputation: 23

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

Upvotes: 1

Related Questions