我有一个通过keydown.enter
触发的函数,它将专注于下一个可用元素,但是我无法通过测试接受我的假KeyboardEvent ...
@ViewChild('btnOne') btnOne: ElementRef<HTMLInputElement>;
@ViewChild('btnTwo') btnTwo: ElementRef<HTMLInputElement>;
myFunct(evt: KeyboardEvent) {
const next = <HTMLElement>evt.srcElement.nextElementSibling;
next.focus();
}
测试:
it('Should shift focus from btnOne to btnTwo', () => {
const b1 = component.btnOne.nativeElement;
const b2 = component.btnTwo.nativeElement;
const button2 = {nodeName:'BUTTON'}
const button1 = {nodeName:'BUTTON', nextElementSibling: button2};
const evtObj = {'code': 'Enter', srcElement: button1);
const kbEvent = new KeyboardEvent('keydown.enter', evtObj);
focusSpy = spyOn(b2, 'focus'); // Spy on focus for 2nd button to be focused
b1.focus(); // Manually apply focus to 1st button so it will progress from here
component.myFunct(kbEvent); // I get the failures here (see below)
expect(b2.focus).toHaveBeenCalled();
});
带有以下消息的测试错误:
TypeError:无法读取null的属性“ nextElementSibling”
我还打印了evt
,因为它是从测试到函数本身的信息:
KeyboardEvent {isTrusted:false}
我已经搜索了isTrusted
个问题并找到了this question,但是我无法用他们的答案解决问题...