alignmentui.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 '../theme/icons/align-left.svg';
  9. import alignRightIcon from '../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. } );
  50. it( 'has isOn bound to command\'s value', () => {
  51. command.value = false;
  52. expect( button ).to.have.property( 'isOn', false );
  53. command.value = 'left';
  54. expect( button ).to.have.property( 'isOn', true );
  55. command.value = 'justify';
  56. expect( button ).to.have.property( 'isOn', false );
  57. } );
  58. it( 'has isEnabled bound to command\'s isEnabled', () => {
  59. command.isEnabled = true;
  60. expect( button ).to.have.property( 'isEnabled', true );
  61. command.isEnabled = false;
  62. expect( button ).to.have.property( 'isEnabled', false );
  63. } );
  64. it( 'executes command when it\'s executed', () => {
  65. const spy = sinon.stub( editor, 'execute' );
  66. button.fire( 'execute' );
  67. expect( spy.calledOnce ).to.be.true;
  68. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  69. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'left' } );
  70. } );
  71. } );
  72. describe( 'alignment:right button', () => {
  73. beforeEach( () => {
  74. command = editor.commands.get( 'alignment' );
  75. button = editor.ui.componentFactory.create( 'alignment:right' );
  76. } );
  77. it( 'has the base properties', () => {
  78. expect( button ).to.have.property( 'label', 'Align right' );
  79. expect( button ).to.have.property( 'icon' );
  80. expect( button ).to.have.property( 'tooltip', true );
  81. } );
  82. it( 'has isOn bound to command\'s value', () => {
  83. command.value = false;
  84. expect( button ).to.have.property( 'isOn', false );
  85. command.value = 'right';
  86. expect( button ).to.have.property( 'isOn', true );
  87. command.value = 'justify';
  88. expect( button ).to.have.property( 'isOn', false );
  89. } );
  90. it( 'has isEnabled bound to command\'s isEnabled', () => {
  91. command.isEnabled = true;
  92. expect( button ).to.have.property( 'isEnabled', true );
  93. command.isEnabled = false;
  94. expect( button ).to.have.property( 'isEnabled', false );
  95. } );
  96. it( 'executes command when it\'s executed', () => {
  97. const spy = sinon.stub( editor, 'execute' );
  98. button.fire( 'execute' );
  99. expect( spy.calledOnce ).to.be.true;
  100. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  101. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'right' } );
  102. } );
  103. } );
  104. describe( 'alignment:center button', () => {
  105. beforeEach( () => {
  106. command = editor.commands.get( 'alignment' );
  107. button = editor.ui.componentFactory.create( 'alignment:center' );
  108. } );
  109. it( 'has the base properties', () => {
  110. expect( button ).to.have.property( 'label', 'Align center' );
  111. expect( button ).to.have.property( 'icon' );
  112. expect( button ).to.have.property( 'tooltip', true );
  113. } );
  114. it( 'has isOn bound to command\'s value', () => {
  115. command.value = false;
  116. expect( button ).to.have.property( 'isOn', false );
  117. command.value = 'center';
  118. expect( button ).to.have.property( 'isOn', true );
  119. command.value = 'justify';
  120. expect( button ).to.have.property( 'isOn', false );
  121. } );
  122. it( 'has isEnabled bound to command\'s isEnabled', () => {
  123. command.isEnabled = true;
  124. expect( button ).to.have.property( 'isEnabled', true );
  125. command.isEnabled = false;
  126. expect( button ).to.have.property( 'isEnabled', false );
  127. } );
  128. it( 'executes command when it\'s executed', () => {
  129. const spy = sinon.stub( editor, 'execute' );
  130. button.fire( 'execute' );
  131. expect( spy.calledOnce ).to.be.true;
  132. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  133. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'center' } );
  134. } );
  135. } );
  136. describe( 'alignment:justify button', () => {
  137. beforeEach( () => {
  138. command = editor.commands.get( 'alignment' );
  139. button = editor.ui.componentFactory.create( 'alignment:justify' );
  140. } );
  141. it( 'has the base properties', () => {
  142. expect( button ).to.have.property( 'label', 'Justify' );
  143. expect( button ).to.have.property( 'icon' );
  144. expect( button ).to.have.property( 'tooltip', true );
  145. } );
  146. it( 'has isOn bound to command\'s value', () => {
  147. command.value = false;
  148. expect( button ).to.have.property( 'isOn', false );
  149. command.value = 'justify';
  150. expect( button ).to.have.property( 'isOn', true );
  151. command.value = 'center';
  152. expect( button ).to.have.property( 'isOn', false );
  153. } );
  154. it( 'has isEnabled bound to command\'s isEnabled', () => {
  155. command.isEnabled = true;
  156. expect( button ).to.have.property( 'isEnabled', true );
  157. command.isEnabled = false;
  158. expect( button ).to.have.property( 'isEnabled', false );
  159. } );
  160. it( 'executes command when it\'s executed', () => {
  161. const spy = sinon.stub( editor, 'execute' );
  162. button.fire( 'execute' );
  163. expect( spy.calledOnce ).to.be.true;
  164. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignment' );
  165. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( { value: 'justify' } );
  166. } );
  167. } );
  168. describe( 'alignment', () => {
  169. let dropdown;
  170. beforeEach( () => {
  171. command = editor.commands.get( 'alignment' );
  172. dropdown = editor.ui.componentFactory.create( 'alignment' );
  173. } );
  174. it( '#buttonView has the base properties', () => {
  175. const button = dropdown.buttonView;
  176. expect( button ).to.have.property( 'label', 'Text alignment' );
  177. expect( button ).to.have.property( 'icon' );
  178. expect( button ).to.have.property( 'tooltip', true );
  179. } );
  180. it( 'should add custom CSS class to dropdown', () => {
  181. dropdown.render();
  182. expect( dropdown.element.classList.contains( 'ck-alignment-dropdown' ) ).to.be.true;
  183. } );
  184. it( '#toolbarView has the base properties', () => {
  185. const toolbarView = dropdown.toolbarView;
  186. expect( toolbarView ).to.have.property( 'isVertical', true );
  187. } );
  188. it( 'should hold defined buttons', () => {
  189. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  190. expect( items ).to.have.length( 4 );
  191. expect( items.includes( 'Align left' ) ).to.be.true;
  192. expect( items.includes( 'Align right' ) ).to.be.true;
  193. expect( items.includes( 'Align center' ) ).to.be.true;
  194. expect( items.includes( 'Justify' ) ).to.be.true;
  195. } );
  196. describe( 'config', () => {
  197. beforeEach( () => {
  198. element = document.createElement( 'div' );
  199. document.body.appendChild( element );
  200. return ClassicTestEditor
  201. .create( element, {
  202. plugins: [ AlignmentEditing, AlignmentUI ],
  203. alignment: { options: [ 'center', 'justify' ] }
  204. } )
  205. .then( newEditor => {
  206. editor = newEditor;
  207. dropdown = editor.ui.componentFactory.create( 'alignment' );
  208. command = editor.commands.get( 'alignment' );
  209. button = editor.ui.componentFactory.create( 'alignment:center' );
  210. } );
  211. } );
  212. it( 'should hold only defined buttons', () => {
  213. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  214. expect( items ).to.have.length( 2 );
  215. expect( items.includes( 'Align center' ) ).to.be.true;
  216. expect( items.includes( 'Justify' ) ).to.be.true;
  217. } );
  218. it( 'should have default icon set (LTR content)', () => {
  219. expect( dropdown.buttonView.icon ).to.equal( alignLeftIcon );
  220. } );
  221. it( 'should have default icon set (RTL content)', () => {
  222. const element = document.createElement( 'div' );
  223. document.body.appendChild( element );
  224. return ClassicTestEditor
  225. .create( element, {
  226. language: {
  227. content: 'ar'
  228. },
  229. plugins: [ AlignmentEditing, AlignmentUI ],
  230. alignment: { options: [ 'center', 'justify' ] }
  231. } )
  232. .then( newEditor => {
  233. dropdown = newEditor.ui.componentFactory.create( 'alignment' );
  234. expect( dropdown.buttonView.icon ).to.equal( alignRightIcon );
  235. return newEditor.destroy();
  236. } );
  237. } );
  238. it( 'should change icon to active alignment', () => {
  239. command.value = 'center';
  240. expect( dropdown.buttonView.icon ).to.equal( button.icon );
  241. } );
  242. } );
  243. } );
  244. } );