【发布时间】:2018-08-30 11:02:17
【问题描述】:
我有一个检测按键的函数,如果按键 = 转义,则触发一个函数。
我在伪造要传入的 KeyboardEvent 本身时遇到问题。
我看到 this post,但实施此解决方案会产生以下输出(我 console.logged 事件本身):
日志:KeyboardEvent{isTrusted: false} Chrome 68.0.3440 (Mac OS X 10.13.6) ConfirmationComponent 应在按下 ESCAPE 按钮失败时调用 onDeny 预计 spy onDeny 已被调用。
component.ts
@HostListener('window:keyup', ['$event'])
keyEvent(event: KeyboardEvent) {
console.log(event);
// Press escape - close dialog with simulated 'cancel' click
if (event.code === 'Escape') {
this.onDeny();
}
}
onDeny() {
// something is done here
}
test.ts
it('should autofocus on cancel button on init', () => {
spyOn(component, 'onDeny');
component.keyEvent(ESCAPE);
expect(component.onDeny).toHaveBeenCalled();
});
【问题讨论】:
标签: javascript angular testing karma-runner