alignmentui.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import AlignmentEditing from '../src/alignmentediting';
  7. import AlignmentUI from '../src/alignmentui';
  8. import alignLeftIcon from '../theme/icons/align-left.svg';
  9. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  10. describe( 'Alignment UI', () => {
  11. let editor, command, element, button;
  12. beforeEach( () => {
  13. element = document.createElement( 'div' );
  14. document.body.appendChild( element );
  15. return ClassicTestEditor
  16. .create( element, {
  17. plugins: [ AlignmentEditing, AlignmentUI ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. } );
  22. } );
  23. afterEach( () => {
  24. element.remove();
  25. return editor.destroy();
  26. } );
  27. describe( 'localizedOptionTitles()', () => {
  28. it( 'should return localized titles of options', () => {
  29. const editorMock = { t: str => str };
  30. const plugin = new AlignmentUI( editorMock );
  31. expect( plugin.localizedOptionTitles ).to.deep.equal( {
  32. 'left': 'Align left',
  33. 'right': 'Align right',
  34. 'center': 'Align center',
  35. 'justify': 'Justify'
  36. } );
  37. } );
  38. } );
  39. describe( 'alignment:left button', () => {
  40. beforeEach( () => {
  41. command = editor.commands.get( 'alignment' );
  42. button = editor.ui.componentFactory.create( 'alignment:left' );
  43. } );
  44. it( 'has the base properties', () => {
  45. expect( button ).to.have.property( 'label', 'Align left' );
  46. expect( button ).to.have.property( 'icon' );
  47. expect( button ).to.have.property( 'tooltip', true );
  48. } );
  49. it( 'has isOn bound to command\'s value', () => {
  50. command.value = false;
  51. expect( button ).to.have.property( 'isOn', false );
  52. command.value = 'left';
  53. expect( button ).to.have.property( 'isOn', true );
  54. command.value = 'justify';
  55. expect( button ).to.have.property( 'isOn', false );
  56. } );
  57. it( 'has isEnabled bound to command\'s isEnabled', () => {
  58. command.isEnabled = true;
  59. expect( button ).to.have.property( 'isEnabled', true );
  60. command.isEnabled = false;
  61. expect( button ).to.have.property( 'isEnabled', false );
  62. } );
  63. it( 'executes command when it\'s executed', () => {
  64. const spy = sinon.stub( editor, 'execute' );
  65. button.fire( 'execute' );
  66. expect( spy.calledOnce ).to.be.true;
  67. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  68. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'left' } );
  69. } );
  70. } );
  71. describe( 'alignment:right button', () => {
  72. beforeEach( () => {
  73. command = editor.commands.get( 'alignment' );
  74. button = editor.ui.componentFactory.create( 'alignment:right' );
  75. } );
  76. it( 'has the base properties', () => {
  77. expect( button ).to.have.property( 'label', 'Align right' );
  78. expect( button ).to.have.property( 'icon' );
  79. expect( button ).to.have.property( 'tooltip', true );
  80. } );
  81. it( 'has isOn bound to command\'s value', () => {
  82. command.value = false;
  83. expect( button ).to.have.property( 'isOn', false );
  84. command.value = 'right';
  85. expect( button ).to.have.property( 'isOn', true );
  86. command.value = 'justify';
  87. expect( button ).to.have.property( 'isOn', false );
  88. } );
  89. it( 'has isEnabled bound to command\'s isEnabled', () => {
  90. command.isEnabled = true;
  91. expect( button ).to.have.property( 'isEnabled', true );
  92. command.isEnabled = false;
  93. expect( button ).to.have.property( 'isEnabled', false );
  94. } );
  95. it( 'executes command when it\'s executed', () => {
  96. const spy = sinon.stub( editor, 'execute' );
  97. button.fire( 'execute' );
  98. expect( spy.calledOnce ).to.be.true;
  99. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  100. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'right' } );
  101. } );
  102. } );
  103. describe( 'alignment:center button', () => {
  104. beforeEach( () => {
  105. command = editor.commands.get( 'alignment' );
  106. button = editor.ui.componentFactory.create( 'alignment:center' );
  107. } );
  108. it( 'has the base properties', () => {
  109. expect( button ).to.have.property( 'label', 'Align center' );
  110. expect( button ).to.have.property( 'icon' );
  111. expect( button ).to.have.property( 'tooltip', true );
  112. } );
  113. it( 'has isOn bound to command\'s value', () => {
  114. command.value = false;
  115. expect( button ).to.have.property( 'isOn', false );
  116. command.value = 'center';
  117. expect( button ).to.have.property( 'isOn', true );
  118. command.value = 'justify';
  119. expect( button ).to.have.property( 'isOn', false );
  120. } );
  121. it( 'has isEnabled bound to command\'s isEnabled', () => {
  122. command.isEnabled = true;
  123. expect( button ).to.have.property( 'isEnabled', true );
  124. command.isEnabled = false;
  125. expect( button ).to.have.property( 'isEnabled', false );
  126. } );
  127. it( 'executes command when it\'s executed', () => {
  128. const spy = sinon.stub( editor, 'execute' );
  129. button.fire( 'execute' );
  130. expect( spy.calledOnce ).to.be.true;
  131. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  132. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'center' } );
  133. } );
  134. } );
  135. describe( 'alignment:justify button', () => {
  136. beforeEach( () => {
  137. command = editor.commands.get( 'alignment' );
  138. button = editor.ui.componentFactory.create( 'alignment:justify' );
  139. } );
  140. it( 'has the base properties', () => {
  141. expect( button ).to.have.property( 'label', 'Justify' );
  142. expect( button ).to.have.property( 'icon' );
  143. expect( button ).to.have.property( 'tooltip', true );
  144. } );
  145. it( 'has isOn bound to command\'s value', () => {
  146. command.value = false;
  147. expect( button ).to.have.property( 'isOn', false );
  148. command.value = 'justify';
  149. expect( button ).to.have.property( 'isOn', true );
  150. command.value = 'center';
  151. expect( button ).to.have.property( 'isOn', false );
  152. } );
  153. it( 'has isEnabled bound to command\'s isEnabled', () => {
  154. command.isEnabled = true;
  155. expect( button ).to.have.property( 'isEnabled', true );
  156. command.isEnabled = false;
  157. expect( button ).to.have.property( 'isEnabled', false );
  158. } );
  159. it( 'executes command when it\'s executed', () => {
  160. const spy = sinon.stub( editor, 'execute' );
  161. button.fire( 'execute' );
  162. expect( spy.calledOnce ).to.be.true;
  163. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  164. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'justify' } );
  165. } );
  166. } );
  167. describe( 'alignment', () => {
  168. let dropdown;
  169. beforeEach( () => {
  170. command = editor.commands.get( 'alignment' );
  171. dropdown = editor.ui.componentFactory.create( 'alignment' );
  172. } );
  173. it( '#buttonView has the base properties', () => {
  174. const button = dropdown.buttonView;
  175. expect( button ).to.have.property( 'label', 'Text alignment' );
  176. expect( button ).to.have.property( 'icon' );
  177. expect( button ).to.have.property( 'tooltip', true );
  178. } );
  179. it( 'should add custom CSS class to dropdown', () => {
  180. dropdown.render();
  181. expect( dropdown.element.classList.contains( 'ck-alignment-dropdown' ) ).to.be.true;
  182. } );
  183. it( '#toolbarView has the base properties', () => {
  184. const toolbarView = dropdown.toolbarView;
  185. expect( toolbarView ).to.have.property( 'isVertical', true );
  186. } );
  187. it( 'should hold defined buttons', () => {
  188. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  189. expect( items ).to.have.length( 4 );
  190. expect( items.includes( 'Align left' ) ).to.be.true;
  191. expect( items.includes( 'Align right' ) ).to.be.true;
  192. expect( items.includes( 'Align center' ) ).to.be.true;
  193. expect( items.includes( 'Justify' ) ).to.be.true;
  194. } );
  195. describe( 'config', () => {
  196. beforeEach( () => {
  197. element = document.createElement( 'div' );
  198. document.body.appendChild( element );
  199. return ClassicTestEditor
  200. .create( element, {
  201. plugins: [ AlignmentEditing, AlignmentUI ],
  202. alignment: { options: [ 'center', 'justify' ] }
  203. } )
  204. .then( newEditor => {
  205. editor = newEditor;
  206. dropdown = editor.ui.componentFactory.create( 'alignment' );
  207. command = editor.commands.get( 'alignment' );
  208. button = editor.ui.componentFactory.create( 'alignment:center' );
  209. } );
  210. } );
  211. it( 'should hold only defined buttons', () => {
  212. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  213. expect( items ).to.have.length( 2 );
  214. expect( items.includes( 'Align center' ) ).to.be.true;
  215. expect( items.includes( 'Justify' ) ).to.be.true;
  216. } );
  217. it( 'should have default icon set', () => {
  218. expect( dropdown.buttonView.icon ).to.equal( alignLeftIcon );
  219. } );
  220. it( 'should change icon to active alignment', () => {
  221. command.value = 'center';
  222. expect( dropdown.buttonView.icon ).to.equal( button.icon );
  223. } );
  224. } );
  225. } );
  226. } );