I want to test one of methods which helps in closing Modal Window when closed outside of Modal Window container.
Component Method
public hide(): void {
this.visibleAnimate = false;
setTimeout(() => { this.visible = false; }, 200);
}
public onModalClicked(event: MouseEvent): void {
if ((<HTMLElement>event.target).classList.contains('dialog-container')) {
this.hide();
}
}
Unit Test
it('should call hide method', fakeAsync(() => {
component.hide();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.visible).toEqual(false);
tick(200);
expect(component.visibleAnimate).toEqual(false);
});
}));
it('should call onModalClicked()', () => {
const mockEvent = new Event('click'); --> Error
component.onModalClicked(mockEvent);
console.log(component.visible);
});
My Unit test runs fine on hide()
method and please let me know if i am doing it in right way or not.
But i am really stuck how to test onModalClicked()
method because that takes MouseEvent as parameter.
In my unit testing method, Event and MouseEvent mismatch error occurs, which is obviouse biut how to cover this method?