createbuttondropdown.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import Model from '../../../src/model';
  7. import createButtonDropdown from '../../../src/dropdown/button/createbuttondropdown';
  8. import ButtonView from '../../../src/button/buttonview';
  9. import ToolbarView from '../../../src/toolbar/toolbarview';
  10. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. describe( 'createButtonDropdown', () => {
  12. let view, model, locale, buttons;
  13. beforeEach( () => {
  14. locale = { t() {} };
  15. buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
  16. const button = new ButtonView();
  17. button.icon = icon;
  18. return button;
  19. } );
  20. model = new Model( {
  21. isVertical: true,
  22. buttons
  23. } );
  24. view = createButtonDropdown( model, locale );
  25. view.render();
  26. document.body.appendChild( view.element );
  27. } );
  28. afterEach( () => {
  29. view.element.remove();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'sets view#locale', () => {
  33. expect( view.locale ).to.equal( locale );
  34. } );
  35. describe( 'view#toolbarView', () => {
  36. it( 'is created', () => {
  37. const panelChildren = view.panelView.children;
  38. expect( panelChildren ).to.have.length( 1 );
  39. expect( panelChildren.get( 0 ) ).to.equal( view.toolbarView );
  40. expect( view.toolbarView ).to.be.instanceof( ToolbarView );
  41. } );
  42. it( 'delegates view.toolbarView#execute to the view', done => {
  43. view.on( 'execute', evt => {
  44. expect( evt.source ).to.equal( view.toolbarView.items.get( 0 ) );
  45. expect( evt.path ).to.deep.equal( [ view.toolbarView.items.get( 0 ), view ] );
  46. done();
  47. } );
  48. view.toolbarView.items.get( 0 ).fire( 'execute' );
  49. } );
  50. it( 'reacts on model#isVertical', () => {
  51. model.isVertical = false;
  52. expect( view.toolbarView.isVertical ).to.be.false;
  53. model.isVertical = true;
  54. expect( view.toolbarView.isVertical ).to.be.true;
  55. } );
  56. it( 'reacts on model#toolbarClassName', () => {
  57. expect( view.toolbarView.className ).to.be.undefined;
  58. model.set( 'toolbarClassName', 'foo' );
  59. expect( view.toolbarView.className ).to.equal( 'foo' );
  60. } );
  61. } );
  62. it( 'changes view#isOpen on view#execute', () => {
  63. view.isOpen = true;
  64. view.fire( 'execute' );
  65. expect( view.isOpen ).to.be.false;
  66. view.fire( 'execute' );
  67. expect( view.isOpen ).to.be.false;
  68. } );
  69. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  70. // Open the dropdown.
  71. view.isOpen = true;
  72. // Fire event from outside of the dropdown.
  73. document.body.dispatchEvent( new Event( 'mousedown', {
  74. bubbles: true
  75. } ) );
  76. // Closed the dropdown.
  77. expect( view.isOpen ).to.be.false;
  78. // Fire event from outside of the dropdown.
  79. document.body.dispatchEvent( new Event( 'mousedown', {
  80. bubbles: true
  81. } ) );
  82. // Dropdown is still closed.
  83. expect( view.isOpen ).to.be.false;
  84. } );
  85. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  86. // Open the dropdown.
  87. view.isOpen = true;
  88. // Event from view.element should be discarded.
  89. view.element.dispatchEvent( new Event( 'mousedown', {
  90. bubbles: true
  91. } ) );
  92. // Dropdown is still open.
  93. expect( view.isOpen ).to.be.true;
  94. // Event from within view.element should be discarded.
  95. const child = document.createElement( 'div' );
  96. view.element.appendChild( child );
  97. child.dispatchEvent( new Event( 'mousedown', {
  98. bubbles: true
  99. } ) );
  100. // Dropdown is still open.
  101. expect( view.isOpen ).to.be.true;
  102. } );
  103. describe( 'activates keyboard navigation for the dropdown', () => {
  104. it( 'so "arrowdown" focuses the #toolbarView if dropdown is open', () => {
  105. const keyEvtData = {
  106. keyCode: keyCodes.arrowdown,
  107. preventDefault: sinon.spy(),
  108. stopPropagation: sinon.spy()
  109. };
  110. const spy = sinon.spy( view.toolbarView, 'focus' );
  111. view.isOpen = false;
  112. view.keystrokes.press( keyEvtData );
  113. sinon.assert.notCalled( spy );
  114. view.isOpen = true;
  115. view.keystrokes.press( keyEvtData );
  116. sinon.assert.calledOnce( spy );
  117. } );
  118. it( 'so "arrowup" focuses the last #item in #toolbarView if dropdown is open', () => {
  119. const keyEvtData = {
  120. keyCode: keyCodes.arrowup,
  121. preventDefault: sinon.spy(),
  122. stopPropagation: sinon.spy()
  123. };
  124. const spy = sinon.spy( view.toolbarView, 'focusLast' );
  125. view.isOpen = false;
  126. view.keystrokes.press( keyEvtData );
  127. sinon.assert.notCalled( spy );
  128. view.isOpen = true;
  129. view.keystrokes.press( keyEvtData );
  130. sinon.assert.calledOnce( spy );
  131. } );
  132. } );
  133. describe( 'icon', () => {
  134. it( 'should be set to first button\'s icon if no defaultIcon defined', () => {
  135. expect( view.buttonView.icon ).to.equal( view.toolbarView.items.get( 0 ).icon );
  136. } );
  137. it( 'should be bound to first button that is on', () => {
  138. view.toolbarView.items.get( 1 ).isOn = true;
  139. expect( view.buttonView.icon ).to.equal( view.toolbarView.items.get( 1 ).icon );
  140. view.toolbarView.items.get( 0 ).isOn = true;
  141. view.toolbarView.items.get( 1 ).isOn = false;
  142. expect( view.buttonView.icon ).to.equal( view.toolbarView.items.get( 0 ).icon );
  143. } );
  144. it( 'should be set to defaultIcon if defined and on button is on', () => {
  145. const model = new Model( {
  146. defaultIcon: '<svg>baz</svg>',
  147. buttons
  148. } );
  149. view = createButtonDropdown( model, locale );
  150. view.render();
  151. expect( view.buttonView.icon ).to.equal( '<svg>baz</svg>' );
  152. } );
  153. it( 'should not bind icons if staticIcon is set', () => {
  154. const model = new Model( {
  155. defaultIcon: '<svg>baz</svg>',
  156. staticIcon: true,
  157. buttons
  158. } );
  159. view = createButtonDropdown( model, locale );
  160. view.render();
  161. expect( view.buttonView.icon ).to.equal( '<svg>baz</svg>' );
  162. view.toolbarView.items.get( 1 ).isOn = true;
  163. expect( view.buttonView.icon ).to.equal( '<svg>baz</svg>' );
  164. } );
  165. } );
  166. } );
  167. } );