keystrokehandler.js 3.3 KB

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