listview.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import ViewCollection from '../../src/viewcollection';
  7. import ListView from '../../src/list/listview';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  10. import FocusCycler from '../../src/focuscycler';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import View from '../../src/view';
  13. describe( 'ListView', () => {
  14. let view;
  15. beforeEach( () => {
  16. view = new ListView();
  17. view.render();
  18. } );
  19. afterEach( () => {
  20. view.destroy();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'creates element from template', () => {
  24. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  25. expect( view.element.classList.contains( 'ck-reset' ) ).to.be.true;
  26. expect( view.element.classList.contains( 'ck-list' ) ).to.be.true;
  27. } );
  28. it( 'creates view#items collection', () => {
  29. expect( view.items ).to.be.instanceOf( ViewCollection );
  30. expect( view.template.children ).to.have.length( 1 );
  31. expect( view.template.children[ 0 ] ).to.equal( view.items );
  32. } );
  33. it( 'creates #focusTracker instance', () => {
  34. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  35. } );
  36. it( 'creates #keystrokeHandler instance', () => {
  37. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  38. } );
  39. it( 'creates #_focusCycler instance', () => {
  40. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  41. } );
  42. } );
  43. describe( 'render()', () => {
  44. it( 'registers #items in #focusTracker', () => {
  45. const view = new ListView();
  46. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  47. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  48. sinon.assert.notCalled( spyAdd );
  49. view.items.add( focusable() );
  50. view.items.add( focusable() );
  51. view.render();
  52. sinon.assert.calledTwice( spyAdd );
  53. view.items.remove( 1 );
  54. sinon.assert.calledOnce( spyRemove );
  55. view.destroy();
  56. } );
  57. it( 'starts listening for #keystrokes coming from #element', () => {
  58. const view = new ListView();
  59. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  60. view.render();
  61. sinon.assert.calledOnce( spy );
  62. sinon.assert.calledWithExactly( spy, view.element );
  63. view.destroy();
  64. } );
  65. describe( 'activates keyboard navigation for the list', () => {
  66. it( 'so "arrowup" focuses previous focusable item', () => {
  67. const keyEvtData = {
  68. keyCode: keyCodes.arrowup,
  69. preventDefault: sinon.spy(),
  70. stopPropagation: sinon.spy()
  71. };
  72. // No children to focus.
  73. view.keystrokes.press( keyEvtData );
  74. sinon.assert.calledOnce( keyEvtData.preventDefault );
  75. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  76. view.items.add( nonFocusable() );
  77. view.items.add( nonFocusable() );
  78. // No focusable children.
  79. view.keystrokes.press( keyEvtData );
  80. sinon.assert.calledTwice( keyEvtData.preventDefault );
  81. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  82. view.items.add( focusable() );
  83. view.items.add( nonFocusable() );
  84. view.items.add( focusable() );
  85. // Mock the last item is focused.
  86. view.focusTracker.isFocused = true;
  87. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  88. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  89. view.keystrokes.press( keyEvtData );
  90. sinon.assert.calledThrice( keyEvtData.preventDefault );
  91. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  92. sinon.assert.calledOnce( spy );
  93. } );
  94. it( 'so "arrowdown" focuses next focusable item', () => {
  95. const keyEvtData = {
  96. keyCode: keyCodes.arrowdown,
  97. preventDefault: sinon.spy(),
  98. stopPropagation: sinon.spy()
  99. };
  100. // No children to focus.
  101. view.keystrokes.press( keyEvtData );
  102. sinon.assert.calledOnce( keyEvtData.preventDefault );
  103. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  104. view.items.add( nonFocusable() );
  105. view.items.add( nonFocusable() );
  106. // No focusable children.
  107. view.keystrokes.press( keyEvtData );
  108. sinon.assert.calledTwice( keyEvtData.preventDefault );
  109. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  110. view.items.add( focusable() );
  111. view.items.add( nonFocusable() );
  112. view.items.add( focusable() );
  113. // Mock the last item is focused.
  114. view.focusTracker.isFocused = true;
  115. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  116. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  117. view.keystrokes.press( keyEvtData );
  118. sinon.assert.calledThrice( keyEvtData.preventDefault );
  119. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  120. sinon.assert.calledOnce( spy );
  121. } );
  122. } );
  123. } );
  124. describe( 'focus()', () => {
  125. it( 'focuses the first focusable item in DOM', () => {
  126. // No children to focus.
  127. view.focus();
  128. // The second child is focusable.
  129. view.items.add( nonFocusable() );
  130. view.items.add( focusable() );
  131. view.items.add( nonFocusable() );
  132. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  133. view.focus();
  134. sinon.assert.calledOnce( spy );
  135. } );
  136. } );
  137. describe( 'focusLast()', () => {
  138. it( 'focuses the last focusable item in DOM', () => {
  139. // No children to focus.
  140. view.focusLast();
  141. // The second child is focusable.
  142. view.items.add( nonFocusable() );
  143. view.items.add( focusable() );
  144. view.items.add( nonFocusable() );
  145. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  146. view.focusLast();
  147. sinon.assert.calledOnce( spy );
  148. } );
  149. } );
  150. } );
  151. function focusable() {
  152. const view = nonFocusable();
  153. view.focus = () => {};
  154. return view;
  155. }
  156. function nonFocusable() {
  157. const view = new View();
  158. view.element = document.createElement( 'li' );
  159. return view;
  160. }