如何在角度2业力中模拟双击事件

时间:2017-02-06 04:07:12

标签: angular karma-jasmine

我有html行元素

<tr class="worktask" (click)="setCurrentTask($event, task)" (dblclick)="openTask($event, task)">some content</tr>

现在我想测试是否在对表中的行元素进行双击时调用了openTask。

我尝试这样做的方式是:

it('should be able to double click on a row to open work stream', async(() => {
        fixture.detectChanges();
        comp.tasks=workflowServiceStub.getTasks(); //get dummy task
        fixture.detectChanges();                   // update view  based on dummy task
        spyOn(comp, 'openTask');                   //spy on openTask function if it is called
        let workTaskRow = fixture.debugElement.nativeElement.querySelectorAll('.worktask'); //getting task row

        workTaskRow[0].dblclick(); 
        fixture.whenStable().then(() => {
           expect(comp.openTask).toHaveBeenCalled();
        })
    }));

但是当我运行测试时,我得到一个错误说:

workTaskRow[0].dblclick is not a function

我尝试了.click()它似乎工作,不确定我是否可以触发双击此处来测试是否调用了openTask()函数。

1 个答案:

答案 0 :(得分:5)

答案是:Angular 2 unit tests: How do I test for the context menu and double click events?

const doubleClickEl: DebugElement[] =  fixture.debugElement.queryAll(By.css("li"); 

doubleClickEl[0].triggerEventHandler("dblclick", new MouseEvent("dblclick"));

fixture.detectChanges();