dropdownview.js 11 KB

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