Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了单元测试 – 使用Jasmine进行Angular2测试,mouseenter / mouseleave-test大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个HighlightDirective,如果鼠标进入某个区域会突出显示,如:

@Directive({
  SELEctor: '[myHighlight]',host: {
    '(mouseenter)': 'onMouseEnter()','(mouSELEavE)': 'onMouSELEave()'
  }
})
export class HighlightDirective {
  private _defaultColor = 'Gainsboro';
  private el: HTMLElement;

  constructor(el: ElementRef) { this.el = el.nativeElement; }

  @Input('myHighlight') highLightcolor: String;

  onMouseEnter() { this.highlight(this.highLightcolor || this._defaultColor); }
  onMouSELEave() { this.highlight(null); }

  private highlight(color:string) {
    this.el.style.BACkgroundColor = color;
  }

}

现在我想测试,如果在事件上调用(右)方法.所以像这样:

it('check if item will be highlighted',inject( [TESTComponentBuilder],(_tcb: TESTComponentBuilder) => {
    return _tcb
      .createAsync(TestHighlight)
      .then( (fixturE) => {
        fixture.detectChanges();
        let element = fixture.nativeElement;
        let component = fixture.componenTinstance;
        spyOn(component,'onMouseEnter');
        let div = element.querySELEctor('div');


        div.mouseenter();


        expect(component.onMouseEnter).toHaveBeenCalled();
      });
  }));

使用TESTClass:

@Component({
  template: `<div myHighlight (mouseenter)='onMouseEnter()' (mouSELEavE)='onMouSELEave()'></div>`,directives: [HighlightDirective]
})
class TestHighlight {
  onMouseEnter() {
  }
  onMouSELEave() {
  }
}

现在,我收到了消息:

那么,有谁知道,哪个是正确的功能(如果存在)?我已经尝试过使用click()..

谢谢!

解决方法

代替

div.mouseenter();

这应该工作:

let event = new Event('mouseenter');
div.dispatchEvent(event);

大佬总结

以上是大佬教程为你收集整理的单元测试 – 使用Jasmine进行Angular2测试,mouseenter / mouseleave-test全部内容,希望文章能够帮你解决单元测试 – 使用Jasmine进行Angular2测试,mouseenter / mouseleave-test所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。