listview.js 5.5 KB

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