keystrokehandler.js 3.7 KB

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