1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
describe('uiKeypress', function () {
var $scope, $compile;
var createKeyEvent = function (mainKey, alt, ctrl, shif) {
var keyEvent = jQuery.Event("keypress");
keyEvent.keyCode = mainKey;
keyEvent.altKey = alt || false;
keyEvent.ctrlKey = ctrl || false;
keyEvent.shiftKey = shif || false;
return keyEvent;
};
var createElement = function (elementDef) {
var elementStr = angular.isString(elementDef) ? elementDef : angular.toJson(elementDef);
return $compile("<span ui-keypress='" + elementStr + "'></span>")($scope);
};
beforeEach(module('ui.directives'));
beforeEach(inject(function (_$rootScope_, _$compile_) {
$compile = _$compile_;
$scope = _$rootScope_.$new();
$scope.cb = function (event) {
this.event1 = event;
};
}));
it('should support single key press', function () {
createElement({'13': 'event=true'}).trigger(createKeyEvent(13));
expect($scope.event).toBe(true);
});
it('should support combined key press', function () {
createElement({'ctrl-shift-13': 'event=true'}).trigger(createKeyEvent(13, false, true, true));
expect($scope.event).toBe(true);
});
it('should support alternative combinations', function () {
$scope.event = 0;
createElement({'ctrl-shift-14 ctrl-shift-13': 'event=event+1'}).trigger(createKeyEvent(13, false, true, true)).trigger(createKeyEvent(14, false, true, true));
expect($scope.event).toBe(2);
});
it('should support multiple key press definitions', function () {
var elm = createElement({'13': 'event1=true', 'ctrl-shift-13': 'event2=true'});
elm.trigger(createKeyEvent(13));
expect($scope.event1).toBe(true);
elm.trigger(createKeyEvent(13, false, true, true));
expect($scope.event2).toBe(true);
});
it('should support $event in expressions', function () {
var element = createElement({'esc': 'cb($event)', '13': 'event2=$event'});
element.trigger(createKeyEvent(27));
expect($scope.event1.keyCode).toBe(27);
element.trigger(createKeyEvent(13));
expect($scope.event2.keyCode).toBe(13);
});
});
|