editingkeystrokehandler.js 2.1 KB

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