focusmanager.js 4.4 KB

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