focustracker.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, FocusEvent */
  6. import FocusTracker from '../src/focustracker';
  7. import CKEditorError from '../src/ckeditorerror';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. testUtils.createSinonSandbox();
  10. describe( 'FocusTracker', () => {
  11. let focusTracker, container, containerFirstInput, containerSecondInput;
  12. beforeEach( () => {
  13. container = document.createElement( 'div' );
  14. containerFirstInput = document.createElement( 'input' );
  15. containerSecondInput = document.createElement( 'input' );
  16. container.appendChild( containerFirstInput );
  17. container.appendChild( containerSecondInput );
  18. testUtils.sinon.useFakeTimers();
  19. focusTracker = new FocusTracker();
  20. } );
  21. describe( 'constructor', () => {
  22. describe( 'isFocused', () => {
  23. it( 'should be false at default', () => {
  24. expect( focusTracker.isFocused ).to.false;
  25. } );
  26. it( 'should be observable', () => {
  27. const observableSpy = testUtils.sinon.spy();
  28. focusTracker.listenTo( focusTracker, 'change:isFocused', observableSpy );
  29. focusTracker.isFocused = true;
  30. expect( observableSpy.calledOnce ).to.true;
  31. } );
  32. } );
  33. } );
  34. describe( 'add', () => {
  35. it( 'should throw an error when element has been already added', () => {
  36. focusTracker.add( containerFirstInput );
  37. expect( () => {
  38. focusTracker.add( containerFirstInput );
  39. } ).to.throw( CKEditorError, /focusTracker-add-element-already-exist/ );
  40. } );
  41. describe( 'single element', () => {
  42. it( 'should start listening on element focus and update `isFocused` property', () => {
  43. focusTracker.add( containerFirstInput );
  44. expect( focusTracker.isFocused ).to.false;
  45. containerFirstInput.dispatchEvent( new FocusEvent( 'focus' ) );
  46. expect( focusTracker.isFocused ).to.true;
  47. } );
  48. it( 'should start listening on element blur and update `isFocused` property', () => {
  49. focusTracker.add( containerFirstInput );
  50. focusTracker.isFocused = true;
  51. containerFirstInput.dispatchEvent( new FocusEvent( 'blur' ) );
  52. expect( focusTracker.isFocused ).to.false;
  53. } );
  54. } );
  55. describe( 'container element', () => {
  56. it( 'should start listening on element focus using event capturing and update `isFocused` property', () => {
  57. focusTracker.add( container );
  58. expect( focusTracker.isFocused ).to.false;
  59. containerFirstInput.dispatchEvent( new FocusEvent( 'focus' ) );
  60. expect( focusTracker.isFocused ).to.true;
  61. } );
  62. it( 'should start listening on element blur using event capturing and update `isFocused` property', () => {
  63. focusTracker.add( container );
  64. focusTracker.isFocused = true;
  65. containerFirstInput.dispatchEvent( new FocusEvent( 'blur' ) );
  66. expect( focusTracker.isFocused ).to.false;
  67. } );
  68. it( 'should not change `isFocused` property when focus is going between child elements', () => {
  69. const changeSpy = testUtils.sinon.spy();
  70. focusTracker.add( container );
  71. containerFirstInput.dispatchEvent( new FocusEvent( 'focus' ) );
  72. focusTracker.listenTo( focusTracker, 'change:isFocused', changeSpy );
  73. expect( focusTracker.isFocused ).to.true;
  74. containerFirstInput.dispatchEvent( new FocusEvent( 'blur', {
  75. relatedTarget: containerSecondInput
  76. } ) );
  77. containerSecondInput.dispatchEvent( new FocusEvent( 'focus' ) );
  78. expect( focusTracker.isFocused ).to.true;
  79. expect( changeSpy.notCalled ).to.true;
  80. } );
  81. // https://github.com/ckeditor/ckeditor5-utils/issues/159
  82. it( 'should keep `isFocused` synced when multiple blur events are followed by the focus', () => {
  83. focusTracker.add( container );
  84. focusTracker.isFocused = true;
  85. container.dispatchEvent( new FocusEvent( 'blur' ) );
  86. containerFirstInput.dispatchEvent( new FocusEvent( 'blur' ) );
  87. containerSecondInput.dispatchEvent( new FocusEvent( 'focus' ) );
  88. expect( focusTracker.isFocused ).to.be.true;
  89. } );
  90. } );
  91. } );
  92. describe( 'remove', () => {
  93. it( 'should do nothing when element was not added', () => {
  94. expect( () => {
  95. focusTracker.remove( container );
  96. } ).to.not.throw();
  97. } );
  98. it( 'should stop listening on element focus', () => {
  99. focusTracker.add( containerFirstInput );
  100. focusTracker.remove( containerFirstInput );
  101. containerFirstInput.dispatchEvent( new FocusEvent( 'focus' ) );
  102. expect( focusTracker.isFocused ).to.false;
  103. } );
  104. it( 'should stop listening on element blur', () => {
  105. focusTracker.add( containerFirstInput );
  106. focusTracker.remove( containerFirstInput );
  107. focusTracker.isFocused = true;
  108. containerFirstInput.dispatchEvent( new FocusEvent( 'blur' ) );
  109. expect( focusTracker.isFocused ).to.true;
  110. } );
  111. it( 'should blur element before removing when is focused', () => {
  112. focusTracker.add( containerFirstInput );
  113. containerFirstInput.dispatchEvent( new FocusEvent( 'focus' ) );
  114. expect( focusTracker.isFocused ).to.true;
  115. focusTracker.remove( containerFirstInput );
  116. expect( focusTracker.isFocused ).to.false;
  117. } );
  118. } );
  119. } );