I am pretty new to Jasmine testing and I am trying to test a directive which handles mouse events like mouse down, up and move. My question is how can I pass mouse coordinates from the spec of Jasmine to my directive and simulate the mouse events. I have searched a lot on this topic but I couldn't find any examples except for this one which doesn't do anything like passing the coordinates of the element.
The following is my attempt to write the test using the TestBed configuration in Angular:
import { Component, Directive, DebugElement } from "@angular/core";
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { By } from '@angular/platform-browser';
import { TestDirective } from "./test";
import { MyService } from "./my-service";
@Component({
template: `<div testDirec style="height:800px; width:500px; background-color:blue;"></div>`
})
class DummyComponent { }
export default function () {
describe('Directive: Zoom', () => {
let fixture: ComponentFixture<TestComponent>;
let debugEle: DebugElement[];
beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
}
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestDirective, DummyComponent],
providers: [MyService]
});
fixture = TestBed.createComponent(DummyComponent);
fixture.detectChanges();
debugEle = fixture.debugElement.queryAll(By.directive(TestDirective));
});
it('mousedown on the div', () => {
debugEle[0].triggerEventHandler('mousedown', null);
expect(debugEle[0].nativeElement.style.width).toBe('500px');
});
it('mousemove on the div', () => {
debugEle[0].triggerEventHandler('mousemove', null);
expect(debugEle[0].nativeElement.style.backgroundColor).toBe('blue');
});
});
}
My directive is as follows:
import { Directive, ElementRef, HostListener} from "@angular/core";
import { MyService } from "./my-service";
@Directive({
selector: "[testDirec]"
})
export class Test {
private initPointX: number;
private initPointY: number;
constructor(private ele: ElementRef,
private serviceInstance: MyService) {
}
@HostListener('mousedown', ['$event'])
onMouseDown(event: MouseEvent) {
console.log("Entered mouse down");
this.initPointX = event.PageX;
this.initPointY = event.PageY;
if (event.ctrlKey) {
// do something
}
}
@HostListener('mousedown', ['$event'])
onMouseMove(event: MouseEvent) {
console.log("Entered mouse move");
if (this.initPointX && this.initPointY) {
// calculate the new mouse x and y coordinates and compute the difference to move the object.
}
}
//other functions.
}
As you can see inside my test spec, I am passing the null as the event. This will successfully execute and run my tests but instead I would like to simulate the mouse events by passing the mouse coordinates from here. Could anyone give me some resources or point me in a right direction how to achieve this or if it cannot be achieved what alternatives I can look into.
Any help would be greatly appreciated.
Thank you.
Tammy Gonzalez
Angular has a total of 9 mouse events. 1. Click Click event occurs when the user clicks the click applied element. click event triggers the function attached to the click. Triggers when user double-click the dblclick applied element. 3. Mouseover Triggers when the cursor is over the mouseover applied element (like hover). 4. Mouseenter
There are two ways to trigger events in unit tests. Let’s examine both of them. The Angular DebugElement instance provides a handy method for triggering events — triggerEventHandler ().
We can test this by using the Jasmine function expect (), which will verify whether the incoming output is equal, contain, toBe an expected output or not. It is going to help us to assert that the functionality returns the given output. Finally, our spec file will look as follows: To run the spec file we need to write the command ng test.
In the beforeEach function for our test suite, we configure a testing module using the TestBed class. This creates a test Angular Module which we can use to instantiate components, perform dependency injection, and so on.The testBed is a mock environment to run Angular 2+ component tests without the browser.
This should be more readable than the other answers:
const btn = btnDbg ? <HTMLButtonElement>btnDbg.nativeElement : new HTMLButtonElement();
btn.dispatchEvent(new Event('mousedown'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With