keystrokehandler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import EmitterMixin from '../src/emittermixin';
  6. import KeystrokeHandler from '../src/keystrokehandler';
  7. import { keyCodes } from '../src/keyboard';
  8. describe( 'KeystrokeHandler', () => {
  9. let emitter, keystrokes;
  10. beforeEach( () => {
  11. emitter = Object.create( EmitterMixin );
  12. keystrokes = new KeystrokeHandler();
  13. keystrokes.listenTo( emitter );
  14. } );
  15. describe( 'listenTo()', () => {
  16. it( 'activates the listening on the emitter', () => {
  17. const spy = sinon.spy();
  18. const keyEvtData = getCtrlA();
  19. keystrokes.set( 'ctrl + A', spy );
  20. emitter.fire( 'keydown', keyEvtData );
  21. sinon.assert.calledOnce( spy );
  22. sinon.assert.calledWithExactly( spy, keyEvtData, sinon.match.func );
  23. } );
  24. } );
  25. describe( 'press()', () => {
  26. it( 'executes a callback', () => {
  27. const spy = sinon.spy();
  28. const keyEvtData = getCtrlA();
  29. keystrokes.set( 'ctrl + A', spy );
  30. const wasHandled = keystrokes.press( keyEvtData );
  31. sinon.assert.calledOnce( spy );
  32. sinon.assert.calledWithExactly( spy, keyEvtData, sinon.match.func );
  33. expect( wasHandled ).to.be.true;
  34. } );
  35. it( 'provides a callback which both preventDefault and stopPropagation', done => {
  36. const keyEvtData = getCtrlA();
  37. Object.assign( keyEvtData, {
  38. preventDefault: sinon.spy(),
  39. stopPropagation: sinon.spy()
  40. } );
  41. keystrokes.set( 'ctrl + A', ( data, cancel ) => {
  42. expect( data ).to.equal( keyEvtData );
  43. sinon.assert.notCalled( keyEvtData.preventDefault );
  44. sinon.assert.notCalled( keyEvtData.stopPropagation );
  45. cancel();
  46. sinon.assert.calledOnce( keyEvtData.preventDefault );
  47. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  48. done();
  49. } );
  50. emitter.fire( 'keydown', keyEvtData );
  51. } );
  52. it( 'returns false when no handler', () => {
  53. const keyEvtData = getCtrlA();
  54. const wasHandled = keystrokes.press( keyEvtData );
  55. expect( wasHandled ).to.be.false;
  56. } );
  57. } );
  58. describe( 'set()', () => {
  59. it( 'handles array format', () => {
  60. const spy = sinon.spy();
  61. keystrokes.set( [ 'ctrl', 'A' ], spy );
  62. expect( keystrokes.press( getCtrlA() ) ).to.be.true;
  63. } );
  64. it( 'aggregates multiple callbacks for the same keystroke', () => {
  65. const spy1 = sinon.spy();
  66. const spy2 = sinon.spy();
  67. keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  68. keystrokes.set( [ 'ctrl', 'A' ], spy2 );
  69. keystrokes.press( getCtrlA() );
  70. sinon.assert.calledOnce( spy1 );
  71. sinon.assert.calledOnce( spy2 );
  72. } );
  73. } );
  74. describe( 'destroy()', () => {
  75. it( 'detaches #keydown listener', () => {
  76. const spy = sinon.spy( keystrokes, 'press' );
  77. keystrokes.destroy();
  78. emitter.fire( 'keydown', { keyCode: 1 } );
  79. sinon.assert.notCalled( spy );
  80. } );
  81. it( 'removes all keystrokes', () => {
  82. const spy = sinon.spy();
  83. const keystrokeHandler = keystrokes;
  84. keystrokeHandler.set( 'ctrl + A', spy );
  85. keystrokeHandler.destroy();
  86. const wasHandled = keystrokeHandler.press( getCtrlA() );
  87. expect( wasHandled ).to.be.false;
  88. sinon.assert.notCalled( spy );
  89. } );
  90. } );
  91. } );
  92. function getCtrlA() {
  93. return { keyCode: keyCodes.a, ctrlKey: true };
  94. }