keystrokehandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import KeystrokeHandler from 'ckeditor5-core/src/keystrokehandler';
  7. import { keyCodes } from 'ckeditor5-utils/src/keyboard';
  8. describe( 'KeystrokeHandler', () => {
  9. let editor;
  10. beforeEach( () => {
  11. return VirtualTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. editor.keystrokes = new KeystrokeHandler( editor );
  15. } );
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'triggers #press on #keydown', () => {
  19. const spy = sinon.spy( editor.keystrokes, 'press' );
  20. const keyEvtData = { keyCode: 1 };
  21. editor.editing.view.fire( 'keydown', keyEvtData );
  22. expect( spy.calledOnce ).to.be.true;
  23. expect( spy.calledWithExactly( keyEvtData ) );
  24. } );
  25. it( 'prevents default when keystroke was handled', () => {
  26. editor.keystrokes.press = () => true;
  27. const keyEvtData = { keyCode: 1, preventDefault: sinon.spy() };
  28. editor.editing.view.fire( 'keydown', keyEvtData );
  29. expect( keyEvtData.preventDefault.calledOnce ).to.be.true;
  30. } );
  31. } );
  32. describe( 'press', () => {
  33. it( 'executes a command', () => {
  34. const spy = sinon.stub( editor, 'execute' );
  35. editor.keystrokes.set( 'ctrl + A', 'foo' );
  36. const wasHandled = editor.keystrokes.press( getCtrlA() );
  37. expect( spy.calledOnce ).to.be.true;
  38. expect( spy.calledWithExactly( 'foo' ) ).to.be.true;
  39. expect( wasHandled ).to.be.true;
  40. } );
  41. it( 'executes a callback', () => {
  42. const spy = sinon.spy();
  43. const keyEvtData = getCtrlA();
  44. editor.keystrokes.set( 'ctrl + A', spy );
  45. const wasHandled = editor.keystrokes.press( keyEvtData );
  46. expect( spy.calledOnce ).to.be.true;
  47. expect( spy.calledWithExactly( keyEvtData ) ).to.be.true;
  48. expect( wasHandled ).to.be.true;
  49. } );
  50. it( 'returns false when no handler', () => {
  51. const keyEvtData = getCtrlA();
  52. const wasHandled = editor.keystrokes.press( keyEvtData );
  53. expect( wasHandled ).to.be.false;
  54. } );
  55. } );
  56. describe( 'set', () => {
  57. it( 'handles array format', () => {
  58. const spy = sinon.spy();
  59. editor.keystrokes.set( [ 'ctrl', 'A' ], spy );
  60. expect( editor.keystrokes.press( getCtrlA() ) ).to.be.true;
  61. } );
  62. it( 'overrides existing keystroke', () => {
  63. const spy1 = sinon.spy();
  64. const spy2 = sinon.spy();
  65. editor.keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  66. editor.keystrokes.set( [ 'ctrl', 'A' ], spy2 );
  67. editor.keystrokes.press( getCtrlA() );
  68. expect( spy1.calledOnce ).to.be.false;
  69. expect( spy2.calledOnce ).to.be.true;
  70. } );
  71. } );
  72. describe( 'destroy', () => {
  73. it( 'detaches #keydown listener', () => {
  74. const spy = sinon.spy( editor.keystrokes, 'press' );
  75. editor.keystrokes.destroy();
  76. editor.editing.view.fire( 'keydown', { keyCode: 1 } );
  77. expect( spy.called ).to.be.false;
  78. } );
  79. it( 'removes all keystrokes', () => {
  80. const spy = sinon.spy();
  81. const keystrokeHandler = editor.keystrokes;
  82. keystrokeHandler.set( 'ctrl + A', spy );
  83. keystrokeHandler.destroy();
  84. const wasHandled = keystrokeHandler.press( getCtrlA() );
  85. expect( wasHandled ).to.be.false;
  86. expect( spy.called ).to.be.false;
  87. } );
  88. } );
  89. } );
  90. function getCtrlA() {
  91. return { keyCode: keyCodes.a, ctrlKey: true };
  92. }