dropdownview.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. describe( 'DropdownView', () => {
  12. let view, buttonView, panelView, locale;
  13. beforeEach( () => {
  14. locale = { t() {} };
  15. buttonView = new ButtonView( locale );
  16. panelView = new DropdownPanelView( locale );
  17. return ( view = new DropdownView( locale, buttonView, panelView ) ).init();
  18. } );
  19. describe( 'constructor()', () => {
  20. it( 'sets view#locale', () => {
  21. expect( view.locale ).to.equal( locale );
  22. } );
  23. it( 'sets view#buttonView', () => {
  24. expect( view.buttonView ).to.equal( buttonView );
  25. } );
  26. it( 'sets view#panelView', () => {
  27. expect( view.panelView ).to.equal( panelView );
  28. } );
  29. it( 'sets view#isOpen false', () => {
  30. expect( view.isOpen ).to.be.false;
  31. } );
  32. it( 'creates #focusTracker instance', () => {
  33. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  34. } );
  35. it( 'creates #keystrokeHandler instance', () => {
  36. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  37. } );
  38. it( 'creates #element from template', () => {
  39. expect( view.element.classList.contains( 'ck-dropdown' ) ).to.be.true;
  40. expect( view.element.firstChild ).to.equal( buttonView.element );
  41. expect( view.element.lastChild ).to.equal( panelView.element );
  42. } );
  43. it( 'sets view#buttonView class', () => {
  44. expect( view.buttonView.element.classList.contains( 'ck-dropdown__button' ) ).to.be.true;
  45. } );
  46. describe( 'bindings', () => {
  47. describe( 'view#isOpen to view.buttonView#execute', () => {
  48. it( 'is activated', () => {
  49. const values = [];
  50. view.on( 'change:isOpen', () => {
  51. values.push( view.isOpen );
  52. } );
  53. view.buttonView.fire( 'execute' );
  54. view.buttonView.fire( 'execute' );
  55. view.buttonView.fire( 'execute' );
  56. expect( values ).to.have.members( [ true, false, true ] );
  57. } );
  58. } );
  59. describe( 'view.panelView#isVisible to view#isOpen', () => {
  60. it( 'is activated', () => {
  61. const values = [];
  62. view.listenTo( view.panelView, 'change:isVisible', () => {
  63. values.push( view.isOpen );
  64. } );
  65. view.isOpen = true;
  66. view.isOpen = false;
  67. view.isOpen = true;
  68. expect( values ).to.have.members( [ true, false, true ] );
  69. } );
  70. } );
  71. } );
  72. } );
  73. describe( 'init()', () => {
  74. it( 'starts listening for #keystrokes coming from #element', () => {
  75. view = new DropdownView( locale,
  76. new ButtonView( locale ),
  77. new DropdownPanelView( locale ) );
  78. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  79. return view.init().then( () => {
  80. sinon.assert.calledOnce( spy );
  81. sinon.assert.calledWithExactly( spy, view.element );
  82. } );
  83. } );
  84. it( 'adds #element to #focusTracker', () => {
  85. view = new DropdownView( locale,
  86. new ButtonView( locale ),
  87. new DropdownPanelView( locale ) );
  88. const spy = sinon.spy( view.focusTracker, 'add' );
  89. return view.init().then( () => {
  90. sinon.assert.calledOnce( spy );
  91. sinon.assert.calledWithExactly( spy, view.element );
  92. } );
  93. } );
  94. describe( 'activates keyboard navigation for the dropdown', () => {
  95. it( 'so "arrowdown" opens the #panelView', () => {
  96. const keyEvtData = {
  97. keyCode: keyCodes.arrowdown,
  98. preventDefault: sinon.spy(),
  99. stopPropagation: sinon.spy()
  100. };
  101. view.isOpen = true;
  102. view.keystrokes.press( keyEvtData );
  103. sinon.assert.notCalled( keyEvtData.preventDefault );
  104. sinon.assert.notCalled( keyEvtData.stopPropagation );
  105. expect( view.isOpen ).to.be.true;
  106. view.isOpen = false;
  107. view.keystrokes.press( keyEvtData );
  108. sinon.assert.calledOnce( keyEvtData.preventDefault );
  109. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  110. expect( view.isOpen ).to.be.true;
  111. } );
  112. it( 'so "arrowright" is blocked', () => {
  113. const keyEvtData = {
  114. keyCode: keyCodes.arrowright,
  115. preventDefault: sinon.spy(),
  116. stopPropagation: sinon.spy()
  117. };
  118. view.false = true;
  119. view.keystrokes.press( keyEvtData );
  120. sinon.assert.notCalled( keyEvtData.preventDefault );
  121. sinon.assert.notCalled( keyEvtData.stopPropagation );
  122. expect( view.isOpen ).to.be.false;
  123. view.isOpen = true;
  124. view.keystrokes.press( keyEvtData );
  125. sinon.assert.calledOnce( keyEvtData.preventDefault );
  126. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  127. expect( view.isOpen ).to.be.true;
  128. } );
  129. it( 'so "arrowleft" closes the #panelView', () => {
  130. const keyEvtData = {
  131. keyCode: keyCodes.arrowleft,
  132. preventDefault: sinon.spy(),
  133. stopPropagation: sinon.spy()
  134. };
  135. const spy = sinon.spy( view.buttonView, 'focus' );
  136. view.isOpen = false;
  137. view.keystrokes.press( keyEvtData );
  138. sinon.assert.notCalled( keyEvtData.preventDefault );
  139. sinon.assert.notCalled( keyEvtData.stopPropagation );
  140. sinon.assert.notCalled( spy );
  141. expect( view.isOpen ).to.be.false;
  142. view.isOpen = true;
  143. view.keystrokes.press( keyEvtData );
  144. sinon.assert.calledOnce( keyEvtData.preventDefault );
  145. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  146. sinon.assert.calledOnce( spy );
  147. expect( view.isOpen ).to.be.false;
  148. } );
  149. it( 'so "esc" closes the #panelView', () => {
  150. const keyEvtData = {
  151. keyCode: keyCodes.esc,
  152. preventDefault: sinon.spy(),
  153. stopPropagation: sinon.spy()
  154. };
  155. const spy = sinon.spy( view.buttonView, 'focus' );
  156. view.isOpen = false;
  157. view.keystrokes.press( keyEvtData );
  158. sinon.assert.notCalled( keyEvtData.preventDefault );
  159. sinon.assert.notCalled( keyEvtData.stopPropagation );
  160. sinon.assert.notCalled( spy );
  161. expect( view.isOpen ).to.be.false;
  162. view.isOpen = true;
  163. view.keystrokes.press( keyEvtData );
  164. sinon.assert.calledOnce( keyEvtData.preventDefault );
  165. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  166. sinon.assert.calledOnce( spy );
  167. expect( view.isOpen ).to.be.false;
  168. } );
  169. } );
  170. } );
  171. describe( 'focus()', () => {
  172. it( 'focuses the #buttonView in DOM', () => {
  173. const spy = sinon.spy( view.buttonView, 'focus' );
  174. view.focus();
  175. sinon.assert.calledOnce( spy );
  176. } );
  177. } );
  178. } );