I need to test if a focus is set correctly, after pressing the TAB button.
The test must be done with karma/jasmine.
I looked already to this answers here, but it didnt help.
StackOverflow - Unable to simulate keypress event in Angular 2 unit test (Jasmine)
StackOverflow - how-do-i-trigger-a-keyup-keydown-event-in-an-angularjs-unit-test
When pressing Tab, the focus should be on the second button element.
Here is a Stackblitz Demo
<div>
<button>1</button>
<button>2</button>
</div>
unit Test
fit('press TAB: should select button "2"', () => {
const firstButton = el.queryAll(By.css('button'))[0].nativeElement;
const secondButton = el.queryAll(By.css('button'))[1].nativeElement;
spyOn(secondButton, "focus");
const pressTAB = new KeyboardEvent("keypress",{"key": "Tab"});
firstButton.dispatchEvent(pressTAB);
fixture.detectChanges();
expect(secondButton.focus).toHaveBeenCalled();
});