|
|
@@ -8,55 +8,68 @@ import EditingKeystrokeHandler from '../src/editingkeystrokehandler';
|
|
|
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
|
|
|
|
|
|
describe( 'EditingKeystrokeHandler', () => {
|
|
|
- let editor, keystrokes;
|
|
|
+ let editor, keystrokes, executeSpy;
|
|
|
|
|
|
beforeEach( () => {
|
|
|
return VirtualTestEditor.create()
|
|
|
.then( newEditor => {
|
|
|
editor = newEditor;
|
|
|
keystrokes = new EditingKeystrokeHandler( editor );
|
|
|
+ executeSpy = sinon.stub( editor, 'execute' );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'listenTo()', () => {
|
|
|
- it( 'prevents default when keystroke was handled', () => {
|
|
|
- const keyEvtData = { keyCode: 1, preventDefault: sinon.spy() };
|
|
|
+ describe( 'set()', () => {
|
|
|
+ describe( 'with a command', () => {
|
|
|
+ it( 'prevents default when the keystroke was handled', () => {
|
|
|
+ const keyEvtData = getCtrlA();
|
|
|
|
|
|
- sinon.stub( keystrokes, 'press' ).returns( true );
|
|
|
+ keystrokes.set( 'ctrl + A', 'foo' );
|
|
|
+ keystrokes.press( keyEvtData );
|
|
|
|
|
|
- keystrokes.listenTo( editor.editing.view );
|
|
|
- editor.editing.view.fire( 'keydown', keyEvtData );
|
|
|
+ sinon.assert.calledWithExactly( executeSpy, 'foo' );
|
|
|
+ sinon.assert.calledOnce( keyEvtData.preventDefault );
|
|
|
+ sinon.assert.calledOnce( keyEvtData.stopPropagation );
|
|
|
+ } );
|
|
|
|
|
|
- sinon.assert.calledOnce( keyEvtData.preventDefault );
|
|
|
- } );
|
|
|
+ it( 'does not prevent default when the keystroke was not handled', () => {
|
|
|
+ const keyEvtData = getCtrlA();
|
|
|
|
|
|
- it( 'does not prevent default when keystroke was not handled', () => {
|
|
|
- const keyEvtData = { keyCode: 1, preventDefault: sinon.spy() };
|
|
|
+ keystrokes.press( keyEvtData );
|
|
|
|
|
|
- sinon.stub( keystrokes, 'press' ).returns( false );
|
|
|
+ sinon.assert.notCalled( executeSpy );
|
|
|
+ sinon.assert.notCalled( keyEvtData.preventDefault );
|
|
|
+ sinon.assert.notCalled( keyEvtData.stopPropagation );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- keystrokes.listenTo( editor.editing.view );
|
|
|
- editor.editing.view.fire( 'keydown', keyEvtData );
|
|
|
+ describe( 'with a callback', () => {
|
|
|
+ it( 'never prevents default', () => {
|
|
|
+ const callback = sinon.spy();
|
|
|
+ const keyEvtData = getCtrlA();
|
|
|
|
|
|
- sinon.assert.notCalled( keyEvtData.preventDefault );
|
|
|
+ keystrokes.set( 'ctrl + A', callback );
|
|
|
+ keystrokes.press( keyEvtData );
|
|
|
+
|
|
|
+ sinon.assert.calledOnce( callback );
|
|
|
+ sinon.assert.notCalled( keyEvtData.preventDefault );
|
|
|
+ sinon.assert.notCalled( keyEvtData.stopPropagation );
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
describe( 'press()', () => {
|
|
|
it( 'executes a command', () => {
|
|
|
- const spy = sinon.stub( editor, 'execute' );
|
|
|
-
|
|
|
keystrokes.set( 'ctrl + A', 'foo' );
|
|
|
|
|
|
const wasHandled = keystrokes.press( getCtrlA() );
|
|
|
|
|
|
- sinon.assert.calledOnce( spy );
|
|
|
- sinon.assert.calledWithExactly( spy, 'foo' );
|
|
|
+ sinon.assert.calledOnce( executeSpy );
|
|
|
+ sinon.assert.calledWithExactly( executeSpy, 'foo' );
|
|
|
expect( wasHandled ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
it( 'executes a callback', () => {
|
|
|
- const executeSpy = sinon.stub( editor, 'execute' );
|
|
|
const callback = sinon.spy();
|
|
|
|
|
|
keystrokes.set( 'ctrl + A', callback );
|
|
|
@@ -71,5 +84,10 @@ describe( 'EditingKeystrokeHandler', () => {
|
|
|
} );
|
|
|
|
|
|
function getCtrlA() {
|
|
|
- return { keyCode: keyCodes.a, ctrlKey: true };
|
|
|
+ return {
|
|
|
+ keyCode: keyCodes.a,
|
|
|
+ ctrlKey: true,
|
|
|
+ preventDefault: sinon.spy(),
|
|
|
+ stopPropagation: sinon.spy()
|
|
|
+ };
|
|
|
}
|