toolbarview.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event */
  6. import ToolbarView from '../../src/toolbar/toolbarview';
  7. import ToolbarSeparatorView from '../../src/toolbar/toolbarseparatorview';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import ComponentFactory from '../../src/componentfactory';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '../../src/focuscycler';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import ViewCollection from '../../src/viewcollection';
  14. import View from '../../src/view';
  15. describe( 'ToolbarView', () => {
  16. let locale, view;
  17. beforeEach( () => {
  18. locale = {};
  19. view = new ToolbarView( locale );
  20. return view.init();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should set view#locale', () => {
  24. expect( view.locale ).to.equal( locale );
  25. } );
  26. it( 'should create view#children collection', () => {
  27. expect( view.items ).to.be.instanceOf( ViewCollection );
  28. } );
  29. it( 'creates #focusTracker instance', () => {
  30. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  31. } );
  32. it( 'creates #keystrokes instance', () => {
  33. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  34. } );
  35. it( 'creates #_focusCycler instance', () => {
  36. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  37. } );
  38. it( 'registers #items in #focusTracker', () => {
  39. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  40. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  41. view.items.add( focusable() );
  42. view.items.add( focusable() );
  43. sinon.assert.calledTwice( spyAdd );
  44. view.items.remove( 1 );
  45. sinon.assert.calledOnce( spyRemove );
  46. } );
  47. } );
  48. describe( 'template', () => {
  49. it( 'should create element from template', () => {
  50. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  51. } );
  52. describe( 'event listeners', () => {
  53. it( 'prevent default on #mousedown', () => {
  54. const evt = new Event( 'mousedown', { bubbles: true } );
  55. const spy = sinon.spy( evt, 'preventDefault' );
  56. view.element.dispatchEvent( evt );
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. } );
  60. } );
  61. describe( 'init()', () => {
  62. it( 'starts listening for #keystrokes coming from #element', () => {
  63. view = new ToolbarView();
  64. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  65. return view.init().then( () => {
  66. sinon.assert.calledOnce( spy );
  67. sinon.assert.calledWithExactly( spy, view.element );
  68. } );
  69. } );
  70. describe( 'activates keyboard navigation for the toolbar', () => {
  71. it( 'so "arrowup" focuses previous focusable item', () => {
  72. const keyEvtData = {
  73. keyCode: keyCodes.arrowup,
  74. preventDefault: sinon.spy(),
  75. stopPropagation: sinon.spy()
  76. };
  77. // No children to focus.
  78. view.keystrokes.press( keyEvtData );
  79. sinon.assert.calledOnce( keyEvtData.preventDefault );
  80. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  81. view.items.add( nonFocusable() );
  82. view.items.add( nonFocusable() );
  83. // No focusable children.
  84. view.keystrokes.press( keyEvtData );
  85. sinon.assert.calledTwice( keyEvtData.preventDefault );
  86. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  87. view.items.add( focusable() );
  88. view.items.add( nonFocusable() );
  89. view.items.add( focusable() );
  90. // Mock the last item is focused.
  91. view.focusTracker.isFocused = true;
  92. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  93. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  94. view.keystrokes.press( keyEvtData );
  95. sinon.assert.calledThrice( keyEvtData.preventDefault );
  96. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  97. sinon.assert.calledOnce( spy );
  98. } );
  99. it( 'so "arrowleft" focuses previous focusable item', () => {
  100. const keyEvtData = {
  101. keyCode: keyCodes.arrowleft,
  102. preventDefault: sinon.spy(),
  103. stopPropagation: sinon.spy()
  104. };
  105. view.items.add( focusable() );
  106. view.items.add( nonFocusable() );
  107. view.items.add( focusable() );
  108. // Mock the last item is focused.
  109. view.focusTracker.isFocused = true;
  110. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  111. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  112. view.keystrokes.press( keyEvtData );
  113. sinon.assert.calledOnce( spy );
  114. } );
  115. it( 'so "arrowdown" focuses next focusable item', () => {
  116. const keyEvtData = {
  117. keyCode: keyCodes.arrowdown,
  118. preventDefault: sinon.spy(),
  119. stopPropagation: sinon.spy()
  120. };
  121. // No children to focus.
  122. view.keystrokes.press( keyEvtData );
  123. sinon.assert.calledOnce( keyEvtData.preventDefault );
  124. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  125. view.items.add( nonFocusable() );
  126. view.items.add( nonFocusable() );
  127. // No focusable children.
  128. view.keystrokes.press( keyEvtData );
  129. sinon.assert.calledTwice( keyEvtData.preventDefault );
  130. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  131. view.items.add( focusable() );
  132. view.items.add( nonFocusable() );
  133. view.items.add( focusable() );
  134. // Mock the last item is focused.
  135. view.focusTracker.isFocused = true;
  136. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  137. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  138. view.keystrokes.press( keyEvtData );
  139. sinon.assert.calledThrice( keyEvtData.preventDefault );
  140. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  141. sinon.assert.calledOnce( spy );
  142. } );
  143. it( 'so "arrowright" focuses next focusable item', () => {
  144. const keyEvtData = {
  145. keyCode: keyCodes.arrowright,
  146. preventDefault: sinon.spy(),
  147. stopPropagation: sinon.spy()
  148. };
  149. view.items.add( focusable() );
  150. view.items.add( nonFocusable() );
  151. view.items.add( focusable() );
  152. // Mock the last item is focused.
  153. view.focusTracker.isFocused = true;
  154. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  155. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  156. view.keystrokes.press( keyEvtData );
  157. sinon.assert.calledOnce( spy );
  158. } );
  159. } );
  160. } );
  161. describe( 'focus()', () => {
  162. it( 'focuses the first focusable item in DOM', () => {
  163. // No children to focus.
  164. view.focus();
  165. // The second child is focusable.
  166. view.items.add( nonFocusable() );
  167. view.items.add( focusable() );
  168. view.items.add( nonFocusable() );
  169. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  170. view.focus();
  171. sinon.assert.calledOnce( spy );
  172. } );
  173. } );
  174. describe( 'fillFromConfig()', () => {
  175. let factory;
  176. beforeEach( () => {
  177. factory = new ComponentFactory( {} );
  178. factory.add( 'foo', namedFactory( 'foo' ) );
  179. factory.add( 'bar', namedFactory( 'bar' ) );
  180. } );
  181. it( 'returns a promise', () => {
  182. expect( view.fillFromConfig() ).to.be.instanceOf( Promise );
  183. } );
  184. it( 'expands the config into collection', () => {
  185. return view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory )
  186. .then( () => {
  187. const items = view.items;
  188. expect( items ).to.have.length( 4 );
  189. expect( items.get( 0 ).name ).to.equal( 'foo' );
  190. expect( items.get( 1 ).name ).to.equal( 'bar' );
  191. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  192. expect( items.get( 3 ).name ).to.equal( 'foo' );
  193. } );
  194. } );
  195. } );
  196. } );
  197. function focusable() {
  198. const view = nonFocusable();
  199. view.focus = () => {};
  200. return view;
  201. }
  202. function nonFocusable() {
  203. const view = new View();
  204. view.element = document.createElement( 'li' );
  205. return view;
  206. }
  207. function namedFactory( name ) {
  208. return ( locale ) => {
  209. const view = new View( locale );
  210. view.name = name;
  211. view.element = document.createElement( 'a' );
  212. return view;
  213. };
  214. }