compositionobserver.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 CompositionObserver from '../../../src/view/observer/compositionobserver';
  7. import View from '../../../src/view/view';
  8. import { StylesProcessor } from '../../../src/view/stylesmap';
  9. describe( 'CompositionObserver', () => {
  10. let view, viewDocument, observer;
  11. beforeEach( () => {
  12. view = new View( new StylesProcessor() );
  13. viewDocument = view.document;
  14. observer = view.getObserver( CompositionObserver );
  15. } );
  16. afterEach( () => {
  17. view.destroy();
  18. } );
  19. it( 'should define domEventType', () => {
  20. expect( observer.domEventType ).to.deep.equal( [ 'compositionstart', 'compositionupdate', 'compositionend' ] );
  21. } );
  22. describe( 'onDomEvent', () => {
  23. it( 'should fire compositionstart with the right event data', () => {
  24. const spy = sinon.spy();
  25. viewDocument.on( 'compositionstart', spy );
  26. observer.onDomEvent( { type: 'compositionstart', target: document.body } );
  27. expect( spy.calledOnce ).to.be.true;
  28. const data = spy.args[ 0 ][ 1 ];
  29. expect( data.domTarget ).to.equal( document.body );
  30. } );
  31. it( 'should fire compositionupdate with the right event data', () => {
  32. const spy = sinon.spy();
  33. viewDocument.on( 'compositionupdate', spy );
  34. observer.onDomEvent( { type: 'compositionupdate', target: document.body } );
  35. expect( spy.calledOnce ).to.be.true;
  36. const data = spy.args[ 0 ][ 1 ];
  37. expect( data.domTarget ).to.equal( document.body );
  38. } );
  39. it( 'should fire compositionend with the right event data', () => {
  40. const spy = sinon.spy();
  41. viewDocument.on( 'compositionend', spy );
  42. observer.onDomEvent( { type: 'compositionend', target: document.body } );
  43. expect( spy.calledOnce ).to.be.true;
  44. const data = spy.args[ 0 ][ 1 ];
  45. expect( data.domTarget ).to.equal( document.body );
  46. } );
  47. } );
  48. describe( 'handle isComposing property of the document', () => {
  49. let domMain;
  50. beforeEach( () => {
  51. domMain = document.createElement( 'div' );
  52. } );
  53. it( 'should set isComposing to true on compositionstart', () => {
  54. observer.onDomEvent( { type: 'compositionstart', target: domMain } );
  55. expect( viewDocument.isComposing ).to.equal( true );
  56. } );
  57. it( 'should set isComposing to false on compositionend', () => {
  58. observer.onDomEvent( { type: 'compositionstart', target: domMain } );
  59. expect( viewDocument.isComposing ).to.equal( true );
  60. observer.onDomEvent( { type: 'compositionend', target: domMain } );
  61. expect( viewDocument.isComposing ).to.equal( false );
  62. } );
  63. it( 'should not change isComposing on compositionupdate during composition', () => {
  64. observer.onDomEvent( { type: 'compositionstart', target: domMain } );
  65. expect( viewDocument.isComposing ).to.equal( true );
  66. observer.onDomEvent( { type: 'compositionupdate', target: domMain } );
  67. expect( viewDocument.isComposing ).to.equal( true );
  68. } );
  69. it( 'should not change isComposing on compositionupdate outside composition', () => {
  70. expect( viewDocument.isComposing ).to.equal( false );
  71. observer.onDomEvent( { type: 'compositionupdate', target: domMain } );
  72. expect( viewDocument.isComposing ).to.equal( false );
  73. } );
  74. } );
  75. } );