I have this simple method which needs to be tested:
public onLayerContainerClick(event: Event): void {
event.stopPropagation();
event.preventDefault();
if (event.srcElement.classList.contains('dpm-info__layerContainer')) {
this.closeInfoLayer(event);
}
}
My objective is actually to increase code coverage for the component. If I test is like this:
it( 'should close on info container click', () => {
spyOn( component, 'onLayerContainerClick' );
const el: DebugElement = fixture.debugElement.query( By.css( '.dpm-info__layerContainer' ) );
el.triggerEventHandler( 'click', null );
expect( component.onLayerContainerClick ).toHaveBeenCalled();
} );
the test is OK, but instanbul says "function not covered". So I guess, I need to call the function explicitly? To do that, I need to have a full event object, including (at least) srcTarget property. How do I define such an event within the unit test?
onLayerContainerClick
to be of typeEvent
. Just passing an object withsrcTarget
property doesn't cut it - I get a type error. – Ratiocinate