2016-11-25 53 views
1

我有模板一些(MyCmp)组件(<my-cmp></my-cmp>)这样角2.单元发射悬停事件测试

<template ngFor let-r [ngForOf]="range" let-index="index"> 
    <span>{{ index < 5 ? 'Y' : 'N' }}, {{r.data}}</span> 
    <i (mouseenter)="somefunc()" (click)="elefunc()"></i> 
    .... 
</template> 

我通过特殊TestComponent

TestBed.configureTestingModule({declarations: [TestComponent], imports: [MyModule]} 
TestBed.overrideComponent(TestComponent, {set: {template: '<my-cmp></my-cmp>'}}); 
fixture = TestBed.createComponent(TestComponent); 
context = fixture.debugElement.componentInstance; 
element = fixture.nativeElement; 
fixture.detectChanges(); 

我配置MyCmp组件测试床认为这不重要。测试工作。

element.querySelectorAll('i')[0].click(); //fine 

但我不知道我应该怎么发出悬停(的mouseenter)和鼠标离开事件

element.querySelectorAll('i')[0].hover() // not a function 
element.querySelectorAll('i')[0].mouseover() // not a function 
element.querySelectorAll('i')[0].createMouseEvent('mouseover') // not a function 
+0

在下面的情况下,您将发出类似于click的事件。 – Manish

+0

是的。我尝试发出类似于click的mouseenter事件,但它不起作用 –

+1

element.querySelectorAll('i')[0] .onmouseover() – Manish

回答

0

如果你还没有为这个答案,你可以使用nativeElement的dispatchEvent以下列方式:

element.dispatchEvent(new MouseEvent('mouseover', { 
    view: window, 
    bubbles: true, 
    cancelable: true 
}));