alignmentui.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * @license Copyright (c) 2003-2017, 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', () => {
  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. it( 'requires AlignmentEditing', () => {
  28. expect( AlignmentUI.requires ).to.deep.equal( [ AlignmentEditing ] );
  29. } );
  30. describe( 'localizedStylesTitles()', () => {
  31. it( 'should return localized titles of styles', () => {
  32. const editorMock = { t: str => str };
  33. const plugin = new AlignmentUI( editorMock );
  34. expect( plugin.localizedStylesTitles ).to.deep.equal( {
  35. 'left': 'Align left',
  36. 'right': 'Align right',
  37. 'center': 'Align center',
  38. 'justify': 'Justify'
  39. } );
  40. } );
  41. } );
  42. describe( 'alignLeft button', () => {
  43. beforeEach( () => {
  44. command = editor.commands.get( 'alignLeft' );
  45. button = editor.ui.componentFactory.create( 'alignLeft' );
  46. } );
  47. it( 'has the base properties', () => {
  48. expect( button ).to.have.property( 'label', 'Align left' );
  49. expect( button ).to.have.property( 'icon' );
  50. expect( button ).to.have.property( 'tooltip', true );
  51. } );
  52. it( 'has isOn bound to command\'s value', () => {
  53. command.value = false;
  54. expect( button ).to.have.property( 'isOn', false );
  55. command.value = true;
  56. expect( button ).to.have.property( 'isOn', true );
  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( 'alignLeft' );
  69. } );
  70. } );
  71. describe( 'alignRight button', () => {
  72. beforeEach( () => {
  73. command = editor.commands.get( 'alignRight' );
  74. button = editor.ui.componentFactory.create( 'alignRight' );
  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 = true;
  85. expect( button ).to.have.property( 'isOn', true );
  86. } );
  87. it( 'has isEnabled bound to command\'s isEnabled', () => {
  88. command.isEnabled = true;
  89. expect( button ).to.have.property( 'isEnabled', true );
  90. command.isEnabled = false;
  91. expect( button ).to.have.property( 'isEnabled', false );
  92. } );
  93. it( 'executes command when it\'s executed', () => {
  94. const spy = sinon.stub( editor, 'execute' );
  95. button.fire( 'execute' );
  96. expect( spy.calledOnce ).to.be.true;
  97. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignRight' );
  98. } );
  99. } );
  100. describe( 'alignCenter button', () => {
  101. beforeEach( () => {
  102. command = editor.commands.get( 'alignCenter' );
  103. button = editor.ui.componentFactory.create( 'alignCenter' );
  104. } );
  105. it( 'has the base properties', () => {
  106. expect( button ).to.have.property( 'label', 'Align center' );
  107. expect( button ).to.have.property( 'icon' );
  108. expect( button ).to.have.property( 'tooltip', true );
  109. } );
  110. it( 'has isOn bound to command\'s value', () => {
  111. command.value = false;
  112. expect( button ).to.have.property( 'isOn', false );
  113. command.value = true;
  114. expect( button ).to.have.property( 'isOn', true );
  115. } );
  116. it( 'has isEnabled bound to command\'s isEnabled', () => {
  117. command.isEnabled = true;
  118. expect( button ).to.have.property( 'isEnabled', true );
  119. command.isEnabled = false;
  120. expect( button ).to.have.property( 'isEnabled', false );
  121. } );
  122. it( 'executes command when it\'s executed', () => {
  123. const spy = sinon.stub( editor, 'execute' );
  124. button.fire( 'execute' );
  125. expect( spy.calledOnce ).to.be.true;
  126. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignCenter' );
  127. } );
  128. } );
  129. describe( 'alignJustify button', () => {
  130. beforeEach( () => {
  131. command = editor.commands.get( 'alignJustify' );
  132. button = editor.ui.componentFactory.create( 'alignJustify' );
  133. } );
  134. it( 'has the base properties', () => {
  135. expect( button ).to.have.property( 'label', 'Justify' );
  136. expect( button ).to.have.property( 'icon' );
  137. expect( button ).to.have.property( 'tooltip', true );
  138. } );
  139. it( 'has isOn bound to command\'s value', () => {
  140. command.value = false;
  141. expect( button ).to.have.property( 'isOn', false );
  142. command.value = true;
  143. expect( button ).to.have.property( 'isOn', true );
  144. } );
  145. it( 'has isEnabled bound to command\'s isEnabled', () => {
  146. command.isEnabled = true;
  147. expect( button ).to.have.property( 'isEnabled', true );
  148. command.isEnabled = false;
  149. expect( button ).to.have.property( 'isEnabled', false );
  150. } );
  151. it( 'executes command when it\'s executed', () => {
  152. const spy = sinon.stub( editor, 'execute' );
  153. button.fire( 'execute' );
  154. expect( spy.calledOnce ).to.be.true;
  155. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignJustify' );
  156. } );
  157. } );
  158. describe( 'alignmentDropdown', () => {
  159. let dropdown;
  160. beforeEach( () => {
  161. command = editor.commands.get( 'alignLeft' );
  162. dropdown = editor.ui.componentFactory.create( 'alignmentDropdown' );
  163. } );
  164. it( 'button has the base properties', () => {
  165. const button = dropdown.buttonView;
  166. expect( button ).to.have.property( 'label', 'Text alignment' );
  167. expect( button ).to.have.property( 'icon' );
  168. expect( button ).to.have.property( 'withText', false );
  169. } );
  170. it( 'buttonGroup has the base properties', () => {
  171. const buttonGroup = dropdown.buttonGroupView;
  172. expect( buttonGroup ).to.have.property( 'isVertical', true );
  173. } );
  174. it( 'should hold defined buttons', () => {
  175. const items = [ ...dropdown.buttonGroupView.items ].map( item => item.label );
  176. expect( items ).to.have.length( 4 );
  177. expect( items.includes( 'Align left' ) ).to.be.true;
  178. expect( items.includes( 'Align right' ) ).to.be.true;
  179. expect( items.includes( 'Align center' ) ).to.be.true;
  180. expect( items.includes( 'Justify' ) ).to.be.true;
  181. } );
  182. describe( 'config', () => {
  183. beforeEach( () => {
  184. element = document.createElement( 'div' );
  185. document.body.appendChild( element );
  186. return ClassicTestEditor
  187. .create( element, {
  188. plugins: [ AlignmentEditing, AlignmentUI ],
  189. alignment: { styles: [ 'center', 'justify' ] }
  190. } )
  191. .then( newEditor => {
  192. editor = newEditor;
  193. dropdown = editor.ui.componentFactory.create( 'alignmentDropdown' );
  194. command = editor.commands.get( 'alignCenter' );
  195. button = editor.ui.componentFactory.create( 'alignCenter' );
  196. } );
  197. } );
  198. it( 'should hold only defined buttons', () => {
  199. const items = [ ...dropdown.buttonGroupView.items ].map( item => item.label );
  200. expect( items ).to.have.length( 2 );
  201. expect( items.includes( 'Align center' ) ).to.be.true;
  202. expect( items.includes( 'Justify' ) ).to.be.true;
  203. } );
  204. it( 'should have default icon set', () => {
  205. expect( dropdown.buttonView.icon ).to.equal( alignLeftIcon );
  206. } );
  207. it( 'should change icon to active alignment', () => {
  208. command.value = true;
  209. expect( dropdown.buttonView.icon ).to.equal( button.icon );
  210. } );
  211. } );
  212. } );
  213. } );