listview.js 5.7 KB

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