8
0

focusobserver.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import FocusObserver from '../../../src/view/observer/focusobserver';
  6. import ViewDocument from '../../../src/view/document';
  7. import ViewRange from '../../../src/view/range';
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. const setTimeout = global.window.setTimeout;
  10. describe( 'FocusObserver', () => {
  11. let viewDocument, observer;
  12. beforeEach( () => {
  13. viewDocument = new ViewDocument();
  14. observer = viewDocument.getObserver( FocusObserver );
  15. } );
  16. afterEach( () => {
  17. viewDocument.destroy();
  18. } );
  19. it( 'should define domEventType', () => {
  20. expect( observer.domEventType ).to.deep.equal( [ 'focus', 'blur' ] );
  21. } );
  22. it( 'should use capturing phase', () => {
  23. expect( observer.useCapture ).to.be.true;
  24. } );
  25. describe( 'onDomEvent', () => {
  26. it( 'should fire focus with the right event data', () => {
  27. const spy = sinon.spy();
  28. viewDocument.on( 'focus', spy );
  29. observer.onDomEvent( { type: 'focus', target: global.document.body } );
  30. expect( spy.calledOnce ).to.be.true;
  31. const data = spy.args[ 0 ][ 1 ];
  32. expect( data.domTarget ).to.equal( global.document.body );
  33. } );
  34. it( 'should fire blur with the right event data', () => {
  35. const spy = sinon.spy();
  36. viewDocument.on( 'blur', spy );
  37. observer.onDomEvent( { type: 'blur', target: global.document.body } );
  38. expect( spy.calledOnce ).to.be.true;
  39. const data = spy.args[ 0 ][ 1 ];
  40. expect( data.domTarget ).to.equal( global.document.body );
  41. } );
  42. it( 'should render document after blurring', () => {
  43. const renderSpy = sinon.spy( viewDocument, 'render' );
  44. observer.onDomEvent( { type: 'blur', target: global.document.body } );
  45. sinon.assert.calledOnce( renderSpy );
  46. } );
  47. } );
  48. describe( 'handle isFocused property of the document', () => {
  49. let domMain, domHeader, viewMain, viewHeader;
  50. beforeEach( () => {
  51. domMain = global.document.createElement( 'div' );
  52. domHeader = global.document.createElement( 'h1' );
  53. viewMain = viewDocument.createRoot( domMain );
  54. viewHeader = viewDocument.createRoot( domHeader, 'header' );
  55. } );
  56. it( 'should set isFocused to true on focus', () => {
  57. observer.onDomEvent( { type: 'focus', target: domMain } );
  58. expect( viewDocument.isFocused ).to.equal( true );
  59. } );
  60. it( 'should set isFocused to false on blur', () => {
  61. observer.onDomEvent( { type: 'focus', target: domMain } );
  62. expect( viewDocument.isFocused ).to.equal( true );
  63. observer.onDomEvent( { type: 'blur', target: domMain } );
  64. expect( viewDocument.isFocused ).to.be.false;
  65. } );
  66. it( 'should set isFocused to false on blur when selection in same editable', () => {
  67. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  68. observer.onDomEvent( { type: 'focus', target: domMain } );
  69. expect( viewDocument.isFocused ).to.equal( true );
  70. observer.onDomEvent( { type: 'blur', target: domMain } );
  71. expect( viewDocument.isFocused ).to.be.false;
  72. } );
  73. it( 'should not set isFocused to false on blur when it is fired on other editable', () => {
  74. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  75. observer.onDomEvent( { type: 'focus', target: domMain } );
  76. expect( viewDocument.isFocused ).to.equal( true );
  77. observer.onDomEvent( { type: 'blur', target: domHeader } );
  78. expect( viewDocument.isFocused ).to.be.true;
  79. } );
  80. it( 'should delay rendering to the next iteration of event loop', ( done ) => {
  81. const renderSpy = sinon.spy( viewDocument, 'render' );
  82. observer.onDomEvent( { type: 'focus', target: domMain } );
  83. sinon.assert.notCalled( renderSpy );
  84. setTimeout( () => {
  85. sinon.assert.called( renderSpy );
  86. done();
  87. }, 0 );
  88. } );
  89. it( 'should not call render if destroyed', ( done ) => {
  90. const renderSpy = sinon.spy( viewDocument, 'render' );
  91. observer.onDomEvent( { type: 'focus', target: domMain } );
  92. sinon.assert.notCalled( renderSpy );
  93. observer.destroy();
  94. setTimeout( () => {
  95. sinon.assert.notCalled( renderSpy );
  96. done();
  97. }, 0 );
  98. } );
  99. } );
  100. } );