toolbarview.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 log from '@ckeditor/ckeditor5-utils/src/log';
  15. import View from '../../src/view';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'ToolbarView', () => {
  18. let locale, view;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. locale = {};
  22. view = new ToolbarView( locale );
  23. view.render();
  24. } );
  25. afterEach( () => {
  26. view.destroy();
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'should set view#locale', () => {
  30. expect( view.locale ).to.equal( locale );
  31. } );
  32. it( 'should set view#isVertical', () => {
  33. expect( view.isVertical ).to.be.false;
  34. } );
  35. it( 'should create view#children collection', () => {
  36. expect( view.items ).to.be.instanceOf( ViewCollection );
  37. } );
  38. it( 'creates #focusTracker instance', () => {
  39. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  40. } );
  41. it( 'creates #keystrokes instance', () => {
  42. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  43. } );
  44. it( 'creates #_focusCycler instance', () => {
  45. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  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( 'element bindings', () => {
  62. describe( 'class', () => {
  63. it( 'reacts on view#isVertical', () => {
  64. view.isVertical = false;
  65. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  66. view.isVertical = true;
  67. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  68. } );
  69. } );
  70. } );
  71. describe( 'render()', () => {
  72. it( 'registers #items in #focusTracker', () => {
  73. const view = new ToolbarView( locale );
  74. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  75. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  76. view.items.add( focusable() );
  77. view.items.add( focusable() );
  78. sinon.assert.notCalled( spyAdd );
  79. view.render();
  80. sinon.assert.calledTwice( spyAdd );
  81. view.items.remove( 1 );
  82. sinon.assert.calledOnce( spyRemove );
  83. view.destroy();
  84. } );
  85. it( 'starts listening for #keystrokes coming from #element', () => {
  86. const view = new ToolbarView();
  87. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  88. view.render();
  89. sinon.assert.calledOnce( spy );
  90. sinon.assert.calledWithExactly( spy, view.element );
  91. view.destroy();
  92. } );
  93. describe( 'activates keyboard navigation for the toolbar', () => {
  94. it( 'so "arrowup" focuses previous focusable item', () => {
  95. const keyEvtData = {
  96. keyCode: keyCodes.arrowup,
  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. it( 'so "arrowleft" focuses previous focusable item', () => {
  123. const keyEvtData = {
  124. keyCode: keyCodes.arrowleft,
  125. preventDefault: sinon.spy(),
  126. stopPropagation: sinon.spy()
  127. };
  128. view.items.add( focusable() );
  129. view.items.add( nonFocusable() );
  130. view.items.add( focusable() );
  131. // Mock the last item is focused.
  132. view.focusTracker.isFocused = true;
  133. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  134. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  135. view.keystrokes.press( keyEvtData );
  136. sinon.assert.calledOnce( spy );
  137. } );
  138. it( 'so "arrowdown" focuses next focusable item', () => {
  139. const keyEvtData = {
  140. keyCode: keyCodes.arrowdown,
  141. preventDefault: sinon.spy(),
  142. stopPropagation: sinon.spy()
  143. };
  144. // No children to focus.
  145. view.keystrokes.press( keyEvtData );
  146. sinon.assert.calledOnce( keyEvtData.preventDefault );
  147. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  148. view.items.add( nonFocusable() );
  149. view.items.add( nonFocusable() );
  150. // No focusable children.
  151. view.keystrokes.press( keyEvtData );
  152. sinon.assert.calledTwice( keyEvtData.preventDefault );
  153. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  154. view.items.add( focusable() );
  155. view.items.add( nonFocusable() );
  156. view.items.add( focusable() );
  157. // Mock the last item is focused.
  158. view.focusTracker.isFocused = true;
  159. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  160. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  161. view.keystrokes.press( keyEvtData );
  162. sinon.assert.calledThrice( keyEvtData.preventDefault );
  163. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  164. sinon.assert.calledOnce( spy );
  165. } );
  166. it( 'so "arrowright" focuses next focusable item', () => {
  167. const keyEvtData = {
  168. keyCode: keyCodes.arrowright,
  169. preventDefault: sinon.spy(),
  170. stopPropagation: sinon.spy()
  171. };
  172. view.items.add( focusable() );
  173. view.items.add( nonFocusable() );
  174. view.items.add( focusable() );
  175. // Mock the last item is focused.
  176. view.focusTracker.isFocused = true;
  177. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  178. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  179. view.keystrokes.press( keyEvtData );
  180. sinon.assert.calledOnce( spy );
  181. } );
  182. } );
  183. } );
  184. describe( 'focus()', () => {
  185. it( 'focuses the first focusable item in DOM', () => {
  186. // No children to focus.
  187. view.focus();
  188. // The second child is focusable.
  189. view.items.add( nonFocusable() );
  190. view.items.add( focusable() );
  191. view.items.add( nonFocusable() );
  192. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  193. view.focus();
  194. sinon.assert.calledOnce( spy );
  195. } );
  196. } );
  197. describe( 'focusLast()', () => {
  198. it( 'focuses the last focusable item in DOM', () => {
  199. // No children to focus.
  200. view.focusLast();
  201. // The second child is focusable.
  202. view.items.add( nonFocusable() );
  203. view.items.add( focusable() );
  204. view.items.add( focusable() );
  205. view.items.add( focusable() );
  206. view.items.add( nonFocusable() );
  207. const spy = sinon.spy( view.items.get( 3 ), 'focus' );
  208. view.focusLast();
  209. sinon.assert.calledOnce( spy );
  210. } );
  211. } );
  212. describe( 'fillFromConfig()', () => {
  213. let factory;
  214. beforeEach( () => {
  215. factory = new ComponentFactory( {} );
  216. factory.add( 'foo', namedFactory( 'foo' ) );
  217. factory.add( 'bar', namedFactory( 'bar' ) );
  218. } );
  219. it( 'expands the config into collection', () => {
  220. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  221. const items = view.items;
  222. expect( items ).to.have.length( 4 );
  223. expect( items.get( 0 ).name ).to.equal( 'foo' );
  224. expect( items.get( 1 ).name ).to.equal( 'bar' );
  225. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  226. expect( items.get( 3 ).name ).to.equal( 'foo' );
  227. } );
  228. it( 'warns if there is no such component in the factory', () => {
  229. const items = view.items;
  230. testUtils.sinon.stub( log, 'warn' );
  231. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  232. expect( items ).to.have.length( 2 );
  233. expect( items.get( 0 ).name ).to.equal( 'foo' );
  234. expect( items.get( 1 ).name ).to.equal( 'bar' );
  235. sinon.assert.calledOnce( log.warn );
  236. sinon.assert.calledWithExactly( log.warn,
  237. sinon.match( /^toolbarview-item-unavailable/ ),
  238. { name: 'baz' }
  239. );
  240. } );
  241. } );
  242. } );
  243. function focusable() {
  244. const view = nonFocusable();
  245. view.focus = () => {};
  246. return view;
  247. }
  248. function nonFocusable() {
  249. const view = new View();
  250. view.element = document.createElement( 'li' );
  251. return view;
  252. }
  253. function namedFactory( name ) {
  254. return locale => {
  255. const view = new View( locale );
  256. view.name = name;
  257. view.element = document.createElement( 'a' );
  258. return view;
  259. };
  260. }