Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test.mousetrap.js « tests « mousetrap « bower_components « libs - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 591183cb61a313accb72ea82018b29f811cb558c (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* jshint es5: true, browser: true, expr: true */
/* globals describe, afterEach, chai, it, sinon, Mousetrap, KeyEvent, Event */
var expect = chai.expect;
afterEach(function() {
    Mousetrap.reset();
});

describe('Mousetrap.bind', function() {
    describe('basic', function() {
        it('z key fires when pressing z', function() {
            var spy = sinon.spy();

            Mousetrap.bind('z', spy);

            KeyEvent.simulate('Z'.charCodeAt(0), 90);

            // really slow for some reason
            // expect(spy).to.have.been.calledOnce;
            expect(spy.callCount).to.equal(1, 'callback should fire once');
            expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
            expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
        });

        it('z key fires from keydown', function() {
            var spy = sinon.spy();

            Mousetrap.bind('z', spy, 'keydown');

            KeyEvent.simulate('Z'.charCodeAt(0), 90);

            // really slow for some reason
            // expect(spy).to.have.been.calledOnce;
            expect(spy.callCount).to.equal(1, 'callback should fire once');
            expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
            expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
        });

        it('z key does not fire when pressing b', function() {
            var spy = sinon.spy();

            Mousetrap.bind('z', spy);

            KeyEvent.simulate('B'.charCodeAt(0), 66);

            expect(spy.callCount).to.equal(0);
        });

        it('z key does not fire when holding a modifier key', function() {
            var spy = sinon.spy();
            var modifiers = ['ctrl', 'alt', 'meta', 'shift'];
            var charCode;
            var modifier;

            Mousetrap.bind('z', spy);

            for (var i = 0; i < 4; i++) {
                modifier = modifiers[i];
                charCode = 'Z'.charCodeAt(0);

                // character code is different when alt is pressed
                if (modifier == 'alt') {
                    charCode = 'Ω'.charCodeAt(0);
                }

                spy.reset();

                KeyEvent.simulate(charCode, 90, [modifier]);

                expect(spy.callCount).to.equal(0);
            }
        });

        it('keyup events should fire', function() {
            var spy = sinon.spy();

            Mousetrap.bind('z', spy, 'keyup');

            KeyEvent.simulate('Z'.charCodeAt(0), 90);

            expect(spy.callCount).to.equal(1, 'keyup event for "z" should fire');

            // for key held down we should only get one key up
            KeyEvent.simulate('Z'.charCodeAt(0), 90, [], document, 10);
            expect(spy.callCount).to.equal(2, 'keyup event for "z" should fire once for held down key');
        });

        it('keyup event for 0 should fire', function() {
            var spy = sinon.spy();

            Mousetrap.bind('0', spy, 'keyup');

            KeyEvent.simulate(0, 48);

            expect(spy.callCount).to.equal(1, 'keyup event for "0" should fire');
        });

        it('rebinding a key overwrites the callback for that key', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();
            Mousetrap.bind('x', spy1);
            Mousetrap.bind('x', spy2);

            KeyEvent.simulate('X'.charCodeAt(0), 88);

            expect(spy1.callCount).to.equal(0, 'original callback should not fire');
            expect(spy2.callCount).to.equal(1, 'new callback should fire');
        });

        it('binding an array of keys', function() {
            var spy = sinon.spy();
            Mousetrap.bind(['a', 'b', 'c'], spy);

            KeyEvent.simulate('A'.charCodeAt(0), 65);
            expect(spy.callCount).to.equal(1, 'new callback was called');
            expect(spy.args[0][1]).to.equal('a', 'callback should match "a"');

            KeyEvent.simulate('B'.charCodeAt(0), 66);
            expect(spy.callCount).to.equal(2, 'new callback was called twice');
            expect(spy.args[1][1]).to.equal('b', 'callback should match "b"');

            KeyEvent.simulate('C'.charCodeAt(0), 67);
            expect(spy.callCount).to.equal(3, 'new callback was called three times');
            expect(spy.args[2][1]).to.equal('c', 'callback should match "c"');
        });

        it('return false should prevent default and stop propagation', function() {
            var spy = sinon.spy(function() {
                return false;
            });

            Mousetrap.bind('command+s', spy);

            KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);

            expect(spy.callCount).to.equal(1, 'callback should fire');
            expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
            expect(spy.args[0][0].cancelBubble).to.be.True;
            expect(spy.args[0][0].defaultPrevented).to.be.True;

            // try without return false
            spy = sinon.spy();
            Mousetrap.bind('command+s', spy);
            KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);

            expect(spy.callCount).to.equal(1, 'callback should fire');
            expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
            expect(spy.args[0][0].cancelBubble).to.be.False;
            expect(spy.args[0][0].defaultPrevented).to.be.False;
        });

        it('capslock key is ignored', function() {
            var spy = sinon.spy();
            Mousetrap.bind('a', spy);

            KeyEvent.simulate('a'.charCodeAt(0), 65);
            expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');

            spy.reset();
            KeyEvent.simulate('A'.charCodeAt(0), 65);
            expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');

            spy.reset();
            KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
            expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
        });
    });

    describe('special characters', function() {
        it('binding special characters', function() {
            var spy = sinon.spy();
            Mousetrap.bind('*', spy);

            KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);

            expect(spy.callCount).to.equal(1, 'callback should fire');
            expect(spy.args[0][1]).to.equal('*', 'callback should match *');
        });

        it('binding special characters keyup', function() {
            var spy = sinon.spy();
            Mousetrap.bind('*', spy, 'keyup');

            KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);

            expect(spy.callCount).to.equal(1, 'callback should fire');
            expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
        });

        it('binding keys with no associated charCode', function() {
            var spy = sinon.spy();
            Mousetrap.bind('left', spy);

            KeyEvent.simulate(0, 37);

            expect(spy.callCount).to.equal(1, 'callback should fire');
            expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
        });
    });

    describe('combos with modifiers', function() {
        it('binding key combinations', function() {
            var spy = sinon.spy();
            Mousetrap.bind('command+o', spy);

            KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);

            expect(spy.callCount).to.equal(1, 'command+o callback should fire');
            expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
        });

        it('binding key combos with multiple modifiers', function() {
            var spy = sinon.spy();
            Mousetrap.bind('command+shift+o', spy);
            KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
            expect(spy.callCount).to.equal(0, 'command+o callback should not fire');

            KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
            expect(spy.callCount).to.equal(1, 'command+o callback should fire');
        });
    });

    describe('sequences', function() {
        it('binding sequences', function() {
            var spy = sinon.spy();
            Mousetrap.bind('g i', spy);

            KeyEvent.simulate('G'.charCodeAt(0), 71);
            expect(spy.callCount).to.equal(0, 'callback should not fire');

            KeyEvent.simulate('I'.charCodeAt(0), 73);
            expect(spy.callCount).to.equal(1, 'callback should fire');
        });

        it('binding sequences with mixed types', function() {
            var spy = sinon.spy();
            Mousetrap.bind('g o enter', spy);

            KeyEvent.simulate('G'.charCodeAt(0), 71);
            expect(spy.callCount).to.equal(0, 'callback should not fire');

            KeyEvent.simulate('O'.charCodeAt(0), 79);
            expect(spy.callCount).to.equal(0, 'callback should not fire');

            KeyEvent.simulate(0, 13);
            expect(spy.callCount).to.equal(1, 'callback should fire');
        });

        it('binding sequences starting with modifier keys', function() {
            var spy = sinon.spy();
            Mousetrap.bind('option enter', spy);
            KeyEvent.simulate(0, 18, ['alt']);
            KeyEvent.simulate(0, 13);
            expect(spy.callCount).to.equal(1, 'callback should fire');

            spy = sinon.spy();
            Mousetrap.bind('command enter', spy);
            KeyEvent.simulate(0, 91, ['meta']);
            KeyEvent.simulate(0, 13);
            expect(spy.callCount).to.equal(1, 'callback should fire');

            spy = sinon.spy();
            Mousetrap.bind('escape enter', spy);
            KeyEvent.simulate(0, 27);
            KeyEvent.simulate(0, 13);
            expect(spy.callCount).to.equal(1, 'callback should fire');
        });

        it('key within sequence should not fire', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();
            Mousetrap.bind('a', spy1);
            Mousetrap.bind('c a t', spy2);

            KeyEvent.simulate('A'.charCodeAt(0), 65);
            expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
            spy1.reset();

            KeyEvent.simulate('C'.charCodeAt(0), 67);
            KeyEvent.simulate('A'.charCodeAt(0), 65);
            KeyEvent.simulate('T'.charCodeAt(0), 84);
            expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
            expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
        });

        it('keyup at end of sequence should not fire', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();

            Mousetrap.bind('t', spy1, 'keyup');
            Mousetrap.bind('b a t', spy2);

            KeyEvent.simulate('B'.charCodeAt(0), 66);
            KeyEvent.simulate('A'.charCodeAt(0), 65);
            KeyEvent.simulate('T'.charCodeAt(0), 84);

            expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
            expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
        });

        it('keyup sequences should work', function() {
            var spy = sinon.spy();
            Mousetrap.bind('b a t', spy, 'keyup');

            KeyEvent.simulate('b'.charCodeAt(0), 66);
            KeyEvent.simulate('a'.charCodeAt(0), 65);

            // hold the last key down for a while
            KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);

            expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
        });

        it('extra spaces in sequences should be ignored', function() {
            var spy = sinon.spy();
            Mousetrap.bind('b   a  t', spy);

            KeyEvent.simulate('b'.charCodeAt(0), 66);
            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate('t'.charCodeAt(0), 84);

            expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
        });

        it('modifiers and sequences play nicely', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();

            Mousetrap.bind('ctrl a', spy1);
            Mousetrap.bind('ctrl+b', spy2);

            KeyEvent.simulate(0, 17, ['ctrl']);
            KeyEvent.simulate('A'.charCodeAt(0), 65);
            expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');

            KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
            expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
        });

        it('sequences that start the same work', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();

            Mousetrap.bind('g g l', spy2);
            Mousetrap.bind('g g o', spy1);

            KeyEvent.simulate('g'.charCodeAt(0), 71);
            KeyEvent.simulate('g'.charCodeAt(0), 71);
            KeyEvent.simulate('o'.charCodeAt(0), 79);
            expect(spy1.callCount).to.equal(1, '"g g o" should fire');
            expect(spy2.callCount).to.equal(0, '"g g l" should not fire');

            spy1.reset();
            spy2.reset();
            KeyEvent.simulate('g'.charCodeAt(0), 71);
            KeyEvent.simulate('g'.charCodeAt(0), 71);
            KeyEvent.simulate('l'.charCodeAt(0), 76);
            expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
            expect(spy2.callCount).to.equal(1, '"g g l" should fire');
        });

        it('sequences should not fire subsequences', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();

            Mousetrap.bind('a b c', spy1);
            Mousetrap.bind('b c', spy2);

            KeyEvent.simulate('A'.charCodeAt(0), 65);
            KeyEvent.simulate('B'.charCodeAt(0), 66);
            KeyEvent.simulate('C'.charCodeAt(0), 67);

            expect(spy1.callCount).to.equal(1, '"a b c" should fire');
            expect(spy2.callCount).to.equal(0, '"b c" should not fire');

            spy1.reset();
            spy2.reset();
            Mousetrap.bind('option b', spy1);
            Mousetrap.bind('a option b', spy2);

            KeyEvent.simulate('A'.charCodeAt(0), 65);
            KeyEvent.simulate(0, 18, ['alt']);
            KeyEvent.simulate('B'.charCodeAt(0), 66);

            expect(spy1.callCount).to.equal(0, '"option b" should not fire');
            expect(spy2.callCount).to.equal(1, '"a option b" should fire');
        });

        it('rebinding same sequence should override previous', function() {
            var spy1 = sinon.spy();
            var spy2 = sinon.spy();
            Mousetrap.bind('a b c', spy1);
            Mousetrap.bind('a b c', spy2);

            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate('b'.charCodeAt(0), 66);
            KeyEvent.simulate('c'.charCodeAt(0), 67);

            expect(spy1.callCount).to.equal(0, 'first callback should not fire');
            expect(spy2.callCount).to.equal(1, 'second callback should fire');
        });

        it('broken sequences', function() {
            var spy = sinon.spy();
            Mousetrap.bind('h a t', spy);

            KeyEvent.simulate('h'.charCodeAt(0), 72);
            KeyEvent.simulate('e'.charCodeAt(0), 69);
            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate('r'.charCodeAt(0), 82);
            KeyEvent.simulate('t'.charCodeAt(0), 84);

            expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
        });

        it('sequences containing combos should work', function() {
            var spy = sinon.spy();
            Mousetrap.bind('a ctrl+b', spy);

            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);

            expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');

            Mousetrap.unbind('a ctrl+b');

            spy = sinon.spy();
            Mousetrap.bind('ctrl+b a', spy);

            KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
            KeyEvent.simulate('a'.charCodeAt(0), 65);

            expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
        });

        it('sequences starting with spacebar should work', function() {
            var spy = sinon.spy();
            Mousetrap.bind('a space b c', spy);

            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate(32, 32);
            KeyEvent.simulate('b'.charCodeAt(0), 66);
            KeyEvent.simulate('c'.charCodeAt(0), 67);

            expect(spy.callCount).to.equal(1, '"a space b c" should fire');
        });

        it('konami code', function() {
            var spy = sinon.spy();
            Mousetrap.bind('up up down down left right left right b a enter', spy);

            KeyEvent.simulate(0, 38);
            KeyEvent.simulate(0, 38);
            KeyEvent.simulate(0, 40);
            KeyEvent.simulate(0, 40);
            KeyEvent.simulate(0, 37);
            KeyEvent.simulate(0, 39);
            KeyEvent.simulate(0, 37);
            KeyEvent.simulate(0, 39);
            KeyEvent.simulate('b'.charCodeAt(0), 66);
            KeyEvent.simulate('a'.charCodeAt(0), 65);
            KeyEvent.simulate(0, 13);

            expect(spy.callCount).to.equal(1, 'konami code should fire');
        });

        it('sequence timer resets', function() {
            var spy = sinon.spy();
            var clock = sinon.useFakeTimers();

            Mousetrap.bind('h a t', spy);

            KeyEvent.simulate('h'.charCodeAt(0), 72);
            clock.tick(600);
            KeyEvent.simulate('a'.charCodeAt(0), 65);
            clock.tick(900);
            KeyEvent.simulate('t'.charCodeAt(0), 84);

            expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
            clock.restore();
        });

        it('sequences timeout', function() {
            var spy = sinon.spy();
            var clock = sinon.useFakeTimers();

            Mousetrap.bind('g t', spy);
            KeyEvent.simulate('g'.charCodeAt(0), 71);
            clock.tick(1000);
            KeyEvent.simulate('t'.charCodeAt(0), 84);

            expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
            clock.restore();
        });
    });

    describe('default actions', function() {
        var keys = {
            keypress: [
                ['a', 65],
                ['A', 65, ['shift']],
                ['7', 55],
                ['?', 191],
                ['*', 56],
                ['+', 187],
                ['$', 52],
                ['[', 219],
                ['.', 190]
            ],
            keydown: [
                ['shift+\'', 222, ['shift']],
                ['shift+a', 65, ['shift']],
                ['shift+5', 53, ['shift']],
                ['command+shift+p', 80, ['meta', 'shift']],
                ['space', 32],
                ['left', 37]
            ]
        };

        function getCallback(key, keyCode, type, modifiers) {
            return function() {
                var spy = sinon.spy();
                Mousetrap.bind(key, spy);

                KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
                expect(spy.callCount).to.equal(1);
                expect(spy.args[0][0].type).to.equal(type);
            };
        }

        for (var type in keys) {
            for (var i = 0; i < keys[type].length; i++) {
                var key = keys[type][i][0];
                var keyCode = keys[type][i][1];
                var modifiers = keys[type][i][2] || [];
                it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
            }
        }
    });
});

describe('Mousetrap.unbind', function() {
    it('unbind works', function() {
        var spy = sinon.spy();
        Mousetrap.bind('a', spy);
        KeyEvent.simulate('a'.charCodeAt(0), 65);
        expect(spy.callCount).to.equal(1, 'callback for a should fire');

        Mousetrap.unbind('a');
        KeyEvent.simulate('a'.charCodeAt(0), 65);
        expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
    });

    it('unbind accepts an array', function() {
        var spy = sinon.spy();
        Mousetrap.bind(['a', 'b', 'c'], spy);
        KeyEvent.simulate('a'.charCodeAt(0), 65);
        KeyEvent.simulate('b'.charCodeAt(0), 66);
        KeyEvent.simulate('c'.charCodeAt(0), 67);
        expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');

        Mousetrap.unbind(['a', 'b', 'c']);
        KeyEvent.simulate('a'.charCodeAt(0), 65);
        KeyEvent.simulate('b'.charCodeAt(0), 66);
        KeyEvent.simulate('c'.charCodeAt(0), 67);
        expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
    });
});