Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate double click event in angular 2 karma

I have html row element as so

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

Now I want to test if openTask is called when some one double click on a row element in the table.

The way I am trying to do this is:

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();
        })
    }));

But when I run the test I get an error saying:

workTaskRow[0].dblclick is not a function

I tried .click() and it seem to work , not sure I can trigger a double click here to test if openTask() function is called.

like image 801
Snedden27 Avatar asked Feb 06 '17 04:02

Snedden27


People also ask

How angular double click is implemented?

The ng-dblclick directive tells AngularJS what to do when an HTML element is double-clicked. The ng-dblclick directive from AngularJS will not override the element's original ondblclick event, both are executed.

What is the correct way to trigger a click event on a button when testing angular component?

For click event we can use triggerEventHandler method of Angular DebugElement class. We can also call native JavaScript click method of button. On click of button, we call a component method and it is possible that our component method has other dependencies to execute.

How do you trigger event in Jasmine?

According to the Angular Testing documentation, to trigger the events from the tests, we use the triggerEventHandler() method on the debug element. This method takes the event name and the object . Now, this works if we are adding the events using the HostListener .

How to use double click event in Angular 2?

Angular 2 Double Click (dblclick) event using the EventEmitter, a mother component receives the double click (dblclick) event from the child component when a user clicks twice on an element. 6. Angular 2 Tutorial: Adding HotKeys (Keyboard shortcuts) to a Component

How do I trigger an event in angular debugelement?

The Angular DebugElement instance provides a handy method for triggering events — triggerEventHandler (). Let’s see how we can use it. We have a simple test for a component that, upon a click, sets an emoji.

How can I navigate differently on a double-click event?

If you want to navigate differently on a double-click, you would have to delay the navigation, to allow the double-click event to be triggered (or not). You could also navigate to a different destination if a key is pressed (e.g. shift, ctrl) when the row is clicked. Here you can use a timeout and a boolean flag to solve this.

How do I trigger a click programmatically using htmlelement?

Luckily, it’s a click event and we can trigger a click programmatically by using the native HTMLElement click () method. Cool, but what about other events, like mouseenter, input, etc.? We can use the native Event APIs in conjunction with the dispatchEvent () method to trigger events programmatically. For example:


1 Answers

the answer is there: 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();
like image 195
Pierre Trollé Avatar answered Oct 22 '22 07:10

Pierre Trollé