test.mousetrap.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* jshint es5: true, browser: true, expr: true */
  2. /* globals describe, afterEach, chai, it, sinon, Mousetrap, KeyEvent, Event */
  3. var expect = chai.expect;
  4. afterEach(function() {
  5. Mousetrap.reset();
  6. });
  7. describe('Mousetrap.bind', function() {
  8. describe('basic', function() {
  9. it('z key fires when pressing z', function() {
  10. var spy = sinon.spy();
  11. Mousetrap.bind('z', spy);
  12. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  13. // really slow for some reason
  14. // expect(spy).to.have.been.calledOnce;
  15. expect(spy.callCount).to.equal(1, 'callback should fire once');
  16. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  17. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  18. });
  19. it('z key fires from keydown', function() {
  20. var spy = sinon.spy();
  21. Mousetrap.bind('z', spy, 'keydown');
  22. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  23. // really slow for some reason
  24. // expect(spy).to.have.been.calledOnce;
  25. expect(spy.callCount).to.equal(1, 'callback should fire once');
  26. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  27. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  28. });
  29. it('z key does not fire when pressing b', function() {
  30. var spy = sinon.spy();
  31. Mousetrap.bind('z', spy);
  32. KeyEvent.simulate('B'.charCodeAt(0), 66);
  33. expect(spy.callCount).to.equal(0);
  34. });
  35. it('z key does not fire when holding a modifier key', function() {
  36. var spy = sinon.spy();
  37. var modifiers = ['ctrl', 'alt', 'meta', 'shift'];
  38. var charCode;
  39. var modifier;
  40. Mousetrap.bind('z', spy);
  41. for (var i = 0; i < 4; i++) {
  42. modifier = modifiers[i];
  43. charCode = 'Z'.charCodeAt(0);
  44. // character code is different when alt is pressed
  45. if (modifier == 'alt') {
  46. charCode = 'Ω'.charCodeAt(0);
  47. }
  48. spy.reset();
  49. KeyEvent.simulate(charCode, 90, [modifier]);
  50. expect(spy.callCount).to.equal(0);
  51. }
  52. });
  53. it('keyup events should fire', function() {
  54. var spy = sinon.spy();
  55. Mousetrap.bind('z', spy, 'keyup');
  56. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  57. expect(spy.callCount).to.equal(1, 'keyup event for "z" should fire');
  58. // for key held down we should only get one key up
  59. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], document, 10);
  60. expect(spy.callCount).to.equal(2, 'keyup event for "z" should fire once for held down key');
  61. });
  62. it('keyup event for 0 should fire', function() {
  63. var spy = sinon.spy();
  64. Mousetrap.bind('0', spy, 'keyup');
  65. KeyEvent.simulate(0, 48);
  66. expect(spy.callCount).to.equal(1, 'keyup event for "0" should fire');
  67. });
  68. it('rebinding a key overwrites the callback for that key', function() {
  69. var spy1 = sinon.spy();
  70. var spy2 = sinon.spy();
  71. Mousetrap.bind('x', spy1);
  72. Mousetrap.bind('x', spy2);
  73. KeyEvent.simulate('X'.charCodeAt(0), 88);
  74. expect(spy1.callCount).to.equal(0, 'original callback should not fire');
  75. expect(spy2.callCount).to.equal(1, 'new callback should fire');
  76. });
  77. it('binding an array of keys', function() {
  78. var spy = sinon.spy();
  79. Mousetrap.bind(['a', 'b', 'c'], spy);
  80. KeyEvent.simulate('A'.charCodeAt(0), 65);
  81. expect(spy.callCount).to.equal(1, 'new callback was called');
  82. expect(spy.args[0][1]).to.equal('a', 'callback should match "a"');
  83. KeyEvent.simulate('B'.charCodeAt(0), 66);
  84. expect(spy.callCount).to.equal(2, 'new callback was called twice');
  85. expect(spy.args[1][1]).to.equal('b', 'callback should match "b"');
  86. KeyEvent.simulate('C'.charCodeAt(0), 67);
  87. expect(spy.callCount).to.equal(3, 'new callback was called three times');
  88. expect(spy.args[2][1]).to.equal('c', 'callback should match "c"');
  89. });
  90. it('return false should prevent default and stop propagation', function() {
  91. var spy = sinon.spy(function() {
  92. return false;
  93. });
  94. Mousetrap.bind('command+s', spy);
  95. KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
  96. expect(spy.callCount).to.equal(1, 'callback should fire');
  97. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  98. expect(spy.args[0][0].cancelBubble).to.be.True;
  99. expect(spy.args[0][0].defaultPrevented).to.be.True;
  100. // try without return false
  101. spy = sinon.spy();
  102. Mousetrap.bind('command+s', spy);
  103. KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
  104. expect(spy.callCount).to.equal(1, 'callback should fire');
  105. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  106. expect(spy.args[0][0].cancelBubble).to.be.False;
  107. expect(spy.args[0][0].defaultPrevented).to.be.False;
  108. });
  109. it('capslock key is ignored', function() {
  110. var spy = sinon.spy();
  111. Mousetrap.bind('a', spy);
  112. KeyEvent.simulate('a'.charCodeAt(0), 65);
  113. expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');
  114. spy.reset();
  115. KeyEvent.simulate('A'.charCodeAt(0), 65);
  116. expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');
  117. spy.reset();
  118. KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
  119. expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
  120. });
  121. });
  122. describe('special characters', function() {
  123. it('binding special characters', function() {
  124. var spy = sinon.spy();
  125. Mousetrap.bind('*', spy);
  126. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  127. expect(spy.callCount).to.equal(1, 'callback should fire');
  128. expect(spy.args[0][1]).to.equal('*', 'callback should match *');
  129. });
  130. it('binding special characters keyup', function() {
  131. var spy = sinon.spy();
  132. Mousetrap.bind('*', spy, 'keyup');
  133. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  134. expect(spy.callCount).to.equal(1, 'callback should fire');
  135. expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
  136. });
  137. it('binding keys with no associated charCode', function() {
  138. var spy = sinon.spy();
  139. Mousetrap.bind('left', spy);
  140. KeyEvent.simulate(0, 37);
  141. expect(spy.callCount).to.equal(1, 'callback should fire');
  142. expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
  143. });
  144. });
  145. describe('combos with modifiers', function() {
  146. it('binding key combinations', function() {
  147. var spy = sinon.spy();
  148. Mousetrap.bind('command+o', spy);
  149. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  150. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  151. expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
  152. });
  153. it('binding key combos with multiple modifiers', function() {
  154. var spy = sinon.spy();
  155. Mousetrap.bind('command+shift+o', spy);
  156. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  157. expect(spy.callCount).to.equal(0, 'command+o callback should not fire');
  158. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
  159. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  160. });
  161. });
  162. describe('sequences', function() {
  163. it('binding sequences', function() {
  164. var spy = sinon.spy();
  165. Mousetrap.bind('g i', spy);
  166. KeyEvent.simulate('G'.charCodeAt(0), 71);
  167. expect(spy.callCount).to.equal(0, 'callback should not fire');
  168. KeyEvent.simulate('I'.charCodeAt(0), 73);
  169. expect(spy.callCount).to.equal(1, 'callback should fire');
  170. });
  171. it('binding sequences with mixed types', function() {
  172. var spy = sinon.spy();
  173. Mousetrap.bind('g o enter', spy);
  174. KeyEvent.simulate('G'.charCodeAt(0), 71);
  175. expect(spy.callCount).to.equal(0, 'callback should not fire');
  176. KeyEvent.simulate('O'.charCodeAt(0), 79);
  177. expect(spy.callCount).to.equal(0, 'callback should not fire');
  178. KeyEvent.simulate(0, 13);
  179. expect(spy.callCount).to.equal(1, 'callback should fire');
  180. });
  181. it('binding sequences starting with modifier keys', function() {
  182. var spy = sinon.spy();
  183. Mousetrap.bind('option enter', spy);
  184. KeyEvent.simulate(0, 18, ['alt']);
  185. KeyEvent.simulate(0, 13);
  186. expect(spy.callCount).to.equal(1, 'callback should fire');
  187. spy = sinon.spy();
  188. Mousetrap.bind('command enter', spy);
  189. KeyEvent.simulate(0, 91, ['meta']);
  190. KeyEvent.simulate(0, 13);
  191. expect(spy.callCount).to.equal(1, 'callback should fire');
  192. spy = sinon.spy();
  193. Mousetrap.bind('escape enter', spy);
  194. KeyEvent.simulate(0, 27);
  195. KeyEvent.simulate(0, 13);
  196. expect(spy.callCount).to.equal(1, 'callback should fire');
  197. });
  198. it('key within sequence should not fire', function() {
  199. var spy1 = sinon.spy();
  200. var spy2 = sinon.spy();
  201. Mousetrap.bind('a', spy1);
  202. Mousetrap.bind('c a t', spy2);
  203. KeyEvent.simulate('A'.charCodeAt(0), 65);
  204. expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
  205. spy1.reset();
  206. KeyEvent.simulate('C'.charCodeAt(0), 67);
  207. KeyEvent.simulate('A'.charCodeAt(0), 65);
  208. KeyEvent.simulate('T'.charCodeAt(0), 84);
  209. expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
  210. expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
  211. });
  212. it('keyup at end of sequence should not fire', function() {
  213. var spy1 = sinon.spy();
  214. var spy2 = sinon.spy();
  215. Mousetrap.bind('t', spy1, 'keyup');
  216. Mousetrap.bind('b a t', spy2);
  217. KeyEvent.simulate('B'.charCodeAt(0), 66);
  218. KeyEvent.simulate('A'.charCodeAt(0), 65);
  219. KeyEvent.simulate('T'.charCodeAt(0), 84);
  220. expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
  221. expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  222. });
  223. it('keyup sequences should work', function() {
  224. var spy = sinon.spy();
  225. Mousetrap.bind('b a t', spy, 'keyup');
  226. KeyEvent.simulate('b'.charCodeAt(0), 66);
  227. KeyEvent.simulate('a'.charCodeAt(0), 65);
  228. // hold the last key down for a while
  229. KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);
  230. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
  231. });
  232. it('extra spaces in sequences should be ignored', function() {
  233. var spy = sinon.spy();
  234. Mousetrap.bind('b a t', spy);
  235. KeyEvent.simulate('b'.charCodeAt(0), 66);
  236. KeyEvent.simulate('a'.charCodeAt(0), 65);
  237. KeyEvent.simulate('t'.charCodeAt(0), 84);
  238. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  239. });
  240. it('modifiers and sequences play nicely', function() {
  241. var spy1 = sinon.spy();
  242. var spy2 = sinon.spy();
  243. Mousetrap.bind('ctrl a', spy1);
  244. Mousetrap.bind('ctrl+b', spy2);
  245. KeyEvent.simulate(0, 17, ['ctrl']);
  246. KeyEvent.simulate('A'.charCodeAt(0), 65);
  247. expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');
  248. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  249. expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
  250. });
  251. it('sequences that start the same work', function() {
  252. var spy1 = sinon.spy();
  253. var spy2 = sinon.spy();
  254. Mousetrap.bind('g g l', spy2);
  255. Mousetrap.bind('g g o', spy1);
  256. KeyEvent.simulate('g'.charCodeAt(0), 71);
  257. KeyEvent.simulate('g'.charCodeAt(0), 71);
  258. KeyEvent.simulate('o'.charCodeAt(0), 79);
  259. expect(spy1.callCount).to.equal(1, '"g g o" should fire');
  260. expect(spy2.callCount).to.equal(0, '"g g l" should not fire');
  261. spy1.reset();
  262. spy2.reset();
  263. KeyEvent.simulate('g'.charCodeAt(0), 71);
  264. KeyEvent.simulate('g'.charCodeAt(0), 71);
  265. KeyEvent.simulate('l'.charCodeAt(0), 76);
  266. expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
  267. expect(spy2.callCount).to.equal(1, '"g g l" should fire');
  268. });
  269. it('sequences should not fire subsequences', function() {
  270. var spy1 = sinon.spy();
  271. var spy2 = sinon.spy();
  272. Mousetrap.bind('a b c', spy1);
  273. Mousetrap.bind('b c', spy2);
  274. KeyEvent.simulate('A'.charCodeAt(0), 65);
  275. KeyEvent.simulate('B'.charCodeAt(0), 66);
  276. KeyEvent.simulate('C'.charCodeAt(0), 67);
  277. expect(spy1.callCount).to.equal(1, '"a b c" should fire');
  278. expect(spy2.callCount).to.equal(0, '"b c" should not fire');
  279. spy1.reset();
  280. spy2.reset();
  281. Mousetrap.bind('option b', spy1);
  282. Mousetrap.bind('a option b', spy2);
  283. KeyEvent.simulate('A'.charCodeAt(0), 65);
  284. KeyEvent.simulate(0, 18, ['alt']);
  285. KeyEvent.simulate('B'.charCodeAt(0), 66);
  286. expect(spy1.callCount).to.equal(0, '"option b" should not fire');
  287. expect(spy2.callCount).to.equal(1, '"a option b" should fire');
  288. });
  289. it('rebinding same sequence should override previous', function() {
  290. var spy1 = sinon.spy();
  291. var spy2 = sinon.spy();
  292. Mousetrap.bind('a b c', spy1);
  293. Mousetrap.bind('a b c', spy2);
  294. KeyEvent.simulate('a'.charCodeAt(0), 65);
  295. KeyEvent.simulate('b'.charCodeAt(0), 66);
  296. KeyEvent.simulate('c'.charCodeAt(0), 67);
  297. expect(spy1.callCount).to.equal(0, 'first callback should not fire');
  298. expect(spy2.callCount).to.equal(1, 'second callback should fire');
  299. });
  300. it('broken sequences', function() {
  301. var spy = sinon.spy();
  302. Mousetrap.bind('h a t', spy);
  303. KeyEvent.simulate('h'.charCodeAt(0), 72);
  304. KeyEvent.simulate('e'.charCodeAt(0), 69);
  305. KeyEvent.simulate('a'.charCodeAt(0), 65);
  306. KeyEvent.simulate('r'.charCodeAt(0), 82);
  307. KeyEvent.simulate('t'.charCodeAt(0), 84);
  308. expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
  309. });
  310. it('sequences containing combos should work', function() {
  311. var spy = sinon.spy();
  312. Mousetrap.bind('a ctrl+b', spy);
  313. KeyEvent.simulate('a'.charCodeAt(0), 65);
  314. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  315. expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');
  316. Mousetrap.unbind('a ctrl+b');
  317. spy = sinon.spy();
  318. Mousetrap.bind('ctrl+b a', spy);
  319. KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
  320. KeyEvent.simulate('a'.charCodeAt(0), 65);
  321. expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
  322. });
  323. it('sequences starting with spacebar should work', function() {
  324. var spy = sinon.spy();
  325. Mousetrap.bind('a space b c', spy);
  326. KeyEvent.simulate('a'.charCodeAt(0), 65);
  327. KeyEvent.simulate(32, 32);
  328. KeyEvent.simulate('b'.charCodeAt(0), 66);
  329. KeyEvent.simulate('c'.charCodeAt(0), 67);
  330. expect(spy.callCount).to.equal(1, '"a space b c" should fire');
  331. });
  332. it('konami code', function() {
  333. var spy = sinon.spy();
  334. Mousetrap.bind('up up down down left right left right b a enter', spy);
  335. KeyEvent.simulate(0, 38);
  336. KeyEvent.simulate(0, 38);
  337. KeyEvent.simulate(0, 40);
  338. KeyEvent.simulate(0, 40);
  339. KeyEvent.simulate(0, 37);
  340. KeyEvent.simulate(0, 39);
  341. KeyEvent.simulate(0, 37);
  342. KeyEvent.simulate(0, 39);
  343. KeyEvent.simulate('b'.charCodeAt(0), 66);
  344. KeyEvent.simulate('a'.charCodeAt(0), 65);
  345. KeyEvent.simulate(0, 13);
  346. expect(spy.callCount).to.equal(1, 'konami code should fire');
  347. });
  348. it('sequence timer resets', function() {
  349. var spy = sinon.spy();
  350. var clock = sinon.useFakeTimers();
  351. Mousetrap.bind('h a t', spy);
  352. KeyEvent.simulate('h'.charCodeAt(0), 72);
  353. clock.tick(600);
  354. KeyEvent.simulate('a'.charCodeAt(0), 65);
  355. clock.tick(900);
  356. KeyEvent.simulate('t'.charCodeAt(0), 84);
  357. expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
  358. clock.restore();
  359. });
  360. it('sequences timeout', function() {
  361. var spy = sinon.spy();
  362. var clock = sinon.useFakeTimers();
  363. Mousetrap.bind('g t', spy);
  364. KeyEvent.simulate('g'.charCodeAt(0), 71);
  365. clock.tick(1000);
  366. KeyEvent.simulate('t'.charCodeAt(0), 84);
  367. expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
  368. clock.restore();
  369. });
  370. });
  371. describe('default actions', function() {
  372. var keys = {
  373. keypress: [
  374. ['a', 65],
  375. ['A', 65, ['shift']],
  376. ['7', 55],
  377. ['?', 191],
  378. ['*', 56],
  379. ['+', 187],
  380. ['$', 52],
  381. ['[', 219],
  382. ['.', 190]
  383. ],
  384. keydown: [
  385. ['shift+\'', 222, ['shift']],
  386. ['shift+a', 65, ['shift']],
  387. ['shift+5', 53, ['shift']],
  388. ['command+shift+p', 80, ['meta', 'shift']],
  389. ['space', 32],
  390. ['left', 37]
  391. ]
  392. };
  393. function getCallback(key, keyCode, type, modifiers) {
  394. return function() {
  395. var spy = sinon.spy();
  396. Mousetrap.bind(key, spy);
  397. KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
  398. expect(spy.callCount).to.equal(1);
  399. expect(spy.args[0][0].type).to.equal(type);
  400. };
  401. }
  402. for (var type in keys) {
  403. for (var i = 0; i < keys[type].length; i++) {
  404. var key = keys[type][i][0];
  405. var keyCode = keys[type][i][1];
  406. var modifiers = keys[type][i][2] || [];
  407. it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
  408. }
  409. }
  410. });
  411. });
  412. describe('Mousetrap.unbind', function() {
  413. it('unbind works', function() {
  414. var spy = sinon.spy();
  415. Mousetrap.bind('a', spy);
  416. KeyEvent.simulate('a'.charCodeAt(0), 65);
  417. expect(spy.callCount).to.equal(1, 'callback for a should fire');
  418. Mousetrap.unbind('a');
  419. KeyEvent.simulate('a'.charCodeAt(0), 65);
  420. expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
  421. });
  422. it('unbind accepts an array', function() {
  423. var spy = sinon.spy();
  424. Mousetrap.bind(['a', 'b', 'c'], spy);
  425. KeyEvent.simulate('a'.charCodeAt(0), 65);
  426. KeyEvent.simulate('b'.charCodeAt(0), 66);
  427. KeyEvent.simulate('c'.charCodeAt(0), 67);
  428. expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');
  429. Mousetrap.unbind(['a', 'b', 'c']);
  430. KeyEvent.simulate('a'.charCodeAt(0), 65);
  431. KeyEvent.simulate('b'.charCodeAt(0), 66);
  432. KeyEvent.simulate('c'.charCodeAt(0), 67);
  433. expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
  434. });
  435. });