8
0

keystrokehandler.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. it( 'triggers #press on #keydown', () => {
  29. const spy = sinon.spy( keystrokes, 'press' );
  30. const keyEvtData = { keyCode: 1 };
  31. emitter.fire( 'keydown', keyEvtData );
  32. sinon.assert.calledOnce( spy );
  33. sinon.assert.calledWithExactly( spy, keyEvtData );
  34. } );
  35. } );
  36. describe( 'press()', () => {
  37. it( 'executes a callback', () => {
  38. const spy = sinon.spy();
  39. const keyEvtData = getCtrlA();
  40. keystrokes.set( 'ctrl + A', spy );
  41. const wasHandled = keystrokes.press( keyEvtData );
  42. sinon.assert.calledOnce( spy );
  43. sinon.assert.calledWithExactly( spy, keyEvtData, sinon.match.func );
  44. expect( wasHandled ).to.be.true;
  45. } );
  46. it( 'provides a callback which both preventDefault and stopPropagation', ( done ) => {
  47. const keyEvtData = getCtrlA();
  48. Object.assign( keyEvtData, {
  49. preventDefault: sinon.spy(),
  50. stopPropagation: sinon.spy()
  51. } );
  52. keystrokes.set( 'ctrl + A', ( data, cancel ) => {
  53. expect( data ).to.equal( keyEvtData );
  54. sinon.assert.notCalled( keyEvtData.preventDefault );
  55. sinon.assert.notCalled( keyEvtData.stopPropagation );
  56. cancel();
  57. sinon.assert.calledOnce( keyEvtData.preventDefault );
  58. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  59. done();
  60. } );
  61. emitter.fire( 'keydown', keyEvtData );
  62. } );
  63. it( 'returns false when no handler', () => {
  64. const keyEvtData = getCtrlA();
  65. const wasHandled = keystrokes.press( keyEvtData );
  66. expect( wasHandled ).to.be.false;
  67. } );
  68. } );
  69. describe( 'set()', () => {
  70. it( 'handles array format', () => {
  71. const spy = sinon.spy();
  72. keystrokes.set( [ 'ctrl', 'A' ], spy );
  73. expect( keystrokes.press( getCtrlA() ) ).to.be.true;
  74. } );
  75. it( 'aggregates multiple callbacks for the same keystroke', () => {
  76. const spy1 = sinon.spy();
  77. const spy2 = sinon.spy();
  78. keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  79. keystrokes.set( [ 'ctrl', 'A' ], spy2 );
  80. keystrokes.press( getCtrlA() );
  81. sinon.assert.calledOnce( spy1 );
  82. sinon.assert.calledOnce( spy2 );
  83. } );
  84. } );
  85. describe( 'destroy()', () => {
  86. it( 'detaches #keydown listener', () => {
  87. const spy = sinon.spy( keystrokes, 'press' );
  88. keystrokes.destroy();
  89. emitter.fire( 'keydown', { keyCode: 1 } );
  90. sinon.assert.notCalled( spy );
  91. } );
  92. it( 'removes all keystrokes', () => {
  93. const spy = sinon.spy();
  94. const keystrokeHandler = keystrokes;
  95. keystrokeHandler.set( 'ctrl + A', spy );
  96. keystrokeHandler.destroy();
  97. const wasHandled = keystrokeHandler.press( getCtrlA() );
  98. expect( wasHandled ).to.be.false;
  99. sinon.assert.notCalled( spy );
  100. } );
  101. } );
  102. } );
  103. function getCtrlA() {
  104. return { keyCode: keyCodes.a, ctrlKey: true };
  105. }