keyobserver.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import KeyObserver from '../../../src/view/observer/keyobserver';
  7. import ViewDocument from '../../../src/view/document';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. describe( 'KeyObserver', () => {
  10. let viewDocument, observer;
  11. beforeEach( () => {
  12. viewDocument = new ViewDocument();
  13. observer = viewDocument.getObserver( KeyObserver );
  14. } );
  15. afterEach( () => {
  16. viewDocument.destroy();
  17. } );
  18. it( 'should define domEventType', () => {
  19. expect( observer.domEventType ).to.contains( 'keydown' );
  20. expect( observer.domEventType ).to.contains( 'keyup' );
  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( {
  27. type: 'keydown',
  28. target: document.body,
  29. keyCode: 111,
  30. altKey: false,
  31. ctrlKey: false,
  32. metaKey: false,
  33. shiftKey: false
  34. } );
  35. expect( spy.calledOnce ).to.be.true;
  36. const data = spy.args[ 0 ][ 1 ];
  37. expect( data ).to.have.property( 'domTarget', document.body );
  38. expect( data ).to.have.property( 'keyCode', 111 );
  39. expect( data ).to.have.property( 'altKey', false );
  40. expect( data ).to.have.property( 'ctrlKey', false );
  41. expect( data ).to.have.property( 'shiftKey', false );
  42. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  43. // Just to be sure.
  44. expect( getCode( data ) ).to.equal( 111 );
  45. } );
  46. it( 'should fire keydown with proper key modifiers info', () => {
  47. const spy = sinon.spy();
  48. viewDocument.on( 'keydown', spy );
  49. observer.onDomEvent( {
  50. type: 'keydown',
  51. target: document.body,
  52. keyCode: 111,
  53. altKey: true,
  54. ctrlKey: true,
  55. metaKey: false,
  56. shiftKey: true
  57. } );
  58. const data = spy.args[ 0 ][ 1 ];
  59. expect( data ).to.have.property( 'keyCode', 111 );
  60. expect( data ).to.have.property( 'altKey', true );
  61. expect( data ).to.have.property( 'ctrlKey', true );
  62. expect( data ).to.have.property( 'shiftKey', true );
  63. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  64. // Just to be sure.
  65. expect( getCode( data ) ).to.be.greaterThan( 111 );
  66. } );
  67. it( 'should fire keydown ctrlKey set to true one meta (cmd) was pressed', () => {
  68. const spy = sinon.spy();
  69. viewDocument.on( 'keydown', spy );
  70. observer.onDomEvent( { type: 'keydown', target: document.body, keyCode: 111, metaKey: true } );
  71. const data = spy.args[ 0 ][ 1 ];
  72. expect( data ).to.have.property( 'ctrlKey', true );
  73. } );
  74. it( 'should fire keyup with the target and key info', () => {
  75. const spy = sinon.spy();
  76. viewDocument.on( 'keyup', spy );
  77. observer.onDomEvent( {
  78. type: 'keyup',
  79. target: document.body,
  80. keyCode: 111,
  81. altKey: false,
  82. ctrlKey: false,
  83. metaKey: false,
  84. shiftKey: false
  85. } );
  86. expect( spy.calledOnce ).to.be.true;
  87. const data = spy.args[ 0 ][ 1 ];
  88. expect( data ).to.have.property( 'domTarget', document.body );
  89. expect( data ).to.have.property( 'keyCode', 111 );
  90. expect( data ).to.have.property( 'altKey', false );
  91. expect( data ).to.have.property( 'ctrlKey', false );
  92. expect( data ).to.have.property( 'shiftKey', false );
  93. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  94. // Just to be sure.
  95. expect( getCode( data ) ).to.equal( 111 );
  96. } );
  97. } );
  98. } );