8
0

compositionobserver.js 3.3 KB

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