alignmentui.js 9.4 KB

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