editingkeystrokehandler.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 testUtils from 'ckeditor5-core/tests/_utils/utils';
  8. import { keyCodes } from 'ckeditor5-utils/src/keyboard';
  9. testUtils.createSinonSandbox();
  10. describe( 'EditingKeystrokeHandler', () => {
  11. let editor;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. editor.keystrokes = new EditingKeystrokeHandler( editor );
  17. } );
  18. } );
  19. describe( 'listenTo()', () => {
  20. it( 'prevents default when keystroke was handled', () => {
  21. const keyEvtData = { keyCode: 1, preventDefault: testUtils.sinon.spy() };
  22. testUtils.sinon.stub( EditingKeystrokeHandler.prototype, 'press' ).returns( true );
  23. editor.editing.view.fire( 'keydown', keyEvtData );
  24. sinon.assert.calledOnce( keyEvtData.preventDefault );
  25. } );
  26. } );
  27. describe( 'press()', () => {
  28. it( 'executes a command', () => {
  29. const spy = testUtils.sinon.stub( editor, 'execute' );
  30. editor.keystrokes.set( 'ctrl + A', 'foo' );
  31. const wasHandled = editor.keystrokes.press( getCtrlA() );
  32. sinon.assert.calledOnce( spy );
  33. sinon.assert.calledWithExactly( spy, 'foo' );
  34. expect( wasHandled ).to.be.true;
  35. } );
  36. } );
  37. } );
  38. function getCtrlA() {
  39. return { keyCode: keyCodes.a, ctrlKey: true };
  40. }