test.mousetrap.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /**
  2. * The following strategy of importing modules allows the tests to be run in a browser environment.
  3. * Test libraries like `mocha`, `sinon`, etc. are expected to be loaded before this file.
  4. */
  5. var sinon = sinon || require('sinon');
  6. var chai = chai || require('chai');
  7. var expect = chai.expect;
  8. if (typeof window === 'undefined') {
  9. require('mocha');
  10. require('jsdom-global')();
  11. }
  12. // Load libraries that require access to the DOM after `jsdom-global`
  13. var Mousetrap = Mousetrap || require('./../mousetrap');
  14. var KeyEvent = KeyEvent || require('./libs/key-event');
  15. // Reset Mousetrap after each test
  16. afterEach(function () {
  17. Mousetrap.reset();
  18. });
  19. describe('Mousetrap.bind', function () {
  20. describe('basic', function () {
  21. it('z key fires when pressing z', function () {
  22. var spy = sinon.spy();
  23. Mousetrap.bind('z', spy);
  24. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  25. // really slow for some reason
  26. // expect(spy).to.have.been.calledOnce;
  27. expect(spy.callCount).to.equal(1, 'callback should fire once');
  28. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  29. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  30. });
  31. it('z key fires from keydown', function () {
  32. var spy = sinon.spy();
  33. Mousetrap.bind('z', spy, 'keydown');
  34. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  35. // really slow for some reason
  36. // expect(spy).to.have.been.calledOnce;
  37. expect(spy.callCount).to.equal(1, 'callback should fire once');
  38. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  39. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  40. });
  41. it('z key does not fire when pressing b', function () {
  42. var spy = sinon.spy();
  43. Mousetrap.bind('z', spy);
  44. KeyEvent.simulate('B'.charCodeAt(0), 66);
  45. expect(spy.callCount).to.equal(0);
  46. });
  47. it('z key does not fire when holding a modifier key', function () {
  48. var spy = sinon.spy();
  49. var modifiers = ['ctrl', 'alt', 'meta', 'shift'];
  50. var charCode;
  51. var modifier;
  52. Mousetrap.bind('z', spy);
  53. for (var i = 0; i < 4; i++) {
  54. modifier = modifiers[i];
  55. charCode = 'Z'.charCodeAt(0);
  56. // character code is different when alt is pressed
  57. if (modifier == 'alt') {
  58. charCode = 'Ω'.charCodeAt(0);
  59. }
  60. spy.resetHistory();
  61. KeyEvent.simulate(charCode, 90, [modifier]);
  62. expect(spy.callCount).to.equal(0);
  63. }
  64. });
  65. it('z key does not fire when inside an input element in an open shadow dom', function() {
  66. var spy = sinon.spy();
  67. var shadowHost = document.createElement('div');
  68. var shadowRoot = shadowHost.attachShadow({ mode: 'open' });
  69. document.body.appendChild(shadowHost);
  70. var inputElement = document.createElement('input');
  71. shadowRoot.appendChild(inputElement);
  72. expect(shadowHost.shadowRoot).to.equal(shadowRoot, 'shadow root accessible');
  73. Mousetrap.bind('z', spy);
  74. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
  75. document.body.removeChild(shadowHost);
  76. expect(spy.callCount).to.equal(0, 'callback should not have fired');
  77. });
  78. it('z key does fire when inside an input element in a closed shadow dom', function() {
  79. var spy = sinon.spy();
  80. var shadowHost = document.createElement('div');
  81. var shadowRoot = shadowHost.attachShadow({ mode: 'closed' });
  82. document.body.appendChild(shadowHost);
  83. var inputElement = document.createElement('input');
  84. shadowRoot.appendChild(inputElement);
  85. expect(shadowHost.shadowRoot).to.equal(null, 'shadow root unaccessible');
  86. Mousetrap.bind('z', spy);
  87. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
  88. document.body.removeChild(shadowHost);
  89. expect(spy.callCount).to.equal(1, 'callback should have fired once');
  90. });
  91. it('keyup events should fire', function() {
  92. var spy = sinon.spy();
  93. Mousetrap.bind('z', spy, 'keyup');
  94. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  95. expect(spy.callCount).to.equal(1, 'keyup event for "z" should fire');
  96. // for key held down we should only get one key up
  97. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], document, 10);
  98. expect(spy.callCount).to.equal(2, 'keyup event for "z" should fire once for held down key');
  99. });
  100. it('keyup event for 0 should fire', function () {
  101. var spy = sinon.spy();
  102. Mousetrap.bind('0', spy, 'keyup');
  103. KeyEvent.simulate(0, 48);
  104. expect(spy.callCount).to.equal(1, 'keyup event for "0" should fire');
  105. });
  106. it('rebinding a key overwrites the callback for that key', function () {
  107. var spy1 = sinon.spy();
  108. var spy2 = sinon.spy();
  109. Mousetrap.bind('x', spy1);
  110. Mousetrap.bind('x', spy2);
  111. KeyEvent.simulate('X'.charCodeAt(0), 88);
  112. expect(spy1.callCount).to.equal(0, 'original callback should not fire');
  113. expect(spy2.callCount).to.equal(1, 'new callback should fire');
  114. });
  115. it('binding an array of keys', function () {
  116. var spy = sinon.spy();
  117. Mousetrap.bind(['a', 'b', 'c'], spy);
  118. KeyEvent.simulate('A'.charCodeAt(0), 65);
  119. expect(spy.callCount).to.equal(1, 'new callback was called');
  120. expect(spy.args[0][1]).to.equal('a', 'callback should match "a"');
  121. KeyEvent.simulate('B'.charCodeAt(0), 66);
  122. expect(spy.callCount).to.equal(2, 'new callback was called twice');
  123. expect(spy.args[1][1]).to.equal('b', 'callback should match "b"');
  124. KeyEvent.simulate('C'.charCodeAt(0), 67);
  125. expect(spy.callCount).to.equal(3, 'new callback was called three times');
  126. expect(spy.args[2][1]).to.equal('c', 'callback should match "c"');
  127. });
  128. it('return false should prevent default and stop propagation', function () {
  129. var spy = sinon.spy(function () {
  130. return false;
  131. });
  132. Mousetrap.bind('command+s', spy);
  133. KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
  134. expect(spy.callCount).to.equal(1, 'callback should fire');
  135. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  136. expect(spy.args[0][0].defaultPrevented).to.be.true;
  137. // cancelBubble is not correctly set to true in webkit/blink
  138. //
  139. // @see https://code.google.com/p/chromium/issues/detail?id=162270
  140. // expect(spy.args[0][0].cancelBubble).to.be.true;
  141. // try without return false
  142. spy = sinon.spy();
  143. Mousetrap.bind('command+s', spy);
  144. KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
  145. expect(spy.callCount).to.equal(1, 'callback should fire');
  146. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  147. expect(spy.args[0][0].cancelBubble).to.be.false;
  148. expect(spy.args[0][0].defaultPrevented).to.be.false;
  149. });
  150. it('capslock key is ignored', function () {
  151. var spy = sinon.spy();
  152. Mousetrap.bind('a', spy);
  153. KeyEvent.simulate('a'.charCodeAt(0), 65);
  154. expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');
  155. spy.resetHistory();
  156. KeyEvent.simulate('A'.charCodeAt(0), 65);
  157. expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');
  158. spy.resetHistory();
  159. KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
  160. expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
  161. });
  162. });
  163. describe('special characters', function () {
  164. it('binding special characters', function () {
  165. var spy = sinon.spy();
  166. Mousetrap.bind('*', spy);
  167. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  168. expect(spy.callCount).to.equal(1, 'callback should fire');
  169. expect(spy.args[0][1]).to.equal('*', 'callback should match *');
  170. });
  171. it('binding special characters keyup', function () {
  172. var spy = sinon.spy();
  173. Mousetrap.bind('*', spy, 'keyup');
  174. KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
  175. expect(spy.callCount).to.equal(1, 'callback should fire');
  176. expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
  177. });
  178. it('binding keys with no associated charCode', function () {
  179. var spy = sinon.spy();
  180. Mousetrap.bind('left', spy);
  181. KeyEvent.simulate(0, 37);
  182. expect(spy.callCount).to.equal(1, 'callback should fire');
  183. expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
  184. });
  185. it('binding plus key alone should work', function () {
  186. var spy = sinon.spy();
  187. Mousetrap.bind('+', spy);
  188. // fires for regular + character
  189. KeyEvent.simulate('+'.charCodeAt(0), 43);
  190. // and for shift+=
  191. KeyEvent.simulate(43, 187, ['shift']);
  192. expect(spy.callCount).to.equal(2, 'callback should fire');
  193. expect(spy.args[0][1]).to.equal('+', 'callback should match "+"');
  194. });
  195. it('binding plus key as "plus" should work', function () {
  196. var spy = sinon.spy();
  197. Mousetrap.bind('plus', spy);
  198. // fires for regular + character
  199. KeyEvent.simulate('+'.charCodeAt(0), 43);
  200. // and for shift+=
  201. KeyEvent.simulate(43, 187, ['shift']);
  202. expect(spy.callCount).to.equal(2, 'callback should fire');
  203. expect(spy.args[0][1]).to.equal('plus', 'callback should match "plus"');
  204. });
  205. it('binding to alt++ should work', function () {
  206. var spy = sinon.spy();
  207. Mousetrap.bind('alt++', spy);
  208. KeyEvent.simulate('+'.charCodeAt(0), 43, ['alt']);
  209. expect(spy.callCount).to.equal(1, 'callback should fire');
  210. expect(spy.args[0][1]).to.equal('alt++', 'callback should match "alt++"');
  211. });
  212. it('binding to alt+shift++ should work as well', function () {
  213. var spy = sinon.spy();
  214. Mousetrap.bind('alt+shift++', spy);
  215. KeyEvent.simulate('+'.charCodeAt(0), 43, ['shift', 'alt']);
  216. expect(spy.callCount).to.equal(1, 'callback should fire');
  217. expect(spy.args[0][1]).to.equal('alt+shift++', 'callback should match "alt++"');
  218. });
  219. });
  220. describe('combos with modifiers', function () {
  221. it('binding key combinations', function () {
  222. var spy = sinon.spy();
  223. Mousetrap.bind('command+o', spy);
  224. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  225. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  226. expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
  227. });
  228. it('binding key combos with multiple modifiers', function () {
  229. var spy = sinon.spy();
  230. Mousetrap.bind('command+shift+o', spy);
  231. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
  232. expect(spy.callCount).to.equal(0, 'command+o callback should not fire');
  233. KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
  234. expect(spy.callCount).to.equal(1, 'command+o callback should fire');
  235. });
  236. it('should fire callback when ctrl+numpad 0 is pressed', function () {
  237. var spy = sinon.spy();
  238. Mousetrap.bind('ctrl+0', spy);
  239. // numpad 0 keycode
  240. KeyEvent.simulate(96, 96, ['ctrl']);
  241. expect(spy.callCount).to.equal(1, 'callback should fire once');
  242. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  243. expect(spy.args[0][1]).to.equal('ctrl+0', 'second argument should be key combo');
  244. });
  245. });
  246. describe('sequences', function () {
  247. it('binding sequences', function () {
  248. var spy = sinon.spy();
  249. Mousetrap.bind('g i', spy);
  250. KeyEvent.simulate('G'.charCodeAt(0), 71);
  251. expect(spy.callCount).to.equal(0, 'callback should not fire');
  252. KeyEvent.simulate('I'.charCodeAt(0), 73);
  253. expect(spy.callCount).to.equal(1, 'callback should fire');
  254. });
  255. it('binding sequences with mixed types', function () {
  256. var spy = sinon.spy();
  257. Mousetrap.bind('g o enter', spy);
  258. KeyEvent.simulate('G'.charCodeAt(0), 71);
  259. expect(spy.callCount).to.equal(0, 'callback should not fire');
  260. KeyEvent.simulate('O'.charCodeAt(0), 79);
  261. expect(spy.callCount).to.equal(0, 'callback should not fire');
  262. KeyEvent.simulate(0, 13);
  263. expect(spy.callCount).to.equal(1, 'callback should fire');
  264. });
  265. it('binding sequences starting with modifier keys', function () {
  266. var spy = sinon.spy();
  267. Mousetrap.bind('option enter', spy);
  268. KeyEvent.simulate(0, 18, ['alt']);
  269. KeyEvent.simulate(0, 13);
  270. expect(spy.callCount).to.equal(1, 'callback should fire');
  271. spy = sinon.spy();
  272. Mousetrap.bind('command enter', spy);
  273. KeyEvent.simulate(0, 91, ['meta']);
  274. KeyEvent.simulate(0, 13);
  275. expect(spy.callCount).to.equal(1, 'callback should fire');
  276. spy = sinon.spy();
  277. Mousetrap.bind('escape enter', spy);
  278. KeyEvent.simulate(0, 27);
  279. KeyEvent.simulate(0, 13);
  280. expect(spy.callCount).to.equal(1, 'callback should fire');
  281. });
  282. it('key within sequence should not fire', function () {
  283. var spy1 = sinon.spy();
  284. var spy2 = sinon.spy();
  285. Mousetrap.bind('a', spy1);
  286. Mousetrap.bind('c a t', spy2);
  287. KeyEvent.simulate('A'.charCodeAt(0), 65);
  288. expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
  289. spy1.resetHistory();
  290. KeyEvent.simulate('C'.charCodeAt(0), 67);
  291. KeyEvent.simulate('A'.charCodeAt(0), 65);
  292. KeyEvent.simulate('T'.charCodeAt(0), 84);
  293. expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
  294. expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
  295. });
  296. it('keyup at end of sequence should not fire', function () {
  297. var spy1 = sinon.spy();
  298. var spy2 = sinon.spy();
  299. Mousetrap.bind('t', spy1, 'keyup');
  300. Mousetrap.bind('b a t', spy2);
  301. KeyEvent.simulate('B'.charCodeAt(0), 66);
  302. KeyEvent.simulate('A'.charCodeAt(0), 65);
  303. KeyEvent.simulate('T'.charCodeAt(0), 84);
  304. expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
  305. expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  306. });
  307. it('keyup sequences should work', function () {
  308. var spy = sinon.spy();
  309. Mousetrap.bind('b a t', spy, 'keyup');
  310. KeyEvent.simulate('b'.charCodeAt(0), 66);
  311. KeyEvent.simulate('a'.charCodeAt(0), 65);
  312. // hold the last key down for a while
  313. KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);
  314. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
  315. });
  316. it('extra spaces in sequences should be ignored', function () {
  317. var spy = sinon.spy();
  318. Mousetrap.bind('b a t', spy);
  319. KeyEvent.simulate('b'.charCodeAt(0), 66);
  320. KeyEvent.simulate('a'.charCodeAt(0), 65);
  321. KeyEvent.simulate('t'.charCodeAt(0), 84);
  322. expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
  323. });
  324. it('modifiers and sequences play nicely', function () {
  325. var spy1 = sinon.spy();
  326. var spy2 = sinon.spy();
  327. Mousetrap.bind('ctrl a', spy1);
  328. Mousetrap.bind('ctrl+b', spy2);
  329. KeyEvent.simulate(0, 17, ['ctrl']);
  330. KeyEvent.simulate('A'.charCodeAt(0), 65);
  331. expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');
  332. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  333. expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
  334. });
  335. it('sequences that start the same work', function () {
  336. var spy1 = sinon.spy();
  337. var spy2 = sinon.spy();
  338. Mousetrap.bind('g g l', spy2);
  339. Mousetrap.bind('g g o', spy1);
  340. KeyEvent.simulate('g'.charCodeAt(0), 71);
  341. KeyEvent.simulate('g'.charCodeAt(0), 71);
  342. KeyEvent.simulate('o'.charCodeAt(0), 79);
  343. expect(spy1.callCount).to.equal(1, '"g g o" should fire');
  344. expect(spy2.callCount).to.equal(0, '"g g l" should not fire');
  345. spy1.resetHistory();
  346. spy2.resetHistory();
  347. KeyEvent.simulate('g'.charCodeAt(0), 71);
  348. KeyEvent.simulate('g'.charCodeAt(0), 71);
  349. KeyEvent.simulate('l'.charCodeAt(0), 76);
  350. expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
  351. expect(spy2.callCount).to.equal(1, '"g g l" should fire');
  352. });
  353. it('sequences should not fire subsequences', function () {
  354. var spy1 = sinon.spy();
  355. var spy2 = sinon.spy();
  356. Mousetrap.bind('a b c', spy1);
  357. Mousetrap.bind('b c', spy2);
  358. KeyEvent.simulate('A'.charCodeAt(0), 65);
  359. KeyEvent.simulate('B'.charCodeAt(0), 66);
  360. KeyEvent.simulate('C'.charCodeAt(0), 67);
  361. expect(spy1.callCount).to.equal(1, '"a b c" should fire');
  362. expect(spy2.callCount).to.equal(0, '"b c" should not fire');
  363. spy1.resetHistory();
  364. spy2.resetHistory();
  365. Mousetrap.bind('option b', spy1);
  366. Mousetrap.bind('a option b', spy2);
  367. KeyEvent.simulate('A'.charCodeAt(0), 65);
  368. KeyEvent.simulate(0, 18, ['alt']);
  369. KeyEvent.simulate('B'.charCodeAt(0), 66);
  370. expect(spy1.callCount).to.equal(0, '"option b" should not fire');
  371. expect(spy2.callCount).to.equal(1, '"a option b" should fire');
  372. });
  373. it('rebinding same sequence should override previous', function () {
  374. var spy1 = sinon.spy();
  375. var spy2 = sinon.spy();
  376. Mousetrap.bind('a b c', spy1);
  377. Mousetrap.bind('a b c', spy2);
  378. KeyEvent.simulate('a'.charCodeAt(0), 65);
  379. KeyEvent.simulate('b'.charCodeAt(0), 66);
  380. KeyEvent.simulate('c'.charCodeAt(0), 67);
  381. expect(spy1.callCount).to.equal(0, 'first callback should not fire');
  382. expect(spy2.callCount).to.equal(1, 'second callback should fire');
  383. });
  384. it('broken sequences', function () {
  385. var spy = sinon.spy();
  386. Mousetrap.bind('h a t', spy);
  387. KeyEvent.simulate('h'.charCodeAt(0), 72);
  388. KeyEvent.simulate('e'.charCodeAt(0), 69);
  389. KeyEvent.simulate('a'.charCodeAt(0), 65);
  390. KeyEvent.simulate('r'.charCodeAt(0), 82);
  391. KeyEvent.simulate('t'.charCodeAt(0), 84);
  392. expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
  393. });
  394. it('sequences containing combos should work', function () {
  395. var spy = sinon.spy();
  396. Mousetrap.bind('a ctrl+b', spy);
  397. KeyEvent.simulate('a'.charCodeAt(0), 65);
  398. KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
  399. expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');
  400. Mousetrap.unbind('a ctrl+b');
  401. spy = sinon.spy();
  402. Mousetrap.bind('ctrl+b a', spy);
  403. KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
  404. KeyEvent.simulate('a'.charCodeAt(0), 65);
  405. expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
  406. });
  407. it('sequences starting with spacebar should work', function () {
  408. var spy = sinon.spy();
  409. Mousetrap.bind('a space b c', spy);
  410. KeyEvent.simulate('a'.charCodeAt(0), 65);
  411. KeyEvent.simulate(32, 32);
  412. KeyEvent.simulate('b'.charCodeAt(0), 66);
  413. KeyEvent.simulate('c'.charCodeAt(0), 67);
  414. expect(spy.callCount).to.equal(1, '"a space b c" should fire');
  415. });
  416. it('konami code', function () {
  417. var spy = sinon.spy();
  418. Mousetrap.bind('up up down down left right left right b a enter', spy);
  419. KeyEvent.simulate(0, 38);
  420. KeyEvent.simulate(0, 38);
  421. KeyEvent.simulate(0, 40);
  422. KeyEvent.simulate(0, 40);
  423. KeyEvent.simulate(0, 37);
  424. KeyEvent.simulate(0, 39);
  425. KeyEvent.simulate(0, 37);
  426. KeyEvent.simulate(0, 39);
  427. KeyEvent.simulate('b'.charCodeAt(0), 66);
  428. KeyEvent.simulate('a'.charCodeAt(0), 65);
  429. KeyEvent.simulate(0, 13);
  430. expect(spy.callCount).to.equal(1, 'konami code should fire');
  431. });
  432. it('sequence timer resets', function () {
  433. var spy = sinon.spy();
  434. var clock = sinon.useFakeTimers();
  435. Mousetrap.bind('h a t', spy);
  436. KeyEvent.simulate('h'.charCodeAt(0), 72);
  437. clock.tick(600);
  438. KeyEvent.simulate('a'.charCodeAt(0), 65);
  439. clock.tick(900);
  440. KeyEvent.simulate('t'.charCodeAt(0), 84);
  441. expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
  442. clock.restore();
  443. });
  444. it('sequences timeout', function () {
  445. var spy = sinon.spy();
  446. var clock = sinon.useFakeTimers();
  447. Mousetrap.bind('g t', spy);
  448. KeyEvent.simulate('g'.charCodeAt(0), 71);
  449. clock.tick(1000);
  450. KeyEvent.simulate('t'.charCodeAt(0), 84);
  451. expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
  452. clock.restore();
  453. });
  454. });
  455. describe('default actions', function () {
  456. var keys = {
  457. keypress: [
  458. ['a', 65],
  459. ['A', 65, ['shift']],
  460. ['7', 55],
  461. ['?', 191],
  462. ['*', 56],
  463. ['+', 187],
  464. ['$', 52],
  465. ['[', 219],
  466. ['.', 190]
  467. ],
  468. keydown: [
  469. ['shift+\'', 222, ['shift']],
  470. ['shift+a', 65, ['shift']],
  471. ['shift+5', 53, ['shift']],
  472. ['command+shift+p', 80, ['meta', 'shift']],
  473. ['space', 32],
  474. ['left', 37]
  475. ]
  476. };
  477. function getCallback(key, keyCode, type, modifiers) {
  478. return function () {
  479. var spy = sinon.spy();
  480. Mousetrap.bind(key, spy);
  481. KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
  482. expect(spy.callCount).to.equal(1);
  483. expect(spy.args[0][0].type).to.equal(type);
  484. };
  485. }
  486. for (var type in keys) {
  487. for (var i = 0; i < keys[type].length; i++) {
  488. var key = keys[type][i][0];
  489. var keyCode = keys[type][i][1];
  490. var modifiers = keys[type][i][2] || [];
  491. it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
  492. }
  493. }
  494. });
  495. });
  496. describe('Mousetrap.unbind', function () {
  497. it('unbind works', function () {
  498. var spy = sinon.spy();
  499. Mousetrap.bind('a', spy);
  500. KeyEvent.simulate('a'.charCodeAt(0), 65);
  501. expect(spy.callCount).to.equal(1, 'callback for a should fire');
  502. Mousetrap.unbind('a');
  503. KeyEvent.simulate('a'.charCodeAt(0), 65);
  504. expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
  505. });
  506. it('unbind accepts an array', function () {
  507. var spy = sinon.spy();
  508. Mousetrap.bind(['a', 'b', 'c'], spy);
  509. KeyEvent.simulate('a'.charCodeAt(0), 65);
  510. KeyEvent.simulate('b'.charCodeAt(0), 66);
  511. KeyEvent.simulate('c'.charCodeAt(0), 67);
  512. expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');
  513. Mousetrap.unbind(['a', 'b', 'c']);
  514. KeyEvent.simulate('a'.charCodeAt(0), 65);
  515. KeyEvent.simulate('b'.charCodeAt(0), 66);
  516. KeyEvent.simulate('c'.charCodeAt(0), 67);
  517. expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
  518. });
  519. });
  520. describe('wrapping a specific element', function () {
  521. // Prepare the DOM for these tests.
  522. document.body.insertAdjacentHTML('afterbegin', `
  523. <form style="display: none;">
  524. <textarea></textarea>
  525. </form>
  526. `);
  527. var form = document.querySelector('form');
  528. var textarea = form.querySelector('textarea');
  529. it('z key fires when pressing z in the target element', function () {
  530. var spy = sinon.spy();
  531. Mousetrap(form).bind('z', spy);
  532. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], form);
  533. expect(spy.callCount).to.equal(1, 'callback should fire once');
  534. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  535. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  536. });
  537. it('z key fires when pressing z in a child of the target element', function () {
  538. var spy = sinon.spy();
  539. Mousetrap(form).bind('z', spy);
  540. KeyEvent.simulate('Z'.charCodeAt(0), 90, [], textarea);
  541. expect(spy.callCount).to.equal(1, 'callback should fire once');
  542. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  543. expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
  544. });
  545. it('z key does not fire when pressing z outside the target element', function () {
  546. var spy = sinon.spy();
  547. Mousetrap(textarea).bind('z', spy);
  548. KeyEvent.simulate('Z'.charCodeAt(0), 90);
  549. expect(spy.callCount).to.equal(0, 'callback should not have fired');
  550. });
  551. it('should work when constructing a new mousetrap object', function () {
  552. var spy = sinon.spy();
  553. var mousetrap = new Mousetrap(form);
  554. mousetrap.bind('a', spy);
  555. KeyEvent.simulate('a'.charCodeAt(0), 65, [], textarea);
  556. expect(spy.callCount).to.equal(1, 'callback should fire once');
  557. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  558. expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
  559. });
  560. it('should allow you to create an empty mousetrap constructor', function () {
  561. var spy = sinon.spy();
  562. var mousetrap = new Mousetrap();
  563. mousetrap.bind('a', spy);
  564. KeyEvent.simulate('a'.charCodeAt(0), 65);
  565. expect(spy.callCount).to.equal(1, 'callback should fire once');
  566. expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
  567. expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
  568. });
  569. });
  570. describe('Mouestrap.addKeycodes', function () {
  571. it('should properly recognize non-default mapping', function () {
  572. const spy = sinon.spy();
  573. Mousetrap.addKeycodes({
  574. 144: 'num',
  575. });
  576. Mousetrap.bind('num', spy);
  577. KeyEvent.simulate(144, 144);
  578. expect(spy.callCount).to.equal(1, 'callback should fire for num');
  579. spy.resetHistory();
  580. });
  581. });