8
0

compositionobserver.js 3.3 KB

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