私はジャスミンテストには新しく、マウスの上下、移動などのマウスイベントを処理するディレクティブをテストしようとしています。私の質問は、Jasmineの仕様からマウスディレクティブにマウス座標を渡し、マウスイベントをシミュレートする方法です。私はこのトピックで多くを検索しましたが、要素の座標を渡すようなことをしないthis以外の例は見つかりませんでした。角度2または4のジャスミンテストでマウスイベントをシミュレートする方法
次は角度でテストベッドの構成を使用してテストを書くための私の試みです:
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');
});
});
}
次のように私のディレクティブがある:
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.
}
あなたは私のテストの仕様内で見ることができるようにイベントとしてnullを渡しています。これでテストは正常に実行され、実行されますが、ここからマウスの座標を渡してマウスイベントをシミュレートしたいと思います。誰かに私にいくつかのリソースを与えたり、これを達成するための正しい方向で私に指摘したり、どのような代替案を見出すことができないかを指摘してください。
ご協力いただければ幸いです。
ありがとうございます。
Tammy Gonzalez
https://github.com/angular/material2/blob/master/src/lib/core/ripple/ripple.spec.ts#L75 –