keyobserver.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: view, browser-only */
  7. import KeyObserver from 'ckeditor5/engine/view/observer/keyobserver.js';
  8. import ViewDocument from 'ckeditor5/engine/view/document.js';
  9. import { getCode } from 'ckeditor5/utils/keyboard.js';
  10. describe( 'KeyObserver', () => {
  11. let viewDocument, observer;
  12. beforeEach( () => {
  13. viewDocument = new ViewDocument();
  14. observer = viewDocument.getObserver( KeyObserver );
  15. } );
  16. afterEach( () => {
  17. viewDocument.destroy();
  18. } );
  19. it( 'should define domEventType', () => {
  20. expect( observer.domEventType ).to.equal( 'keydown' );
  21. } );
  22. describe( 'onDomEvent', () => {
  23. it( 'should fire keydown with the target and key info', () => {
  24. const spy = sinon.spy();
  25. viewDocument.on( 'keydown', spy );
  26. observer.onDomEvent( { target: document.body, keyCode: 111, altKey: false, ctrlKey: false, metaKey: false, shiftKey: false } );
  27. expect( spy.calledOnce ).to.be.true;
  28. const data = spy.args[ 0 ][ 1 ];
  29. expect( data ).to.have.property( 'domTarget', document.body );
  30. expect( data ).to.have.property( 'keyCode', 111 );
  31. expect( data ).to.have.property( 'altKey', false );
  32. expect( data ).to.have.property( 'ctrlKey', false );
  33. expect( data ).to.have.property( 'shiftKey', false );
  34. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  35. // Just to be sure.
  36. expect( getCode( data ) ).to.equal( 111 );
  37. } );
  38. it( 'should fire keydown with proper key modifiers info', () => {
  39. const spy = sinon.spy();
  40. viewDocument.on( 'keydown', spy );
  41. observer.onDomEvent( { target: document.body, keyCode: 111, altKey: true, ctrlKey: true, metaKey: false, shiftKey: true } );
  42. const data = spy.args[ 0 ][ 1 ];
  43. expect( data ).to.have.property( 'keyCode', 111 );
  44. expect( data ).to.have.property( 'altKey', true );
  45. expect( data ).to.have.property( 'ctrlKey', true );
  46. expect( data ).to.have.property( 'shiftKey', true );
  47. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  48. // Just to be sure.
  49. expect( getCode( data ) ).to.be.greaterThan( 111 );
  50. } );
  51. it( 'should fire keydown ctrlKey set to true one meta (cmd) was pressed', () => {
  52. const spy = sinon.spy();
  53. viewDocument.on( 'keydown', spy );
  54. observer.onDomEvent( { target: document.body, keyCode: 111, metaKey: true } );
  55. const data = spy.args[ 0 ][ 1 ];
  56. expect( data ).to.have.property( 'ctrlKey', true );
  57. } );
  58. } );
  59. } );