forked from Jaware/angular-hotkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotkeysSpec.js
213 lines (178 loc) · 5.63 KB
/
HotkeysSpec.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
describe('ParseKey expression parser', function() {
var parse, userAgent;
beforeEach(module('drahak.hotkeys'));
beforeEach(inject(function(ParseKey, $window) {
parse = ParseKey;
userAgent = $window.navigator.userAgent.toLowerCase();
}));
it('parses string into keycodes array', function() {
var keys = parse('B + A');
expect(keys).toEqual([66, 65]);
});
it('parses control key', function() {
var ctrl = parse('Ctrl');
var control = parse('Control');
expect(ctrl).toEqual([17]);
expect(control).toEqual([17]);
});
it('parses shift key', function() {
var shift = parse('Shift');
var lowercase = parse('shift');
expect(shift).toEqual([16]);
expect(lowercase).toEqual([16]);
});
it('parses alt key', function() {
var alt = parse('Alt');
var lowercase = parse('alt');
expect(alt).toEqual([18]);
expect(lowercase).toEqual([18]);
});
it('parses escape key', function() {
var esc = parse('Esc');
var escape = parse('Escape');
expect(esc).toEqual([27]);
expect(escape).toEqual([27]);
});
it('parses enter key', function() {
var enter = parse('Enter');
expect(enter).toEqual([13]);
});
it('parses tab key', function() {
var tab = parse('Tab');
var tabulator = parse('Tabulator');
expect(tab).toEqual([9]);
expect(tabulator).toEqual([9]);
});
it('parses delete key', function() {
var del = parse('Del');
var deleteKey = parse('Delete');
expect(del).toEqual([46]);
expect(deleteKey).toEqual([46]);
});
it('parses insert key', function() {
var insert = parse('Insert');
expect(insert).toEqual([45]);
});
it('parses arrow keys', function() {
var arrows = parse('left + up + right + down');
expect(arrows).toEqual([37, 38, 39, 40]);
});
it('fails if one expression is invalid', function() {
var trigger = function() {
parse('left + esc + a + some + right');
};
expect(trigger).toThrow();
});
it('parses command key', function() {
var commandKeyCode = userAgent.indexOf('firefox') > -1 ? 224 : (userAgent.indexOf('opera') > -1 ? 17 : 91);
var command = parse('Command');
var cmd = parse('Cmd');
expect(cmd).toEqual([commandKeyCode]);
expect(command).toEqual([commandKeyCode]);
});
});
describe('HotKey event manager', function() {
var hotKey;
beforeEach(module('drahak.hotkeys'));
beforeEach(inject(function(HotKeys) {
hotKey = HotKeys();
}));
it('triggers registered event handler', function() {
var handler = jasmine.createSpy();
hotKey.bind('Ctrl + S', handler);
hotKey.trigger('Ctrl + S');
expect(handler).toHaveBeenCalled();
});
it('removes registered event handler', function() {
var handler = jasmine.createSpy();
hotKey.bind('Ctrl + S', handler);
hotKey.unbind('S + Ctrl');
hotKey.trigger('Ctrl + S');
expect(handler).not.toHaveBeenCalled();
});
it('registers hot key using key codes array', function() {
var handler = jasmine.createSpy();
hotKey.bind([13], handler);
hotKey.trigger('Enter');
expect(handler).toHaveBeenCalled();
});
it('throws error if invalid hot key is given', function() {
var callback = function() {
hotKey.bind(13, function(){});
};
expect(callback).toThrow();
});
});
describe('HotKey element', function() {
var scope = null;
var element = null;
var hotKeys = null;
beforeEach(module('drahak.hotkeys'));
beforeEach(function() {
module({
HotKeys: function() {
return {
trigger: jasmine.createSpy()
}
}
});
inject(function(HotKeysElement, $window) {
scope = {};
element = angular.element($window);
spyOn(element, 'scope').andReturn(scope);
hotKeys = HotKeysElement(element);
});
});
it('adds HotKeys service to element scope as $hotKeys', function() {
expect(element.scope().$hotKeys).toBe(hotKeys);
});
it('triggers hotkey on key down', function() {
element.triggerHandler('keydown');
expect(hotKeys.trigger).toHaveBeenCalled();
});
});
describe('Hotkey directive', function() {
var $compile;
var $rootScope;
var hotKeyElement;
beforeEach(module('drahak.hotkeys'));
beforeEach(function() {
var mockHotKey = function() {
return { bind: jasmine.createSpy() };
};
hotKeyElement = mockHotKey();
module({
$hotkey: mockHotKey(),
HotKeysElement: function() {
return hotKeyElement;
}
});
inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
});
});
it('binds event from hotkey and invoke attributes to hotkey event manager', function() {
$compile('<div hotkey="Ctrl + S" invoke="test()"></div>')($rootScope);
expect(hotKeyElement.bind).toHaveBeenCalledWith('Ctrl + S', jasmine.any(Function));
});
it('binds events from hotkey attribute if we pass object', function() {
$rootScope.handler = function() {};
$rootScope.handlerToo = function() {};
$compile('<div hotkey="{ \'Ctrl + S\': handler, \'Shift + J\': handlerToo }"></div>')($rootScope);
expect(hotKeyElement.bind).toHaveBeenCalledWith('Ctrl + S', $rootScope.handler);
expect(hotKeyElement.bind).toHaveBeenCalledWith('Shift + J', $rootScope.handlerToo);
});
it('binds events to global scope', inject(function($hotkey) {
$compile('<hotkey bind="Ctrl + A" invoke="handler"></hotkey>')($rootScope);
expect($hotkey.bind).toHaveBeenCalledWith('Ctrl + A', jasmine.any(Function));
}));
it('binds events to global scope using object in bind attribute', inject(function($hotkey) {
$rootScope.handler = function() {};
$rootScope.handlerToo = function() {};
$compile('<hotkey bind="{ \'Ctrl + S\': handler, \'Shift + J\': handlerToo }"></hotkey>')($rootScope);
expect($hotkey.bind).toHaveBeenCalledWith('Ctrl + S', $rootScope.handler);
expect($hotkey.bind).toHaveBeenCalledWith('Shift + J', $rootScope.handlerToo);
}));
});