8
0

focusobserver.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import FocusObserver from 'ckeditor5-engine/src/view/observer/focusobserver';
  7. import ViewDocument from 'ckeditor5-engine/src/view/document';
  8. import ViewRange from 'ckeditor5-engine/src/view/range';
  9. describe( 'FocusObserver', () => {
  10. let viewDocument, observer;
  11. beforeEach( () => {
  12. viewDocument = new ViewDocument();
  13. observer = viewDocument.getObserver( FocusObserver );
  14. } );
  15. afterEach( () => {
  16. viewDocument.destroy();
  17. } );
  18. it( 'should define domEventType', () => {
  19. expect( observer.domEventType ).to.deep.equal( [ 'focus', 'blur' ] );
  20. } );
  21. describe( 'onDomEvent', () => {
  22. it( 'should fire focus with the right event data', () => {
  23. const spy = sinon.spy();
  24. viewDocument.on( 'focus', spy );
  25. observer.onDomEvent( { type: 'focus', 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 blur with the right event data', () => {
  31. const spy = sinon.spy();
  32. viewDocument.on( 'blur', spy );
  33. observer.onDomEvent( { type: 'blur', 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. } );
  39. describe( 'handle isFocused property of the document', () => {
  40. let domMain, domHeader, viewMain, viewHeader;
  41. beforeEach( () => {
  42. domMain = document.createElement( 'div' );
  43. domHeader = document.createElement( 'h1' );
  44. viewMain = viewDocument.createRoot( domMain );
  45. viewHeader = viewDocument.createRoot( domHeader, 'header' );
  46. } );
  47. it( 'should set isFocused to true on focus', () => {
  48. observer.onDomEvent( { type: 'focus', target: domMain } );
  49. expect( viewDocument.isFocused ).to.equal( true );
  50. } );
  51. it( 'should set isFocused to false on blur', () => {
  52. observer.onDomEvent( { type: 'focus', target: domMain } );
  53. expect( viewDocument.isFocused ).to.equal( true );
  54. observer.onDomEvent( { type: 'blur', target: domMain } );
  55. expect( viewDocument.isFocused ).to.be.false;
  56. } );
  57. it( 'should set isFocused to false on blur when selection in same editable', () => {
  58. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  59. observer.onDomEvent( { type: 'focus', target: domMain } );
  60. expect( viewDocument.isFocused ).to.equal( true );
  61. observer.onDomEvent( { type: 'blur', target: domMain } );
  62. expect( viewDocument.isFocused ).to.be.false;
  63. } );
  64. it( 'should not set isFocused to false on blur when it is fired on other editable', () => {
  65. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  66. observer.onDomEvent( { type: 'focus', target: domMain } );
  67. expect( viewDocument.isFocused ).to.equal( true );
  68. observer.onDomEvent( { type: 'blur', target: domHeader } );
  69. expect( viewDocument.isFocused ).to.be.true;
  70. } );
  71. } );
  72. } );