1
0

test.mousetrap.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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].defaultPrevented).to.be.true;
  99. // cancelBubble is not correctly set to true in webkit/blink
  100. //
  101. // @see https://code.google.com/p/chromium/issues/detail?id=162270
  102. // expect(spy.args[0][0].cancelBubble).to.be.true;
  103. // try without return false
  104. spy = sinon.spy();
  105. Mousetrap.bind('command+s', spy);
  106. KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
  107. expect(spy.callCount).to.equal(1, 'callback should fire');
  108. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  109. expect(spy.args[0][0].cancelBubble).to.be.falsey;
  110. expect(spy.args[0][0].defaultPrevented).to.be.falsey;
  111. });
  112. it('capslock key is ignored', function() {
  113. var spy = sinon.spy();
  114. Mousetrap.bind('a', spy);
  115. KeyEvent.simulate('a'.charCodeAt(0), 65);
  116. expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');
  117. spy.reset();
  118. KeyEvent.simulate('A'.charCodeAt(0), 65);
  119. expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');
  120. spy.reset();
  121. KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
  122. expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
  123. });
  124. });
  125. describe('special characters', function() {
  126. it('binding special characters', function() {
  127. var spy = sinon.spy();
  128. Mousetrap.bind('*', spy);
  129. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  130. expect(spy.callCount).to.equal(1, 'callback should fire');
  131. expect(spy.args[0][1]).to.equal('*', 'callback should match *');
  132. });
  133. it('binding special characters keyup', function() {
  134. var spy = sinon.spy();
  135. Mousetrap.bind('*', spy, 'keyup');
  136. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  137. expect(spy.callCount).to.equal(1, 'callback should fire');
  138. expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
  139. });
  140. it('binding keys with no associated charCode', function() {
  141. var spy = sinon.spy();
  142. Mousetrap.bind('left', spy);
  143. KeyEvent.simulate(0, 37);
  144. expect(spy.callCount).to.equal(1, 'callback should fire');
  145. expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
  146. });
  147. it('binding plus key alone should work', function() {
  148. var spy = sinon.spy();
  149. Mousetrap.bind('+', spy);
  150. // fires for regular + character
  151. KeyEvent.simulate('+'.charCodeAt(0), 43);
  152. // and for shift+=
  153. KeyEvent.simulate(43, 187, ['shift']);
  154. expect(spy.callCount).to.equal(2, 'callback should fire');
  155. expect(spy.args[0][1]).to.equal('+', 'callback should match "+"');
  156. });
  157. it('binding plus key as "plus" should work', function() {
  158. var spy = sinon.spy();
  159. Mousetrap.bind('plus', spy);
  160. // fires for regular + character
  161. KeyEvent.simulate('+'.charCodeAt(0), 43);
  162. // and for shift+=
  163. KeyEvent.simulate(43, 187, ['shift']);
  164. expect(spy.callCount).to.equal(2, 'callback should fire');
  165. expect(spy.args[0][1]).to.equal('plus', 'callback should match "plus"');
  166. });
  167. it('binding to alt++ should work', function() {
  168. var spy = sinon.spy();
  169. Mousetrap.bind('alt++', spy);
  170. KeyEvent.simulate('+'.charCodeAt(0), 43, ['alt']);
  171. expect(spy.callCount).to.equal(1, 'callback should fire');
  172. expect(spy.args[0][1]).to.equal('alt++', 'callback should match "alt++"');
  173. });
  174. it('binding to alt+shift++ should work as well', function() {
  175. var spy = sinon.spy();
  176. Mousetrap.bind('alt+shift++', spy);
  177. KeyEvent.simulate('+'.charCodeAt(0), 43, ['shift', 'alt']);
  178. expect(spy.callCount).to.equal(1, 'callback should fire');
  179. expect(spy.args[0][1]).to.equal('alt+shift++', 'callback should match "alt++"');
  180. })
  181. });
  182. describe('combos with modifiers', function() {
  183. it('binding key combinations', function() {
  184. var spy = sinon.spy();
  185. Mousetrap.bind('command+o', spy);
  186. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  187. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  188. expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
  189. });
  190. it('binding key combos with multiple modifiers', function() {
  191. var spy = sinon.spy();
  192. Mousetrap.bind('command+shift+o', spy);
  193. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  194. expect(spy.callCount).to.equal(0, 'command+o callback should not fire');
  195. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
  196. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  197. });
  198. });
  199. describe('sequences', function() {
  200. it('binding sequences', function() {
  201. var spy = sinon.spy();
  202. Mousetrap.bind('g i', spy);
  203. KeyEvent.simulate('G'.charCodeAt(0), 71);
  204. expect(spy.callCount).to.equal(0, 'callback should not fire');
  205. KeyEvent.simulate('I'.charCodeAt(0), 73);
  206. expect(spy.callCount).to.equal(1, 'callback should fire');
  207. });
  208. it('binding sequences with mixed types', function() {
  209. var spy = sinon.spy();
  210. Mousetrap.bind('g o enter', spy);
  211. KeyEvent.simulate('G'.charCodeAt(0), 71);
  212. expect(spy.callCount).to.equal(0, 'callback should not fire');
  213. KeyEvent.simulate('O'.charCodeAt(0), 79);
  214. expect(spy.callCount).to.equal(0, 'callback should not fire');
  215. KeyEvent.simulate(0, 13);
  216. expect(spy.callCount).to.equal(1, 'callback should fire');
  217. });
  218. it('binding sequences starting with modifier keys', function() {
  219. var spy = sinon.spy();
  220. Mousetrap.bind('option enter', spy);
  221. KeyEvent.simulate(0, 18, ['alt']);
  222. KeyEvent.simulate(0, 13);
  223. expect(spy.callCount).to.equal(1, 'callback should fire');
  224. spy = sinon.spy();
  225. Mousetrap.bind('command enter', spy);
  226. KeyEvent.simulate(0, 91, ['meta']);
  227. KeyEvent.simulate(0, 13);
  228. expect(spy.callCount).to.equal(1, 'callback should fire');
  229. spy = sinon.spy();
  230. Mousetrap.bind('escape enter', spy);
  231. KeyEvent.simulate(0, 27);
  232. KeyEvent.simulate(0, 13);
  233. expect(spy.callCount).to.equal(1, 'callback should fire');
  234. });
  235. it('key within sequence should not fire', function() {
  236. var spy1 = sinon.spy();
  237. var spy2 = sinon.spy();
  238. Mousetrap.bind('a', spy1);
  239. Mousetrap.bind('c a t', spy2);
  240. KeyEvent.simulate('A'.charCodeAt(0), 65);
  241. expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
  242. spy1.reset();
  243. KeyEvent.simulate('C'.charCodeAt(0), 67);
  244. KeyEvent.simulate('A'.charCodeAt(0), 65);
  245. KeyEvent.simulate('T'.charCodeAt(0), 84);
  246. expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
  247. expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
  248. });
  249. it('keyup at end of sequence should not fire', function() {
  250. var spy1 = sinon.spy();
  251. var spy2 = sinon.spy();
  252. Mousetrap.bind('t', spy1, 'keyup');
  253. Mousetrap.bind('b a t', spy2);
  254. KeyEvent.simulate('B'.charCodeAt(0), 66);
  255. KeyEvent.simulate('A'.charCodeAt(0), 65);
  256. KeyEvent.simulate('T'.charCodeAt(0), 84);
  257. expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
  258. expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  259. });
  260. it('keyup sequences should work', function() {
  261. var spy = sinon.spy();
  262. Mousetrap.bind('b a t', spy, 'keyup');
  263. KeyEvent.simulate('b'.charCodeAt(0), 66);
  264. KeyEvent.simulate('a'.charCodeAt(0), 65);
  265. // hold the last key down for a while
  266. KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);
  267. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
  268. });
  269. it('extra spaces in sequences should be ignored', function() {
  270. var spy = sinon.spy();
  271. Mousetrap.bind('b a t', spy);
  272. KeyEvent.simulate('b'.charCodeAt(0), 66);
  273. KeyEvent.simulate('a'.charCodeAt(0), 65);
  274. KeyEvent.simulate('t'.charCodeAt(0), 84);
  275. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  276. });
  277. it('modifiers and sequences play nicely', function() {
  278. var spy1 = sinon.spy();
  279. var spy2 = sinon.spy();
  280. Mousetrap.bind('ctrl a', spy1);
  281. Mousetrap.bind('ctrl+b', spy2);
  282. KeyEvent.simulate(0, 17, ['ctrl']);
  283. KeyEvent.simulate('A'.charCodeAt(0), 65);
  284. expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');
  285. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  286. expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
  287. });
  288. it('sequences that start the same work', function() {
  289. var spy1 = sinon.spy();
  290. var spy2 = sinon.spy();
  291. Mousetrap.bind('g g l', spy2);
  292. Mousetrap.bind('g g o', spy1);
  293. KeyEvent.simulate('g'.charCodeAt(0), 71);
  294. KeyEvent.simulate('g'.charCodeAt(0), 71);
  295. KeyEvent.simulate('o'.charCodeAt(0), 79);
  296. expect(spy1.callCount).to.equal(1, '"g g o" should fire');
  297. expect(spy2.callCount).to.equal(0, '"g g l" should not fire');
  298. spy1.reset();
  299. spy2.reset();
  300. KeyEvent.simulate('g'.charCodeAt(0), 71);
  301. KeyEvent.simulate('g'.charCodeAt(0), 71);
  302. KeyEvent.simulate('l'.charCodeAt(0), 76);
  303. expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
  304. expect(spy2.callCount).to.equal(1, '"g g l" should fire');
  305. });
  306. it('sequences should not fire subsequences', function() {
  307. var spy1 = sinon.spy();
  308. var spy2 = sinon.spy();
  309. Mousetrap.bind('a b c', spy1);
  310. Mousetrap.bind('b c', spy2);
  311. KeyEvent.simulate('A'.charCodeAt(0), 65);
  312. KeyEvent.simulate('B'.charCodeAt(0), 66);
  313. KeyEvent.simulate('C'.charCodeAt(0), 67);
  314. expect(spy1.callCount).to.equal(1, '"a b c" should fire');
  315. expect(spy2.callCount).to.equal(0, '"b c" should not fire');
  316. spy1.reset();
  317. spy2.reset();
  318. Mousetrap.bind('option b', spy1);
  319. Mousetrap.bind('a option b', spy2);
  320. KeyEvent.simulate('A'.charCodeAt(0), 65);
  321. KeyEvent.simulate(0, 18, ['alt']);
  322. KeyEvent.simulate('B'.charCodeAt(0), 66);
  323. expect(spy1.callCount).to.equal(0, '"option b" should not fire');
  324. expect(spy2.callCount).to.equal(1, '"a option b" should fire');
  325. });
  326. it('rebinding same sequence should override previous', function() {
  327. var spy1 = sinon.spy();
  328. var spy2 = sinon.spy();
  329. Mousetrap.bind('a b c', spy1);
  330. Mousetrap.bind('a b c', spy2);
  331. KeyEvent.simulate('a'.charCodeAt(0), 65);
  332. KeyEvent.simulate('b'.charCodeAt(0), 66);
  333. KeyEvent.simulate('c'.charCodeAt(0), 67);
  334. expect(spy1.callCount).to.equal(0, 'first callback should not fire');
  335. expect(spy2.callCount).to.equal(1, 'second callback should fire');
  336. });
  337. it('broken sequences', function() {
  338. var spy = sinon.spy();
  339. Mousetrap.bind('h a t', spy);
  340. KeyEvent.simulate('h'.charCodeAt(0), 72);
  341. KeyEvent.simulate('e'.charCodeAt(0), 69);
  342. KeyEvent.simulate('a'.charCodeAt(0), 65);
  343. KeyEvent.simulate('r'.charCodeAt(0), 82);
  344. KeyEvent.simulate('t'.charCodeAt(0), 84);
  345. expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
  346. });
  347. it('sequences containing combos should work', function() {
  348. var spy = sinon.spy();
  349. Mousetrap.bind('a ctrl+b', spy);
  350. KeyEvent.simulate('a'.charCodeAt(0), 65);
  351. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  352. expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');
  353. Mousetrap.unbind('a ctrl+b');
  354. spy = sinon.spy();
  355. Mousetrap.bind('ctrl+b a', spy);
  356. KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
  357. KeyEvent.simulate('a'.charCodeAt(0), 65);
  358. expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
  359. });
  360. it('sequences starting with spacebar should work', function() {
  361. var spy = sinon.spy();
  362. Mousetrap.bind('a space b c', spy);
  363. KeyEvent.simulate('a'.charCodeAt(0), 65);
  364. KeyEvent.simulate(32, 32);
  365. KeyEvent.simulate('b'.charCodeAt(0), 66);
  366. KeyEvent.simulate('c'.charCodeAt(0), 67);
  367. expect(spy.callCount).to.equal(1, '"a space b c" should fire');
  368. });
  369. it('konami code', function() {
  370. var spy = sinon.spy();
  371. Mousetrap.bind('up up down down left right left right b a enter', spy);
  372. KeyEvent.simulate(0, 38);
  373. KeyEvent.simulate(0, 38);
  374. KeyEvent.simulate(0, 40);
  375. KeyEvent.simulate(0, 40);
  376. KeyEvent.simulate(0, 37);
  377. KeyEvent.simulate(0, 39);
  378. KeyEvent.simulate(0, 37);
  379. KeyEvent.simulate(0, 39);
  380. KeyEvent.simulate('b'.charCodeAt(0), 66);
  381. KeyEvent.simulate('a'.charCodeAt(0), 65);
  382. KeyEvent.simulate(0, 13);
  383. expect(spy.callCount).to.equal(1, 'konami code should fire');
  384. });
  385. it('sequence timer resets', function() {
  386. var spy = sinon.spy();
  387. var clock = sinon.useFakeTimers();
  388. Mousetrap.bind('h a t', spy);
  389. KeyEvent.simulate('h'.charCodeAt(0), 72);
  390. clock.tick(600);
  391. KeyEvent.simulate('a'.charCodeAt(0), 65);
  392. clock.tick(900);
  393. KeyEvent.simulate('t'.charCodeAt(0), 84);
  394. expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
  395. clock.restore();
  396. });
  397. it('sequences timeout', function() {
  398. var spy = sinon.spy();
  399. var clock = sinon.useFakeTimers();
  400. Mousetrap.bind('g t', spy);
  401. KeyEvent.simulate('g'.charCodeAt(0), 71);
  402. clock.tick(1000);
  403. KeyEvent.simulate('t'.charCodeAt(0), 84);
  404. expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
  405. clock.restore();
  406. });
  407. });
  408. describe('default actions', function() {
  409. var keys = {
  410. keypress: [
  411. ['a', 65],
  412. ['A', 65, ['shift']],
  413. ['7', 55],
  414. ['?', 191],
  415. ['*', 56],
  416. ['+', 187],
  417. ['$', 52],
  418. ['[', 219],
  419. ['.', 190]
  420. ],
  421. keydown: [
  422. ['shift+\'', 222, ['shift']],
  423. ['shift+a', 65, ['shift']],
  424. ['shift+5', 53, ['shift']],
  425. ['command+shift+p', 80, ['meta', 'shift']],
  426. ['space', 32],
  427. ['left', 37]
  428. ]
  429. };
  430. function getCallback(key, keyCode, type, modifiers) {
  431. return function() {
  432. var spy = sinon.spy();
  433. Mousetrap.bind(key, spy);
  434. KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
  435. expect(spy.callCount).to.equal(1);
  436. expect(spy.args[0][0].type).to.equal(type);
  437. };
  438. }
  439. for (var type in keys) {
  440. for (var i = 0; i < keys[type].length; i++) {
  441. var key = keys[type][i][0];
  442. var keyCode = keys[type][i][1];
  443. var modifiers = keys[type][i][2] || [];
  444. it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
  445. }
  446. }
  447. });
  448. });
  449. describe('Mousetrap.unbind', function() {
  450. it('unbind works', function() {
  451. var spy = sinon.spy();
  452. Mousetrap.bind('a', spy);
  453. KeyEvent.simulate('a'.charCodeAt(0), 65);
  454. expect(spy.callCount).to.equal(1, 'callback for a should fire');
  455. Mousetrap.unbind('a');
  456. KeyEvent.simulate('a'.charCodeAt(0), 65);
  457. expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
  458. });
  459. it('unbind accepts an array', function() {
  460. var spy = sinon.spy();
  461. Mousetrap.bind(['a', 'b', 'c'], spy);
  462. KeyEvent.simulate('a'.charCodeAt(0), 65);
  463. KeyEvent.simulate('b'.charCodeAt(0), 66);
  464. KeyEvent.simulate('c'.charCodeAt(0), 67);
  465. expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');
  466. Mousetrap.unbind(['a', 'b', 'c']);
  467. KeyEvent.simulate('a'.charCodeAt(0), 65);
  468. KeyEvent.simulate('b'.charCodeAt(0), 66);
  469. KeyEvent.simulate('c'.charCodeAt(0), 67);
  470. expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
  471. });
  472. });
  473. describe('wrapping a specific element', function() {
  474. var form = document.querySelector('form');
  475. var textarea = form.querySelector('textarea');
  476. it('z key fires when pressing z in the target element', function() {
  477. var spy = sinon.spy();
  478. Mousetrap(form).bind('z', spy);
  479. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], form);
  480. expect(spy.callCount).to.equal(1, 'callback should fire once');
  481. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  482. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  483. });
  484. it('z key fires when pressing z in a child of the target element', function() {
  485. var spy = sinon.spy();
  486. Mousetrap(form).bind('z', spy);
  487. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], textarea);
  488. expect(spy.callCount).to.equal(1, 'callback should fire once');
  489. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  490. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  491. });
  492. it('z key does not fire when pressing z outside the target element', function() {
  493. var spy = sinon.spy();
  494. Mousetrap(textarea).bind('z', spy);
  495. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  496. expect(spy.callCount).to.equal(0, 'callback should not have fired');
  497. });
  498. it('should work when constructing a new mousetrap object', function() {
  499. var spy = sinon.spy();
  500. var mousetrap = new Mousetrap(form);
  501. mousetrap.bind('a', spy);
  502. KeyEvent.simulate('a'.charCodeAt(0), 65, [], textarea);
  503. expect(spy.callCount).to.equal(1, 'callback should fire once');
  504. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  505. expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
  506. });
  507. it('should allow you to create an empty mousetrap constructor', function() {
  508. var spy = sinon.spy();
  509. var mousetrap = new Mousetrap();
  510. mousetrap.bind('a', spy);
  511. KeyEvent.simulate('a'.charCodeAt(0), 65);
  512. expect(spy.callCount).to.equal(1, 'callback should fire once');
  513. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  514. expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
  515. });
  516. });