focustracker.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event */
  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 Event( '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 Event( 'blur' ) );
  52. testUtils.sinon.clock.tick( 0 );
  53. expect( focusTracker.isFocused ).to.false;
  54. } );
  55. } );
  56. describe( 'container element', () => {
  57. it( 'should start listening on element focus using event capturing and update `isFocused` property', () => {
  58. focusTracker.add( container );
  59. expect( focusTracker.isFocused ).to.false;
  60. containerFirstInput.dispatchEvent( new Event( 'focus' ) );
  61. expect( focusTracker.isFocused ).to.true;
  62. } );
  63. it( 'should start listening on element blur using event capturing and update `isFocused` property', () => {
  64. focusTracker.add( container );
  65. focusTracker.isFocused = true;
  66. containerFirstInput.dispatchEvent( new Event( 'blur' ) );
  67. testUtils.sinon.clock.tick( 0 );
  68. expect( focusTracker.isFocused ).to.false;
  69. } );
  70. it( 'should not change `isFocused` property when focus is going between child elements', () => {
  71. const changeSpy = testUtils.sinon.spy();
  72. focusTracker.add( container );
  73. containerFirstInput.dispatchEvent( new Event( 'focus' ) );
  74. focusTracker.listenTo( focusTracker, 'change:isFocused', changeSpy );
  75. expect( focusTracker.isFocused ).to.true;
  76. containerFirstInput.dispatchEvent( new Event( 'blur' ) );
  77. containerSecondInput.dispatchEvent( new Event( 'focus' ) );
  78. testUtils.sinon.clock.tick( 0 );
  79. expect( focusTracker.isFocused ).to.true;
  80. expect( changeSpy.notCalled ).to.true;
  81. } );
  82. } );
  83. } );
  84. describe( 'remove', () => {
  85. it( 'should do nothing when element was not added', () => {
  86. expect( () => {
  87. focusTracker.remove( container );
  88. } ).to.not.throw();
  89. } );
  90. it( 'should stop listening on element focus', () => {
  91. focusTracker.add( containerFirstInput );
  92. focusTracker.remove( containerFirstInput );
  93. containerFirstInput.dispatchEvent( new Event( 'focus' ) );
  94. expect( focusTracker.isFocused ).to.false;
  95. } );
  96. it( 'should stop listening on element blur', () => {
  97. focusTracker.add( containerFirstInput );
  98. focusTracker.remove( containerFirstInput );
  99. focusTracker.isFocused = true;
  100. containerFirstInput.dispatchEvent( new Event( 'blur' ) );
  101. testUtils.sinon.clock.tick( 0 );
  102. expect( focusTracker.isFocused ).to.true;
  103. } );
  104. it( 'should blur element before removing when is focused', () => {
  105. focusTracker.add( containerFirstInput );
  106. containerFirstInput.dispatchEvent( new Event( 'focus' ) );
  107. expect( focusTracker.isFocused ).to.true;
  108. focusTracker.remove( containerFirstInput );
  109. testUtils.sinon.clock.tick( 0 );
  110. expect( focusTracker.isFocused ).to.false;
  111. } );
  112. } );
  113. } );