I am trying to test a button click event. The official Angular manual says there are two ways to do this.
1.
it('should raise selected event when clicked (triggerEventHandler)', () => {
let selectedHero: Hero;
const heroDe = fixture.debugElement.query(By.css('dashboard-hero'));
comp.selected.subscribe((hero: Hero) => selectedHero = hero);
heroDe.triggerEventHandler('click', null);
expect(selectedHero).toBe(expectedHero);
});
it('should raise selected event when clicked (element.click)', () => {
let selectedHero: Hero;
const heroEl: HTMLElement = fixture.nativeElement.querySelector('.hero');
comp.selected.subscribe((hero: Hero) => selectedHero = hero);
heroEl.click();
expect(selectedHero).toBe(expectedHero);
});
The first method worked well for me. But when I tried the second option, the test ran without errors, but Karma started running tests in an infinite loop.
Here is my implementation of both ways.
it('test1', () => {
fixture.detectChanges();
let treeNodeElement = fixture.debugElement.query(By.css('#buttonSend'));
treeNodeElement.triggerEventHandler('click', null);
expect(component.tabService.tabs.length).toBe(1);
expect(component.tabService.tabs[0].title).toBe('Test1');
});
it('test2', () => {
fixture.detectChanges();
const treeNodeElement: HTMLElement = fixture.nativeElement.querySelector('#buttonSend');
treeNodeElement.click();
expect(component.tabService.tabs.length).toBe(1);
expect(component.tabService.tabs[0].title).toBe('Test1');
});
Can you please tell me where is the error in the second option?
