keystrokehandler.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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( 'returns false when no handler', () => {
  36. const keyEvtData = getCtrlA();
  37. const wasHandled = keystrokes.press( keyEvtData );
  38. expect( wasHandled ).to.be.false;
  39. } );
  40. } );
  41. describe( 'set()', () => {
  42. it( 'handles array format', () => {
  43. const spy = sinon.spy();
  44. keystrokes.set( [ 'Ctrl', 'A' ], spy );
  45. expect( keystrokes.press( getCtrlA() ) ).to.be.true;
  46. } );
  47. it( 'aggregates multiple callbacks for the same keystroke', () => {
  48. const spy1 = sinon.spy();
  49. const spy2 = sinon.spy();
  50. keystrokes.set( [ 'Ctrl', 'A' ], spy1 );
  51. keystrokes.set( [ 'Ctrl', 'A' ], spy2 );
  52. keystrokes.press( getCtrlA() );
  53. sinon.assert.calledOnce( spy1 );
  54. sinon.assert.calledOnce( spy2 );
  55. } );
  56. it( 'supports priorities', () => {
  57. const spy1 = sinon.spy();
  58. const spy2 = sinon.spy();
  59. const spy3 = sinon.spy();
  60. const spy4 = sinon.spy();
  61. keystrokes.set( [ 'Ctrl', 'A' ], spy1 );
  62. keystrokes.set( [ 'Ctrl', 'A' ], spy2, { priority: 'high' } );
  63. keystrokes.set( [ 'Ctrl', 'A' ], spy3, { priority: 'low' } );
  64. keystrokes.set( [ 'Ctrl', 'A' ], spy4 );
  65. keystrokes.press( getCtrlA() );
  66. sinon.assert.callOrder( spy2, spy1, spy4, spy3 );
  67. } );
  68. it( 'provides a callback which causes preventDefault and stopPropagation in the DOM', done => {
  69. const keyEvtData = getCtrlA();
  70. keystrokes.set( 'Ctrl+A', ( data, cancel ) => {
  71. expect( data ).to.equal( keyEvtData );
  72. sinon.assert.notCalled( keyEvtData.preventDefault );
  73. sinon.assert.notCalled( keyEvtData.stopPropagation );
  74. cancel();
  75. sinon.assert.calledOnce( keyEvtData.preventDefault );
  76. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  77. done();
  78. } );
  79. emitter.fire( 'keydown', keyEvtData );
  80. } );
  81. it( 'provides a callback which stops the event and remaining callbacks in the keystroke handler', () => {
  82. const spy1 = sinon.spy();
  83. const spy2 = sinon.spy();
  84. const spy3 = sinon.spy();
  85. const spy4 = sinon.spy();
  86. keystrokes.set( [ 'Ctrl', 'A' ], spy1 );
  87. keystrokes.set( [ 'Ctrl', 'A' ], spy2, { priority: 'high' } );
  88. keystrokes.set( [ 'Ctrl', 'A' ], spy3, { priority: 'low' } );
  89. keystrokes.set( [ 'Ctrl', 'A' ], ( keyEvtData, cancel ) => {
  90. spy4();
  91. cancel();
  92. } );
  93. keystrokes.press( getCtrlA() );
  94. sinon.assert.callOrder( spy2, spy1, spy4 );
  95. sinon.assert.notCalled( spy3 );
  96. } );
  97. } );
  98. describe( 'destroy()', () => {
  99. it( 'detaches #keydown listener', () => {
  100. const spy = sinon.spy( keystrokes, 'press' );
  101. keystrokes.destroy();
  102. emitter.fire( 'keydown', { keyCode: 1 } );
  103. sinon.assert.notCalled( spy );
  104. } );
  105. it( 'removes all keystrokes', () => {
  106. const spy = sinon.spy();
  107. const keystrokeHandler = keystrokes;
  108. keystrokeHandler.set( 'Ctrl+A', spy );
  109. keystrokeHandler.destroy();
  110. const wasHandled = keystrokeHandler.press( getCtrlA() );
  111. expect( wasHandled ).to.be.false;
  112. sinon.assert.notCalled( spy );
  113. } );
  114. } );
  115. } );
  116. function getCtrlA() {
  117. return {
  118. keyCode: keyCodes.a,
  119. ctrlKey: true,
  120. preventDefault: sinon.spy(),
  121. stopPropagation: sinon.spy()
  122. };
  123. }