8
0

dropdownview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import DropdownView from '../../src/dropdown/dropdownview';
  6. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  7. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import ButtonView from '../../src/button/buttonview';
  10. import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'DropdownView', () => {
  14. let view, buttonView, panelView, locale;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = {
  18. uiLanguageDirection: 'ltr',
  19. t() {}
  20. };
  21. buttonView = new ButtonView( locale );
  22. panelView = new DropdownPanelView( locale );
  23. view = new DropdownView( locale, buttonView, panelView );
  24. view.render();
  25. // The #panelView positioning depends on the utility that uses DOM Rects.
  26. // To avoid an avalanche of warnings (DOM Rects do not work until
  27. // the element is in DOM), let's allow the dropdown to render in DOM.
  28. global.document.body.appendChild( view.element );
  29. } );
  30. afterEach( () => {
  31. view.element.remove();
  32. } );
  33. describe( 'constructor()', () => {
  34. it( 'sets view#locale', () => {
  35. expect( view.locale ).to.equal( locale );
  36. } );
  37. it( 'sets view#buttonView', () => {
  38. expect( view.buttonView ).to.equal( buttonView );
  39. } );
  40. it( 'sets view#panelView', () => {
  41. expect( view.panelView ).to.equal( panelView );
  42. } );
  43. it( 'sets view#isOpen false', () => {
  44. expect( view.isOpen ).to.be.false;
  45. } );
  46. it( 'sets view#isEnabled true', () => {
  47. expect( view.isEnabled ).to.be.true;
  48. } );
  49. it( 'sets view#panelPosition "auto"', () => {
  50. expect( view.panelPosition ).to.equal( 'auto' );
  51. } );
  52. it( 'creates #focusTracker instance', () => {
  53. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  54. } );
  55. it( 'creates #keystrokeHandler instance', () => {
  56. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  57. } );
  58. it( 'creates #element from template', () => {
  59. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  60. expect( view.element.classList.contains( 'ck-dropdown' ) ).to.be.true;
  61. expect( view.element.children ).to.have.length( 2 );
  62. expect( view.element.children[ 0 ] ).to.equal( buttonView.element );
  63. expect( view.element.children[ 1 ] ).to.equal( panelView.element );
  64. } );
  65. it( 'sets view#buttonView class', () => {
  66. expect( view.buttonView.element.classList.contains( 'ck-dropdown__button' ) ).to.be.true;
  67. } );
  68. describe( 'bindings', () => {
  69. describe( 'view#isOpen to view.buttonView#select', () => {
  70. it( 'is activated', () => {
  71. const values = [];
  72. view.on( 'change:isOpen', () => {
  73. values.push( view.isOpen );
  74. } );
  75. view.buttonView.fire( 'open' );
  76. view.buttonView.fire( 'open' );
  77. view.buttonView.fire( 'open' );
  78. expect( values ).to.have.members( [ true, false, true ] );
  79. } );
  80. } );
  81. describe( 'view.panelView#isVisible to view#isOpen', () => {
  82. it( 'is activated', () => {
  83. const values = [];
  84. view.listenTo( view.panelView, 'change:isVisible', () => {
  85. values.push( view.isOpen );
  86. } );
  87. view.isOpen = true;
  88. view.isOpen = false;
  89. view.isOpen = true;
  90. expect( values ).to.have.members( [ true, false, true ] );
  91. } );
  92. } );
  93. describe( 'view.panelView#position to view#panelPosition', () => {
  94. it( 'does not update until the dropdown is open', () => {
  95. view.isOpen = false;
  96. view.panelPosition = 'nw';
  97. expect( panelView.position ).to.equal( 'se' );
  98. view.isOpen = true;
  99. expect( panelView.position ).to.equal( 'nw' );
  100. } );
  101. describe( 'in "auto" mode', () => {
  102. it( 'uses _getOptimalPosition() and a dedicated set of positions (LTR)', () => {
  103. const spy = testUtils.sinon.spy( DropdownView, '_getOptimalPosition' );
  104. const { southEast, southWest, northEast, northWest } = DropdownView.defaultPanelPositions;
  105. view.isOpen = true;
  106. sinon.assert.calledWithExactly( spy, sinon.match( {
  107. element: panelView.element,
  108. target: buttonView.element,
  109. positions: [
  110. southEast, southWest, northEast, northWest
  111. ],
  112. fitInViewport: true
  113. } ) );
  114. } );
  115. it( 'uses _getOptimalPosition() and a dedicated set of positions (RTL)', () => {
  116. const spy = testUtils.sinon.spy( DropdownView, '_getOptimalPosition' );
  117. const { southEast, southWest, northEast, northWest } = DropdownView.defaultPanelPositions;
  118. view.locale.uiLanguageDirection = 'rtl';
  119. view.isOpen = true;
  120. sinon.assert.calledWithExactly( spy, sinon.match( {
  121. element: panelView.element,
  122. target: buttonView.element,
  123. positions: [
  124. southWest, southEast, northWest, northEast
  125. ],
  126. fitInViewport: true
  127. } ) );
  128. } );
  129. } );
  130. } );
  131. describe( 'DOM element bindings', () => {
  132. describe( 'class', () => {
  133. it( 'reacts on view#isEnabled', () => {
  134. view.isEnabled = true;
  135. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.false;
  136. view.isEnabled = false;
  137. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true;
  138. } );
  139. it( 'reacts on view#class', () => {
  140. view.class = 'custom-css-class';
  141. expect( view.element.classList.contains( 'custom-css-class' ) ).to.be.true;
  142. } );
  143. } );
  144. } );
  145. } );
  146. } );
  147. describe( 'render()', () => {
  148. it( 'starts listening for #keystrokes coming from #element', () => {
  149. const view = new DropdownView( locale,
  150. new ButtonView( locale ),
  151. new DropdownPanelView( locale ) );
  152. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  153. view.render();
  154. sinon.assert.calledOnce( spy );
  155. sinon.assert.calledWithExactly( spy, view.element );
  156. view.element.remove();
  157. } );
  158. it( 'adds #element to #focusTracker', () => {
  159. const view = new DropdownView( locale,
  160. new ButtonView( locale ),
  161. new DropdownPanelView( locale ) );
  162. const spy = sinon.spy( view.focusTracker, 'add' );
  163. view.render();
  164. sinon.assert.calledOnce( spy );
  165. sinon.assert.calledWithExactly( spy, view.element );
  166. view.element.remove();
  167. } );
  168. describe( 'activates keyboard navigation for the dropdown', () => {
  169. it( 'so "arrowdown" opens the #panelView', () => {
  170. const keyEvtData = {
  171. keyCode: keyCodes.arrowdown,
  172. preventDefault: sinon.spy(),
  173. stopPropagation: sinon.spy()
  174. };
  175. view.buttonView.isEnabled = true;
  176. view.isOpen = true;
  177. view.keystrokes.press( keyEvtData );
  178. sinon.assert.notCalled( keyEvtData.preventDefault );
  179. sinon.assert.notCalled( keyEvtData.stopPropagation );
  180. expect( view.isOpen ).to.be.true;
  181. view.isOpen = false;
  182. view.keystrokes.press( keyEvtData );
  183. sinon.assert.calledOnce( keyEvtData.preventDefault );
  184. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  185. expect( view.isOpen ).to.be.true;
  186. } );
  187. it( 'so "arrowdown" won\'t open the #panelView when #isEnabled is false', () => {
  188. const keyEvtData = {
  189. keyCode: keyCodes.arrowdown,
  190. preventDefault: sinon.spy(),
  191. stopPropagation: sinon.spy()
  192. };
  193. view.buttonView.isEnabled = false;
  194. view.isOpen = false;
  195. view.keystrokes.press( keyEvtData );
  196. sinon.assert.notCalled( keyEvtData.preventDefault );
  197. sinon.assert.notCalled( keyEvtData.stopPropagation );
  198. expect( view.isOpen ).to.be.false;
  199. } );
  200. it( 'so "arrowright" is blocked', () => {
  201. const keyEvtData = {
  202. keyCode: keyCodes.arrowright,
  203. preventDefault: sinon.spy(),
  204. stopPropagation: sinon.spy()
  205. };
  206. view.false = true;
  207. view.keystrokes.press( keyEvtData );
  208. sinon.assert.notCalled( keyEvtData.preventDefault );
  209. sinon.assert.notCalled( keyEvtData.stopPropagation );
  210. expect( view.isOpen ).to.be.false;
  211. view.isOpen = true;
  212. view.keystrokes.press( keyEvtData );
  213. sinon.assert.calledOnce( keyEvtData.preventDefault );
  214. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  215. expect( view.isOpen ).to.be.true;
  216. } );
  217. it( 'so "arrowleft" closes the #panelView', () => {
  218. const keyEvtData = {
  219. keyCode: keyCodes.arrowleft,
  220. preventDefault: sinon.spy(),
  221. stopPropagation: sinon.spy()
  222. };
  223. const spy = sinon.spy( view.buttonView, 'focus' );
  224. view.isOpen = false;
  225. view.keystrokes.press( keyEvtData );
  226. sinon.assert.notCalled( keyEvtData.preventDefault );
  227. sinon.assert.notCalled( keyEvtData.stopPropagation );
  228. sinon.assert.notCalled( spy );
  229. expect( view.isOpen ).to.be.false;
  230. view.isOpen = true;
  231. view.keystrokes.press( keyEvtData );
  232. sinon.assert.calledOnce( keyEvtData.preventDefault );
  233. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  234. sinon.assert.calledOnce( spy );
  235. expect( view.isOpen ).to.be.false;
  236. } );
  237. it( 'so "esc" closes the #panelView', () => {
  238. const keyEvtData = {
  239. keyCode: keyCodes.esc,
  240. preventDefault: sinon.spy(),
  241. stopPropagation: sinon.spy()
  242. };
  243. const spy = sinon.spy( view.buttonView, 'focus' );
  244. view.isOpen = false;
  245. view.keystrokes.press( keyEvtData );
  246. sinon.assert.notCalled( keyEvtData.preventDefault );
  247. sinon.assert.notCalled( keyEvtData.stopPropagation );
  248. sinon.assert.notCalled( spy );
  249. expect( view.isOpen ).to.be.false;
  250. view.isOpen = true;
  251. view.keystrokes.press( keyEvtData );
  252. sinon.assert.calledOnce( keyEvtData.preventDefault );
  253. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  254. sinon.assert.calledOnce( spy );
  255. expect( view.isOpen ).to.be.false;
  256. } );
  257. } );
  258. } );
  259. describe( 'focus()', () => {
  260. it( 'focuses the #buttonView in DOM', () => {
  261. const spy = sinon.spy( view.buttonView, 'focus' );
  262. view.focus();
  263. sinon.assert.calledOnce( spy );
  264. } );
  265. } );
  266. describe( 'DropdownView.defaultPanelPositions', () => {
  267. let positions, buttonRect, panelRect;
  268. beforeEach( () => {
  269. positions = DropdownView.defaultPanelPositions;
  270. buttonRect = {
  271. top: 100,
  272. bottom: 200,
  273. left: 100,
  274. right: 200,
  275. width: 100,
  276. height: 100
  277. };
  278. panelRect = {
  279. top: 0,
  280. bottom: 0,
  281. left: 0,
  282. right: 0,
  283. width: 50,
  284. height: 50
  285. };
  286. } );
  287. it( 'should have a proper length', () => {
  288. expect( Object.keys( positions ) ).to.have.length( 4 );
  289. } );
  290. it( 'should define the "southEast" position', () => {
  291. expect( positions.southEast( buttonRect, panelRect ) ).to.deep.equal( {
  292. top: 200,
  293. left: 100,
  294. name: 'se'
  295. } );
  296. } );
  297. it( 'should define the "southWest" position', () => {
  298. expect( positions.southWest( buttonRect, panelRect ) ).to.deep.equal( {
  299. top: 200,
  300. left: 150,
  301. name: 'sw'
  302. } );
  303. } );
  304. it( 'should define the "northEast" position', () => {
  305. expect( positions.northEast( buttonRect, panelRect ) ).to.deep.equal( {
  306. top: 50,
  307. left: 100,
  308. name: 'ne'
  309. } );
  310. } );
  311. it( 'should define the "northWest" position', () => {
  312. expect( positions.northWest( buttonRect, panelRect ) ).to.deep.equal( {
  313. top: 150,
  314. left: 150,
  315. name: 'nw'
  316. } );
  317. } );
  318. } );
  319. } );