angular - 用于上下箭头键盘事件的 Jasmine 测试用例
问题描述
我正在尝试为自动完成组件的键盘事件编写单元测试用例。当用户按下 keyup 和 keydown 从下拉列表中选择值时,将调用以下函数:
onKeyDown(event:KeyboardEvent){
if(event.which !== 40 && event.which !== 38){
return;
}
event.stopPropagation();
if (event.which === 40 && this.arrowkeylocation < this.suggestionArray.length - 1) {
// Arrow Down
this.arrowkeylocation++;
this.textSearch = this.suggestionArray[this.arrowkeylocation].title;
} else if (event.which === 38 && this.arrowkeylocation > 0) {
// Arrow Up
this.arrowkeylocation--;
this.textSearch = this.suggestionArray[this.arrowkeylocation].title;
}
}
以下是不起作用的测试用例:
it('should call onKeyDown 40 event', () => {
const keyevent = {keyCode: 40};
component.suggestionArray = [suggest,suggest];
inputEl.triggerEventHandler('keydown',keyevent);
expect(component.onKeyDown).toBeTruthy();
expect(component.arrowkeylocation).toEqual(0);
expect(component.textSearch).toEqual(component.suggestionArray[0].title);
});
当用户按下 keyup 或 keydown 时,arrowkeylocation 计数器初始化为 -1 以自动完成文本框值。建议数组是自动完成下拉值。
解决方案
推荐阅读
- nagios - 如何在 Thruk 中隐藏密码?
- conditional-statements - 使用行数的 Conga 条件语句
- api - 将实体数据模型(edmx 文件)添加到 .net 核心 API
- python - 如何在 setuptools 中更改构建目录
- php - “/var/www/html/”的Nginx目录索引被禁止
- java - 有没有办法异步使用来自 RabbitMQ 主题的消息?
- lighthouse - 使用内容可见性属性会影响累积布局偏移 (CLS) 指标吗?
- react-native - 你如何让 React Native Flash 消息显示
- java - 试图在java中用石英制作每日工作计划
- flutter - 在颤动中关闭包含 DraggableScrollableSheet 的 BottomSheet