editingkeystrokehandler.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '../tests/_utils/virtualtesteditor';
  6. import EditingKeystrokeHandler from '../src/editingkeystrokehandler';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. describe( 'EditingKeystrokeHandler', () => {
  9. let editor, keystrokes, executeSpy;
  10. beforeEach( () => {
  11. return VirtualTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. keystrokes = new EditingKeystrokeHandler( editor );
  15. executeSpy = sinon.stub( editor, 'execute' );
  16. } );
  17. } );
  18. describe( 'set()', () => {
  19. describe( 'with a command', () => {
  20. it( 'prevents default when the keystroke was handled', () => {
  21. const keyEvtData = getCtrlA();
  22. keystrokes.set( 'Ctrl+A', 'foo' );
  23. keystrokes.press( keyEvtData );
  24. sinon.assert.calledWithExactly( executeSpy, 'foo' );
  25. sinon.assert.calledOnce( keyEvtData.preventDefault );
  26. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  27. } );
  28. it( 'does not prevent default when the keystroke was not handled', () => {
  29. const keyEvtData = getCtrlA();
  30. keystrokes.press( keyEvtData );
  31. sinon.assert.notCalled( executeSpy );
  32. sinon.assert.notCalled( keyEvtData.preventDefault );
  33. sinon.assert.notCalled( keyEvtData.stopPropagation );
  34. } );
  35. } );
  36. describe( 'with a callback', () => {
  37. it( 'never prevents default', () => {
  38. const callback = sinon.spy();
  39. const keyEvtData = getCtrlA();
  40. keystrokes.set( 'Ctrl+A', callback );
  41. keystrokes.press( keyEvtData );
  42. sinon.assert.calledOnce( callback );
  43. sinon.assert.notCalled( keyEvtData.preventDefault );
  44. sinon.assert.notCalled( keyEvtData.stopPropagation );
  45. } );
  46. } );
  47. } );
  48. describe( 'press()', () => {
  49. it( 'executes a command', () => {
  50. keystrokes.set( 'Ctrl+A', 'foo' );
  51. const wasHandled = keystrokes.press( getCtrlA() );
  52. sinon.assert.calledOnce( executeSpy );
  53. sinon.assert.calledWithExactly( executeSpy, 'foo' );
  54. expect( wasHandled ).to.be.true;
  55. } );
  56. it( 'executes a callback', () => {
  57. const callback = sinon.spy();
  58. keystrokes.set( 'Ctrl+A', callback );
  59. const wasHandled = keystrokes.press( getCtrlA() );
  60. expect( executeSpy.called ).to.be.false;
  61. expect( callback.calledOnce ).to.be.true;
  62. expect( wasHandled ).to.be.true;
  63. } );
  64. } );
  65. } );
  66. function getCtrlA() {
  67. return {
  68. keyCode: keyCodes.a,
  69. ctrlKey: true,
  70. preventDefault: sinon.spy(),
  71. stopPropagation: sinon.spy()
  72. };
  73. }