keystrokehandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. 'use strict';
  7. import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
  8. import KeystrokeHandler from '/ckeditor5/keystrokehandler.js';
  9. import { keyCodes } from '/ckeditor5/utils/keyboard.js';
  10. describe( 'KeystrokeHandler', () => {
  11. let editor;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. editor.keystrokes = new KeystrokeHandler( editor );
  17. } );
  18. } );
  19. describe( 'constructor', () => {
  20. it( 'triggers #press on #keydown', () => {
  21. const spy = sinon.spy( editor.keystrokes, 'press' );
  22. const keyEvtData = { keyCode: 1 };
  23. editor.editing.view.fire( 'keydown', keyEvtData );
  24. expect( spy.calledOnce ).to.be.true;
  25. expect( spy.calledWithExactly( keyEvtData ) );
  26. } );
  27. it( 'prevents default when keystroke was handled', () => {
  28. editor.keystrokes.press = () => true;
  29. const keyEvtData = { keyCode: 1, preventDefault: sinon.spy() };
  30. editor.editing.view.fire( 'keydown', keyEvtData );
  31. expect( keyEvtData.preventDefault.calledOnce ).to.be.true;
  32. } );
  33. } );
  34. describe( 'press', () => {
  35. it( 'executes a command', () => {
  36. const spy = sinon.stub( editor, 'execute' );
  37. editor.keystrokes.set( 'ctrl + A', 'foo' );
  38. const wasHandled = editor.keystrokes.press( getCtrlA() );
  39. expect( spy.calledOnce ).to.be.true;
  40. expect( spy.calledWithExactly( 'foo' ) ).to.be.true;
  41. expect( wasHandled ).to.be.true;
  42. } );
  43. it( 'executes a callback', () => {
  44. const spy = sinon.spy();
  45. const keyEvtData = getCtrlA();
  46. editor.keystrokes.set( 'ctrl + A', spy );
  47. const wasHandled = editor.keystrokes.press( keyEvtData );
  48. expect( spy.calledOnce ).to.be.true;
  49. expect( spy.calledWithExactly( keyEvtData ) ).to.be.true;
  50. expect( wasHandled ).to.be.true;
  51. } );
  52. it( 'returns false when no handler', () => {
  53. const keyEvtData = getCtrlA();
  54. const wasHandled = editor.keystrokes.press( keyEvtData );
  55. expect( wasHandled ).to.be.false;
  56. } );
  57. } );
  58. describe( 'set', () => {
  59. it( 'handles array format', () => {
  60. const spy = sinon.spy();
  61. editor.keystrokes.set( [ 'ctrl', 'A' ], spy );
  62. expect( editor.keystrokes.press( getCtrlA() ) ).to.be.true;
  63. } );
  64. it( 'overrides existing keystroke', () => {
  65. const spy1 = sinon.spy();
  66. const spy2 = sinon.spy();
  67. editor.keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  68. editor.keystrokes.set( [ 'ctrl', 'A' ], spy2 );
  69. editor.keystrokes.press( getCtrlA() );
  70. expect( spy1.calledOnce ).to.be.false;
  71. expect( spy2.calledOnce ).to.be.true;
  72. } );
  73. } );
  74. describe( 'destroy', () => {
  75. it( 'detaches #keydown listener', () => {
  76. const spy = sinon.spy( editor.keystrokes, 'press' );
  77. editor.keystrokes.destroy();
  78. editor.editing.view.fire( 'keydown', { keyCode: 1 } );
  79. expect( spy.called ).to.be.false;
  80. } );
  81. it( 'removes all keystrokes', () => {
  82. const spy = sinon.spy();
  83. const keystrokeHandler = editor.keystrokes;
  84. keystrokeHandler.set( 'ctrl + A', spy );
  85. keystrokeHandler.destroy();
  86. const wasHandled = keystrokeHandler.press( getCtrlA() );
  87. expect( wasHandled ).to.be.false;
  88. expect( spy.called ).to.be.false;
  89. } );
  90. } );
  91. } );
  92. function getCtrlA() {
  93. return { keyCode: keyCodes.a, ctrlKey: true };
  94. }