toolbarview.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, Event, console */
  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. view.render();
  21. } );
  22. afterEach( () => {
  23. sinon.restore();
  24. view.destroy();
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'should set view#locale', () => {
  28. expect( view.locale ).to.equal( locale );
  29. } );
  30. it( 'should set view#isVertical', () => {
  31. expect( view.isVertical ).to.be.false;
  32. } );
  33. it( 'should create view#children collection', () => {
  34. expect( view.items ).to.be.instanceOf( ViewCollection );
  35. } );
  36. it( 'creates #focusTracker instance', () => {
  37. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  38. } );
  39. it( 'creates #keystrokes instance', () => {
  40. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  41. } );
  42. it( 'creates #_focusCycler instance', () => {
  43. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  44. } );
  45. } );
  46. describe( 'template', () => {
  47. it( 'should create element from template', () => {
  48. expect( view.element.classList.contains( 'ck' ) ).to.true;
  49. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  50. } );
  51. describe( 'event listeners', () => {
  52. it( 'prevent default on #mousedown', () => {
  53. const evt = new Event( 'mousedown', { bubbles: true } );
  54. const spy = sinon.spy( evt, 'preventDefault' );
  55. view.element.dispatchEvent( evt );
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. } );
  59. } );
  60. describe( 'element bindings', () => {
  61. describe( 'class', () => {
  62. it( 'reacts on view#isVertical', () => {
  63. view.isVertical = false;
  64. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  65. view.isVertical = true;
  66. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  67. } );
  68. it( 'reacts on view#class', () => {
  69. view.class = 'foo';
  70. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  71. view.class = 'bar';
  72. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  73. view.class = false;
  74. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  75. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  76. } );
  77. } );
  78. } );
  79. describe( 'render()', () => {
  80. it( 'registers #items in #focusTracker', () => {
  81. const view = new ToolbarView( locale );
  82. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  83. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  84. view.items.add( focusable() );
  85. view.items.add( focusable() );
  86. sinon.assert.notCalled( spyAdd );
  87. view.render();
  88. sinon.assert.calledTwice( spyAdd );
  89. view.items.remove( 1 );
  90. sinon.assert.calledOnce( spyRemove );
  91. view.destroy();
  92. } );
  93. it( 'starts listening for #keystrokes coming from #element', () => {
  94. const view = new ToolbarView();
  95. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  96. view.render();
  97. sinon.assert.calledOnce( spy );
  98. sinon.assert.calledWithExactly( spy, view.element );
  99. view.destroy();
  100. } );
  101. describe( 'activates keyboard navigation for the toolbar', () => {
  102. it( 'so "arrowup" focuses previous focusable item', () => {
  103. const keyEvtData = {
  104. keyCode: keyCodes.arrowup,
  105. preventDefault: sinon.spy(),
  106. stopPropagation: sinon.spy()
  107. };
  108. // No children to focus.
  109. view.keystrokes.press( keyEvtData );
  110. sinon.assert.calledOnce( keyEvtData.preventDefault );
  111. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  112. view.items.add( nonFocusable() );
  113. view.items.add( nonFocusable() );
  114. // No focusable children.
  115. view.keystrokes.press( keyEvtData );
  116. sinon.assert.calledTwice( keyEvtData.preventDefault );
  117. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  118. view.items.add( focusable() );
  119. view.items.add( nonFocusable() );
  120. view.items.add( focusable() );
  121. // Mock the last item is focused.
  122. view.focusTracker.isFocused = true;
  123. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  124. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  125. view.keystrokes.press( keyEvtData );
  126. sinon.assert.calledThrice( keyEvtData.preventDefault );
  127. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  128. sinon.assert.calledOnce( spy );
  129. } );
  130. it( 'so "arrowleft" focuses previous focusable item', () => {
  131. const keyEvtData = {
  132. keyCode: keyCodes.arrowleft,
  133. preventDefault: sinon.spy(),
  134. stopPropagation: sinon.spy()
  135. };
  136. view.items.add( focusable() );
  137. view.items.add( nonFocusable() );
  138. view.items.add( focusable() );
  139. // Mock the last item is focused.
  140. view.focusTracker.isFocused = true;
  141. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  142. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  143. view.keystrokes.press( keyEvtData );
  144. sinon.assert.calledOnce( spy );
  145. } );
  146. it( 'so "arrowdown" focuses next focusable item', () => {
  147. const keyEvtData = {
  148. keyCode: keyCodes.arrowdown,
  149. preventDefault: sinon.spy(),
  150. stopPropagation: sinon.spy()
  151. };
  152. // No children to focus.
  153. view.keystrokes.press( keyEvtData );
  154. sinon.assert.calledOnce( keyEvtData.preventDefault );
  155. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  156. view.items.add( nonFocusable() );
  157. view.items.add( nonFocusable() );
  158. // No focusable children.
  159. view.keystrokes.press( keyEvtData );
  160. sinon.assert.calledTwice( keyEvtData.preventDefault );
  161. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  162. view.items.add( focusable() );
  163. view.items.add( nonFocusable() );
  164. view.items.add( focusable() );
  165. // Mock the last item is focused.
  166. view.focusTracker.isFocused = true;
  167. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  168. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  169. view.keystrokes.press( keyEvtData );
  170. sinon.assert.calledThrice( keyEvtData.preventDefault );
  171. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  172. sinon.assert.calledOnce( spy );
  173. } );
  174. it( 'so "arrowright" focuses next focusable item', () => {
  175. const keyEvtData = {
  176. keyCode: keyCodes.arrowright,
  177. preventDefault: sinon.spy(),
  178. stopPropagation: sinon.spy()
  179. };
  180. view.items.add( focusable() );
  181. view.items.add( nonFocusable() );
  182. view.items.add( focusable() );
  183. // Mock the last item is focused.
  184. view.focusTracker.isFocused = true;
  185. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  186. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  187. view.keystrokes.press( keyEvtData );
  188. sinon.assert.calledOnce( spy );
  189. } );
  190. } );
  191. } );
  192. describe( 'focus()', () => {
  193. it( 'focuses the first focusable item in DOM', () => {
  194. // No children to focus.
  195. view.focus();
  196. // The second child is focusable.
  197. view.items.add( nonFocusable() );
  198. view.items.add( focusable() );
  199. view.items.add( nonFocusable() );
  200. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  201. view.focus();
  202. sinon.assert.calledOnce( spy );
  203. } );
  204. } );
  205. describe( 'focusLast()', () => {
  206. it( 'focuses the last focusable item in DOM', () => {
  207. // No children to focus.
  208. view.focusLast();
  209. // The second child is focusable.
  210. view.items.add( nonFocusable() );
  211. view.items.add( focusable() );
  212. view.items.add( focusable() );
  213. view.items.add( focusable() );
  214. view.items.add( nonFocusable() );
  215. const spy = sinon.spy( view.items.get( 3 ), 'focus' );
  216. view.focusLast();
  217. sinon.assert.calledOnce( spy );
  218. } );
  219. } );
  220. describe( 'fillFromConfig()', () => {
  221. let factory;
  222. beforeEach( () => {
  223. factory = new ComponentFactory( {} );
  224. factory.add( 'foo', namedFactory( 'foo' ) );
  225. factory.add( 'bar', namedFactory( 'bar' ) );
  226. } );
  227. it( 'expands the config into collection', () => {
  228. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  229. const items = view.items;
  230. expect( items ).to.have.length( 4 );
  231. expect( items.get( 0 ).name ).to.equal( 'foo' );
  232. expect( items.get( 1 ).name ).to.equal( 'bar' );
  233. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  234. expect( items.get( 3 ).name ).to.equal( 'foo' );
  235. } );
  236. it( 'warns if there is no such component in the factory', () => {
  237. const items = view.items;
  238. const consoleWarnStub = sinon.stub( console, 'warn' );
  239. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  240. expect( items ).to.have.length( 2 );
  241. expect( items.get( 0 ).name ).to.equal( 'foo' );
  242. expect( items.get( 1 ).name ).to.equal( 'bar' );
  243. sinon.assert.calledOnce( consoleWarnStub );
  244. sinon.assert.calledWithExactly( consoleWarnStub,
  245. sinon.match( /^toolbarview-item-unavailable/ ),
  246. { name: 'baz' }
  247. );
  248. } );
  249. } );
  250. } );
  251. function focusable() {
  252. const view = nonFocusable();
  253. view.focus = () => {};
  254. return view;
  255. }
  256. function nonFocusable() {
  257. const view = new View();
  258. view.element = document.createElement( 'li' );
  259. return view;
  260. }
  261. function namedFactory( name ) {
  262. return locale => {
  263. const view = new View( locale );
  264. view.name = name;
  265. view.element = document.createElement( 'a' );
  266. return view;
  267. };
  268. }