8
0

focuscycler.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewCollection from 'ckeditor5-ui/src/viewcollection';
  6. import View from 'ckeditor5-ui/src/view';
  7. import FocusCycler from 'ckeditor5-utils/src/focuscycler';
  8. describe( 'FocusCycler', () => {
  9. let viewCollection, focusTracker, cycler;
  10. beforeEach( () => {
  11. viewCollection = new ViewCollection();
  12. focusTracker = {
  13. focusedElement: null
  14. };
  15. cycler = new FocusCycler( viewCollection, focusTracker );
  16. viewCollection.add( nonFocusable() );
  17. viewCollection.add( focusable() );
  18. viewCollection.add( focusable() );
  19. viewCollection.add( focusable() );
  20. viewCollection.add( nonFocusable() );
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'sets class properties', () => {
  24. expect( cycler.viewCollection ).to.equal( viewCollection );
  25. expect( cycler.focusTracker ).to.equal( focusTracker );
  26. } );
  27. } );
  28. describe( 'current()', () => {
  29. it( 'returns null when no view is focused', () => {
  30. expect( cycler.current ).to.equal( null );
  31. focusTracker.focusedElement = viewCollection.get( 2 ).element;
  32. expect( cycler.current ).to.equal( 2 );
  33. focusTracker.focusedElement = null;
  34. expect( cycler.current ).to.equal( null );
  35. } );
  36. } );
  37. describe( 'first()', () => {
  38. it( 'returns first focusable view', () => {
  39. expect( cycler.first ).to.equal( viewCollection.get( 1 ) );
  40. } );
  41. it( 'returns null when no focusable items', () => {
  42. viewCollection = new ViewCollection();
  43. cycler = new FocusCycler( viewCollection, focusTracker );
  44. viewCollection.add( nonFocusable() );
  45. viewCollection.add( nonFocusable() );
  46. expect( cycler.first ).to.be.null;
  47. } );
  48. it( 'returns null when no items', () => {
  49. viewCollection = new ViewCollection();
  50. cycler = new FocusCycler( viewCollection, focusTracker );
  51. expect( cycler.first ).to.be.null;
  52. } );
  53. } );
  54. describe( 'last()', () => {
  55. it( 'returns last focusable view', () => {
  56. expect( cycler.last ).to.equal( viewCollection.get( 3 ) );
  57. } );
  58. it( 'returns null when no focusable items', () => {
  59. viewCollection = new ViewCollection();
  60. cycler = new FocusCycler( viewCollection, focusTracker );
  61. viewCollection.add( nonFocusable() );
  62. viewCollection.add( nonFocusable() );
  63. expect( cycler.last ).to.be.null;
  64. } );
  65. it( 'returns null when no items', () => {
  66. viewCollection = new ViewCollection();
  67. cycler = new FocusCycler( viewCollection, focusTracker );
  68. expect( cycler.last ).to.be.null;
  69. } );
  70. } );
  71. describe( 'next()', () => {
  72. it( 'cycles to return the next focusable view', () => {
  73. focusTracker.focusedElement = viewCollection.get( 2 ).element;
  74. expect( cycler.next ).to.equal( viewCollection.get( 3 ) );
  75. focusTracker.focusedElement = viewCollection.get( 3 ).element;
  76. expect( cycler.next ).to.equal( viewCollection.get( 1 ) );
  77. focusTracker.focusedElement = viewCollection.get( 1 ).element;
  78. expect( cycler.next ).to.equal( viewCollection.get( 2 ) );
  79. } );
  80. it( 'returns null when no view is focused', () => {
  81. focusTracker.focusedElement = null;
  82. expect( cycler.next ).to.be.null;
  83. } );
  84. it( 'returns null when no items', () => {
  85. viewCollection = new ViewCollection();
  86. cycler = new FocusCycler( viewCollection, focusTracker );
  87. expect( cycler.next ).to.be.null;
  88. } );
  89. it( 'returns null when no focusable items', () => {
  90. viewCollection = new ViewCollection();
  91. cycler = new FocusCycler( viewCollection, focusTracker );
  92. viewCollection.add( nonFocusable() );
  93. viewCollection.add( nonFocusable() );
  94. expect( cycler.next ).to.be.null;
  95. } );
  96. it( 'returns null if the only focusable in viewCollection', () => {
  97. viewCollection = new ViewCollection();
  98. cycler = new FocusCycler( viewCollection, focusTracker );
  99. viewCollection.add( nonFocusable() );
  100. viewCollection.add( focusable() );
  101. viewCollection.add( nonFocusable() );
  102. focusTracker.focusedElement = viewCollection.get( 1 ).element;
  103. expect( cycler.first ).to.equal( viewCollection.get( 1 ) );
  104. expect( cycler.next ).to.be.null;
  105. } );
  106. } );
  107. describe( 'previous()', () => {
  108. it( 'cycles to return the previous focusable view', () => {
  109. focusTracker.focusedElement = viewCollection.get( 1 ).element;
  110. expect( cycler.previous ).to.equal( viewCollection.get( 3 ) );
  111. focusTracker.focusedElement = viewCollection.get( 2 ).element;
  112. expect( cycler.previous ).to.equal( viewCollection.get( 1 ) );
  113. focusTracker.focusedElement = viewCollection.get( 3 ).element;
  114. expect( cycler.previous ).to.equal( viewCollection.get( 2 ) );
  115. } );
  116. it( 'returns null when no view is focused', () => {
  117. focusTracker.focusedElement = null;
  118. expect( cycler.previous ).to.be.null;
  119. } );
  120. it( 'returns null when no items', () => {
  121. viewCollection = new ViewCollection();
  122. cycler = new FocusCycler( viewCollection, focusTracker );
  123. expect( cycler.previous ).to.be.null;
  124. } );
  125. it( 'returns null when no focusable items', () => {
  126. viewCollection = new ViewCollection();
  127. cycler = new FocusCycler( viewCollection, focusTracker );
  128. viewCollection.add( nonFocusable() );
  129. viewCollection.add( nonFocusable() );
  130. expect( cycler.previous ).to.be.null;
  131. } );
  132. it( 'returns null if the only focusable in viewCollection', () => {
  133. viewCollection = new ViewCollection();
  134. cycler = new FocusCycler( viewCollection, focusTracker );
  135. viewCollection.add( nonFocusable() );
  136. viewCollection.add( focusable() );
  137. viewCollection.add( nonFocusable() );
  138. focusTracker.focusedElement = viewCollection.get( 1 ).element;
  139. expect( cycler.first ).to.equal( viewCollection.get( 1 ) );
  140. expect( cycler.previous ).to.be.null;
  141. } );
  142. } );
  143. } );
  144. function focusable() {
  145. const view = new View();
  146. view.focus = () => {};
  147. view.element = {};
  148. return view;
  149. }
  150. function nonFocusable() {
  151. return new View();
  152. }