keyobserver.js 3.6 KB

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