茉莉花测试用例,用于向上和向下箭头键盘事件

时间:2018-09-06 13:02:00

标签: angular jasmine karma-jasmine

我正在尝试为自动完成组件的键盘事件编写单元测试用例。当用户按下向上键和向下键以从下拉菜单中选择值时,将调用以下功能:

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);        
});

当用户按下向上或向下键时,将arrowkeylocation计数器初始化为-1,以自动完成文本框的值。 proposalionArray是自动填充下拉列表值。

0 个答案:

没有答案