8
0

compositionobserver.js 3.2 KB

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