This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
test.js
120 lines (98 loc) · 2.76 KB
/
test.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
(function($) {
$(function() {
test('bind', function() {
expect(2);
var target = $('<div />');
deepEqual(target.bind('keydown', 'A', $.noop), target);
deepEqual(target.bind('keyup', 'A', $.noop), target);
});
test('unbind', function() {
expect(2);
var target = $('<div />');
target.bind('keydown', 'A', $.noop);
target.bind('keyup', 'A', $.noop);
deepEqual(target.unbind('keydown', 'A', $.noop), target);
deepEqual(target.unbind('keyup', 'A', $.noop), target);
});
test('trigger', function() {
expect(1);
var target = $('<div />');
var f = function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'A');
};
target.bind('keydown', 'A', f);
var e = $.Event('keydown');
e.keyCode = 'A'.charCodeAt();
target.trigger(e);
target.unbind('keydown', f).trigger(e);
});
test('shiftKey', function() {
expect(2);
var target = $('<div />');
target.bind('keydown', 'S-A', function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'A');
ok(e.shiftKey, 'shiftKey');
});
var e = $.Event('keydown');
e.keyCode = 'A'.charCodeAt();
e.shiftKey = true;
target.trigger(e);
});
test('ctrlKey', function() {
expect(2);
var target = $('<div />');
target.bind('keydown', 'C-A', function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'A');
ok(e.ctrlKey, 'ctrlKey');
});
var e = $.Event('keydown');
e.keyCode = 'A'.charCodeAt();
e.ctrlKey = true;
target.trigger(e);
});
test('altKey', function() {
expect(2);
var target = $('<div />');
target.bind('keydown', 'A-A', function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'A');
ok(e.altKey, 'altKey');
});
var e = $.Event('keydown');
e.keyCode = 'A'.charCodeAt();
e.altKey = true;
target.trigger(e);
});
test('multiple strokes', function() {
expect(1);
var target = $('<div />');
target.bind('keydown', 'A B C', function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'C');
});
var e1 = $.Event('keydown');
e1.keyCode = 'A'.charCodeAt();
var e2 = $.Event('keydown');
e2.keyCode = 'B'.charCodeAt();
var e3 = $.Event('keydown');
e3.keyCode = 'C'.charCodeAt();
target.trigger(e1);
target.trigger(e2);
target.trigger(e3);
});
test('branch', function() {
expect(1);
var target = $('<div />');
target.bind('keydown', 'A B', function(e) {
deepEqual(String.fromCharCode(e.keyCode), 'B');
});
target.bind('keydown', 'A C', function(e) {
ok(false);
});
var e1 = $.Event('keydown');
e1.keyCode = 'A'.charCodeAt();
var e2 = $.Event('keydown');
e2.keyCode = 'B'.charCodeAt();
target.trigger(e1);
target.trigger(e2);
});
});
})(jQuery);