vendredi 1 juin 2018

How to test image events in Angular

Let's say we have a simple component that takes some actions on image event

<img src="" (error)="onImageError($event)" (load)="onImageLoad()" width="">

Now I want to test that this event handlers were triggered.

I tried the most simple approach

const spyError = spyOn(component, 'onImageError').and.callThrough();

fixture.detectChanges();

expect(spyError).toHaveBeenCalled();

It didn't work because the event loop is out of the execution stack of JavaSctipy. I tried to resolve it with asyn or fakeAsync but it didn't help.

The only worked solution is to use setTimeout inside the tests, which I don't really like.

So, my questions are

  1. Shall we test it at all? It looks like part of Angular/browser functionality. I can call component.onImageError() directly in a test.

  2. What is the clean way to test that event handler was called?

Aucun commentaire:

Enregistrer un commentaire