[AngularJS Unit tesint] Testing keyboard event
HTML:
<div ng-focus="vm.onFocus(month)",
aria-focus="{{vm.focus == month}}",
ng-keydown="vm.onKeydown($event, month)">
Component Controller:
onKeydown(e, num) {
switch(e.which) {
case : // enter
case : {
// Space
e.preventDefault();
this.select(+num);
break;
}
case : {
// up
e.preventDefault();
this.onUp(this.focus);
break;
}
case : {
// right
e.preventDefault();
this.onRight(this.focus);
break;
}
case : {
// left
e.preventDefault();
this.onLeft(this.focus);
break;
}
case : {
//down
e.preventDefault();
this.onDown(this.focus);
break;
}
default: {
angular.noop();
}
}
}
onFocus(num) {
this.focus = +num;
}
onUp(crtFocus) {
if (crtFocus === null) {
return;
}
const newFocus = crtFocus - <= this._MIN_MONTH ? this._MIN_MONTH : crtFocus - ;
this.onFocus(newFocus);
}
onDown(crtFocus) {
if (crtFocus === null) {
return;
}
const newFocus = crtFocus + >= this._MAX_MONTH ? this._MAX_MONTH : crtFocus + ;
this.onFocus(newFocus);
}
onRight(crtFocus) {
if (crtFocus === null) {
return;
}
const newFocus = crtFocus + >= this._MAX_MONTH ? this._MAX_MONTH : crtFocus + ;
this.onFocus(newFocus);
}
onLeft(crtFocus) {
if (crtFocus === null) {
return;
}
const newFocus = crtFocus - <= this._MIN_MONTH ? this._MIN_MONTH : crtFocus - ;
this.onFocus(newFocus);
}
onBlur() {
this.focus = null;
}
Testing:
describe('keyboard control', () => {
const UNKNOWN = 10, ENTER = 13, SPACE = 32, UP = 38, RIGHT = 39, LEFT = 37, DOWN = 40;
it('onKeydown should be called when recevice on keydown event', () => {
spyOn(ctrl, 'onKeydown');
angular.element(firstTile).triggerHandler({type: 'keydown', which: ENTER});
$scope.$digest();
expect(ctrl.onKeydown).toHaveBeenCalled();
});
it('select function should be called when receive "Enter" keydown events', () => {
spyOn(ctrl, 'select').and.callThrough();
angular.element(firstTile).triggerHandler({type: 'keydown', which: ENTER});
$scope.$digest();
expect(ctrl.select).toHaveBeenCalledWith(1);
});
it('select function should be called when receive "Space" keydown events', () => {
spyOn(ctrl, 'select').and.callThrough();
angular.element(firstTile).triggerHandler({type: 'keydown', which: SPACE});
$scope.$digest();
expect(ctrl.select).toHaveBeenCalledWith(1);
});
it('onUp function should be called when UP keydown triggered', () => {
spyOn(ctrl, 'onUp').and.callThrough();
ctrl.focus = 12;
const expected = ctrl.focus - 4;
angular.element(lastTile).triggerHandler({type: 'keydown', which: UP});
$scope.$digest();
expect(ctrl.onUp).toHaveBeenCalledWith(12);
expect(ctrl.focus).toEqual(expected);
});
it('if current focus is null, onFocus should not be called', () => {
spyOn(ctrl, 'onFocus');
expect(ctrl.focus).toBe(null);
angular.element(lastTile).triggerHandler({type: 'keydown', which: UP});
$scope.$digest();
expect(ctrl.onFocus).not.toHaveBeenCalled();
});
it('onLeft function should be called when UP keydown triggered', () => {
spyOn(ctrl, 'onLeft').and.callThrough();
ctrl.focus = 12;
const expected = ctrl.focus - 1;
angular.element(lastTile).triggerHandler({type: 'keydown', which: LEFT});
$scope.$digest();
expect(ctrl.onLeft).toHaveBeenCalledWith(12);
expect(ctrl.focus).toEqual(expected);
});
it('onRight function should be called when UP keydown triggered', () => {
spyOn(ctrl, 'onRight').and.callThrough();
ctrl.focus = 1;
const expected = ctrl.focus + 1;
angular.element(firstTile).triggerHandler({type: 'keydown', which: RIGHT});
$scope.$digest();
expect(ctrl.onRight).toHaveBeenCalledWith(1);
expect(ctrl.focus).toEqual(expected);
});
it('onDown function should be called when UP keydown triggered', () => {
spyOn(ctrl, 'onDown').and.callThrough();
ctrl.focus = 1;
const expected = ctrl.focus + 4;
angular.element(firstTile).triggerHandler({type: 'keydown', which: DOWN});
$scope.$digest();
expect(ctrl.onDown).toHaveBeenCalledWith(1);
expect(ctrl.focus).toEqual(expected);
});
it('should only trigger angular.noop() function when other keycode keydown event trigger', () => {
spyOn(angular, 'noop');
angular.element(firstTile).triggerHandler({type: 'keydown', which: UNKNOWN});
$scope.$digest();
expect(angular.noop).toHaveBeenCalled();
});
});
[AngularJS Unit tesint] Testing keyboard event的更多相关文章
- [Unit Testing] AngularJS Unit Testing - Karma
Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...
- [Angular + Unit] AngularJS Unit testing using Karma
http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...
- [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope
<div> <h2>{{vm.userInfo.number}} - {{vm.userInfo.name}}</h2> </div> 'use str ...
- [AngularJS + Unit Testing] Testing a component with requiring ngModel
The component test: describe('The component test', () => { let component, $componentController, $ ...
- Run Unit API Testing Which Was Distributed To Multiple Test Agents
Recently I am blocked by a very weird issue, from the VS installed machine, I can run performance te ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- AngularJS unit test report / coverage report
参考来源: http://www.cnblogs.com/vipyoumay/p/5331787.html 这篇是学习基于Angularjs的nodejs平台的单元测试报告和覆盖率报告.用到的都是现有 ...
- c++ get keyboard event
#include <string> #include <iostream> #include "windows.h" #include <conio. ...
- angularJS学习资源最全汇总
基础 官方: http://docs.angularjs.org angularjs官方网站已被墙,可看 http://www.ngnice.com/: 官方zip下载包 https://github ...
随机推荐
- python入门(一)作业
一,流程控制之if...else 1. 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小妞 age_of_girl = 21 if age_of_girl >30: print('阿姨' ...
- 【php】 给数组重建索引
array_values $a = [1,2]; unset($a[1]); $a[] = 3; print_r($a); // 输出 [0=>1,2=>3]
- 爬虫必备:Python 执行 JS 代码 —— PyExecJS、PyV8、Js2Py
在使用爬虫中,经常会遇到网页请求数据是经过 JS 处理的,特别是模拟登录时可能有加密请求.而目前绝大部分前端 JS 代码都是经过混淆的,可读性极低,想理解代码逻辑需要花费大量时间.这时不要着急使用 S ...
- URI URL URN 关系
我们一起来看下面这个虚构的例子.这是一个URI: http://bitpoetry.io/posts/hello.html#intro 我们开始分析 http:// 是定义如何访问资源的方式.另外 b ...
- 编辑器sublime(转)摘自网络
一.下载和安装 Sublime Text2是一款开源的软件,不需要注册即可使用(虽然没有注册会有弹窗,但是基本不影响使用). 下载地址:http://www.sublimetext.com/,请自行根 ...
- joyoi2020/lfyzoj114 Rainbow 的信号
位与位间互不影响.一位一位计算. 长度为 \(1\) 的区间,选出概率为 \(1/n^2\).其余区间,选出概率为 \(2/n^2\).(这里的区间 \(l \leq r\)) 枚举右端点.记 \(l ...
- 学习笔记7——wp版本更新需要注意的问题
平时开发时应该避免修改wp的核心代码, 因为在升级wp版本时,核心代码都会被覆盖, wp升级时只有wp-content文件夹不会被覆盖.
- BNUOJ 4215 最长公共连续子序列
最长公共连续子序列 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...
- 编辑被标记为“只读”的Word文档
从邮件接收到的Word文档,打开时总是被标记为“只读”,在阅读时对其进行编辑,但不能保存,会提示文档为只读的.要想对其进行编辑并保存,需要进行一定的操作. 进入文件所在的目录,鼠标右键点击Word文档 ...
- python 列表 字符串 转换
列表转字符串python中的列表l = ['1','2','3','4']转成str型'1,2,3,4'','.join(l)这个方法,列表里都是字符串的话可以这样用.列表里是整数的情况可以用: &g ...