我有一个带有鼠标滚轮事件的指令,它用于放大和缩小画布。我想知道如何为这样的事件编写单元测试。我在网上找不到任何例子,有人能指出我正确的方向吗?
我的指示:
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('mousewheel', ['$event'])
scroll(event: MouseEvent) {
console.log("Entered mouse wheel");
let wheelDelta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
if(wheelDelta > 0) {
factor = 0.5;
}else {
factor = 2.0;
}
this.initPointX = event.PageX;
this.initPointY = event.PageY;
}
}
答案 0 :(得分:5)
你应该使用转换并测试它。如下
<div scrollstyle="height:1000px;background-color:red">
<input type="text" value=""/>
</div>
@Directive({
selector: '[scroll]'
})
export class ChangeDirective{
i:number=1;
constructor(
private renderer: Renderer,
private el: ElementRef
){}
@HostListener('mousewheel', ['$event']) onMousewheel(event) {
if(event.wheelDelta>0){
event.srcElement.style.setProperty('transition','all 200ms ease-in')
event.srcElement.style.setProperty('transform',"scale(" + this.i+1 +")")
}
if(event.wheelDelta<0){
event.srcElement.style.setProperty('transition','all 200ms ease-out')
event.srcElement.style.setProperty('transform',"scale(" + this.i-1 +")")
}
}
}
<强> LIVE DEMO 强>