alignmentui.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Copyright (c) 2003-2018, 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( 'alignLeft button', () => {
  40. beforeEach( () => {
  41. command = editor.commands.get( 'alignLeft' );
  42. button = editor.ui.componentFactory.create( 'alignLeft' );
  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 = true;
  53. expect( button ).to.have.property( 'isOn', true );
  54. } );
  55. it( 'has isEnabled bound to command\'s isEnabled', () => {
  56. command.isEnabled = true;
  57. expect( button ).to.have.property( 'isEnabled', true );
  58. command.isEnabled = false;
  59. expect( button ).to.have.property( 'isEnabled', false );
  60. } );
  61. it( 'executes command when it\'s executed', () => {
  62. const spy = sinon.stub( editor, 'execute' );
  63. button.fire( 'execute' );
  64. expect( spy.calledOnce ).to.be.true;
  65. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignLeft' );
  66. } );
  67. } );
  68. describe( 'alignRight button', () => {
  69. beforeEach( () => {
  70. command = editor.commands.get( 'alignRight' );
  71. button = editor.ui.componentFactory.create( 'alignRight' );
  72. } );
  73. it( 'has the base properties', () => {
  74. expect( button ).to.have.property( 'label', 'Align right' );
  75. expect( button ).to.have.property( 'icon' );
  76. expect( button ).to.have.property( 'tooltip', true );
  77. } );
  78. it( 'has isOn bound to command\'s value', () => {
  79. command.value = false;
  80. expect( button ).to.have.property( 'isOn', false );
  81. command.value = true;
  82. expect( button ).to.have.property( 'isOn', true );
  83. } );
  84. it( 'has isEnabled bound to command\'s isEnabled', () => {
  85. command.isEnabled = true;
  86. expect( button ).to.have.property( 'isEnabled', true );
  87. command.isEnabled = false;
  88. expect( button ).to.have.property( 'isEnabled', false );
  89. } );
  90. it( 'executes command when it\'s executed', () => {
  91. const spy = sinon.stub( editor, 'execute' );
  92. button.fire( 'execute' );
  93. expect( spy.calledOnce ).to.be.true;
  94. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignRight' );
  95. } );
  96. } );
  97. describe( 'alignCenter button', () => {
  98. beforeEach( () => {
  99. command = editor.commands.get( 'alignCenter' );
  100. button = editor.ui.componentFactory.create( 'alignCenter' );
  101. } );
  102. it( 'has the base properties', () => {
  103. expect( button ).to.have.property( 'label', 'Align center' );
  104. expect( button ).to.have.property( 'icon' );
  105. expect( button ).to.have.property( 'tooltip', true );
  106. } );
  107. it( 'has isOn bound to command\'s value', () => {
  108. command.value = false;
  109. expect( button ).to.have.property( 'isOn', false );
  110. command.value = true;
  111. expect( button ).to.have.property( 'isOn', true );
  112. } );
  113. it( 'has isEnabled bound to command\'s isEnabled', () => {
  114. command.isEnabled = true;
  115. expect( button ).to.have.property( 'isEnabled', true );
  116. command.isEnabled = false;
  117. expect( button ).to.have.property( 'isEnabled', false );
  118. } );
  119. it( 'executes command when it\'s executed', () => {
  120. const spy = sinon.stub( editor, 'execute' );
  121. button.fire( 'execute' );
  122. expect( spy.calledOnce ).to.be.true;
  123. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignCenter' );
  124. } );
  125. } );
  126. describe( 'alignJustify button', () => {
  127. beforeEach( () => {
  128. command = editor.commands.get( 'alignJustify' );
  129. button = editor.ui.componentFactory.create( 'alignJustify' );
  130. } );
  131. it( 'has the base properties', () => {
  132. expect( button ).to.have.property( 'label', 'Justify' );
  133. expect( button ).to.have.property( 'icon' );
  134. expect( button ).to.have.property( 'tooltip', true );
  135. } );
  136. it( 'has isOn bound to command\'s value', () => {
  137. command.value = false;
  138. expect( button ).to.have.property( 'isOn', false );
  139. command.value = true;
  140. expect( button ).to.have.property( 'isOn', true );
  141. } );
  142. it( 'has isEnabled bound to command\'s isEnabled', () => {
  143. command.isEnabled = true;
  144. expect( button ).to.have.property( 'isEnabled', true );
  145. command.isEnabled = false;
  146. expect( button ).to.have.property( 'isEnabled', false );
  147. } );
  148. it( 'executes command when it\'s executed', () => {
  149. const spy = sinon.stub( editor, 'execute' );
  150. button.fire( 'execute' );
  151. expect( spy.calledOnce ).to.be.true;
  152. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'alignJustify' );
  153. } );
  154. } );
  155. describe( 'alignmentDropdown', () => {
  156. let dropdown;
  157. beforeEach( () => {
  158. command = editor.commands.get( 'alignLeft' );
  159. dropdown = editor.ui.componentFactory.create( 'alignmentDropdown' );
  160. } );
  161. it( 'button has the base properties', () => {
  162. const button = dropdown.buttonView;
  163. expect( button ).to.have.property( 'label', 'Text alignment' );
  164. expect( button ).to.have.property( 'icon' );
  165. expect( button ).to.have.property( 'tooltip', true );
  166. expect( button ).to.have.property( 'withText', false );
  167. } );
  168. it( '#toolbarView has the base properties', () => {
  169. const toolbarView = dropdown.toolbarView;
  170. expect( toolbarView ).to.have.property( 'isVertical', true );
  171. } );
  172. it( 'should hold defined buttons', () => {
  173. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  174. expect( items ).to.have.length( 4 );
  175. expect( items.includes( 'Align left' ) ).to.be.true;
  176. expect( items.includes( 'Align right' ) ).to.be.true;
  177. expect( items.includes( 'Align center' ) ).to.be.true;
  178. expect( items.includes( 'Justify' ) ).to.be.true;
  179. } );
  180. describe( 'config', () => {
  181. beforeEach( () => {
  182. element = document.createElement( 'div' );
  183. document.body.appendChild( element );
  184. return ClassicTestEditor
  185. .create( element, {
  186. plugins: [ AlignmentEditing, AlignmentUI ],
  187. alignment: { options: [ 'center', 'justify' ] }
  188. } )
  189. .then( newEditor => {
  190. editor = newEditor;
  191. dropdown = editor.ui.componentFactory.create( 'alignmentDropdown' );
  192. command = editor.commands.get( 'alignCenter' );
  193. button = editor.ui.componentFactory.create( 'alignCenter' );
  194. } );
  195. } );
  196. it( 'should hold only defined buttons', () => {
  197. const items = [ ...dropdown.toolbarView.items ].map( item => item.label );
  198. expect( items ).to.have.length( 2 );
  199. expect( items.includes( 'Align center' ) ).to.be.true;
  200. expect( items.includes( 'Justify' ) ).to.be.true;
  201. } );
  202. it( 'should have default icon set', () => {
  203. expect( dropdown.buttonView.icon ).to.equal( alignLeftIcon );
  204. } );
  205. it( 'should change icon to active alignment', () => {
  206. command.value = true;
  207. expect( dropdown.buttonView.icon ).to.equal( button.icon );
  208. } );
  209. } );
  210. } );
  211. } );