| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import EmitterMixin from 'ckeditor5-utils/src/emittermixin';
- import KeystrokeHandler from 'ckeditor5-utils/src/keystrokehandler';
- import { keyCodes } from 'ckeditor5-utils/src/keyboard';
- describe( 'KeystrokeHandler', () => {
- let emitter, keystrokes;
- beforeEach( () => {
- emitter = Object.create( EmitterMixin );
- keystrokes = new KeystrokeHandler();
- keystrokes.listenTo( emitter );
- } );
- describe( 'listenTo()', () => {
- it( 'activates the listening on the emitter', () => {
- emitter = Object.create( EmitterMixin );
- keystrokes = new KeystrokeHandler();
- const spy = sinon.spy( keystrokes, 'press' );
- const keyEvtData = { keyCode: 1 };
- emitter.fire( 'keydown', keyEvtData );
- expect( spy.notCalled ).to.be.true;
- keystrokes.listenTo( emitter );
- emitter.fire( 'keydown', keyEvtData );
- sinon.assert.calledOnce( spy );
- sinon.assert.calledWithExactly( spy, keyEvtData );
- } );
- it( 'triggers #press on #keydown', () => {
- const spy = sinon.spy( keystrokes, 'press' );
- const keyEvtData = { keyCode: 1 };
- emitter.fire( 'keydown', keyEvtData );
- sinon.assert.calledOnce( spy );
- sinon.assert.calledWithExactly( spy, keyEvtData );
- } );
- } );
- describe( 'press()', () => {
- it( 'executes a callback', () => {
- const spy = sinon.spy();
- const keyEvtData = getCtrlA();
- keystrokes.set( 'ctrl + A', spy );
- const wasHandled = keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( spy );
- sinon.assert.calledWithExactly( spy, keyEvtData, sinon.match.func );
- expect( wasHandled ).to.be.true;
- } );
- it( 'provides a callback which both preventDefault and stopPropagation', ( done ) => {
- const keyEvtData = getCtrlA();
- Object.assign( keyEvtData, {
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- } );
- keystrokes.set( 'ctrl + A', ( data, cancel ) => {
- expect( data ).to.equal( keyEvtData );
- sinon.assert.notCalled( keyEvtData.preventDefault );
- sinon.assert.notCalled( keyEvtData.stopPropagation );
- cancel();
- sinon.assert.calledOnce( keyEvtData.preventDefault );
- sinon.assert.calledOnce( keyEvtData.stopPropagation );
- done();
- } );
- emitter.fire( 'keydown', keyEvtData );
- } );
- it( 'returns false when no handler', () => {
- const keyEvtData = getCtrlA();
- const wasHandled = keystrokes.press( keyEvtData );
- expect( wasHandled ).to.be.false;
- } );
- } );
- describe( 'set()', () => {
- it( 'handles array format', () => {
- const spy = sinon.spy();
- keystrokes.set( [ 'ctrl', 'A' ], spy );
- expect( keystrokes.press( getCtrlA() ) ).to.be.true;
- } );
- it( 'aggregates multiple callbacks for the same keystroke', () => {
- const spy1 = sinon.spy();
- const spy2 = sinon.spy();
- keystrokes.set( [ 'ctrl', 'A' ], spy1 );
- keystrokes.set( [ 'ctrl', 'A' ], spy2 );
- keystrokes.press( getCtrlA() );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- } );
- } );
- describe( 'destroy()', () => {
- it( 'detaches #keydown listener', () => {
- const spy = sinon.spy( keystrokes, 'press' );
- keystrokes.destroy();
- emitter.fire( 'keydown', { keyCode: 1 } );
- sinon.assert.notCalled( spy );
- } );
- it( 'removes all keystrokes', () => {
- const spy = sinon.spy();
- const keystrokeHandler = keystrokes;
- keystrokeHandler.set( 'ctrl + A', spy );
- keystrokeHandler.destroy();
- const wasHandled = keystrokeHandler.press( getCtrlA() );
- expect( wasHandled ).to.be.false;
- sinon.assert.notCalled( spy );
- } );
- } );
- } );
- function getCtrlA() {
- return { keyCode: keyCodes.a, ctrlKey: true };
- }
|