8
0

tableui.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  8. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import TableEditing from '../src/tableediting';
  11. import TableUI from '../src/tableui';
  12. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  13. testUtils.createSinonSandbox();
  14. describe( 'TableUI', () => {
  15. let editor, element;
  16. before( () => {
  17. addTranslations( 'en', {} );
  18. addTranslations( 'pl', {} );
  19. } );
  20. after( () => {
  21. clearTranslations();
  22. } );
  23. beforeEach( () => {
  24. element = document.createElement( 'div' );
  25. document.body.appendChild( element );
  26. return ClassicTestEditor
  27. .create( element, {
  28. plugins: [ TableEditing, TableUI ]
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. } );
  33. } );
  34. afterEach( () => {
  35. element.remove();
  36. return editor.destroy();
  37. } );
  38. describe( 'insertTable button', () => {
  39. let insertTable;
  40. beforeEach( () => {
  41. insertTable = editor.ui.componentFactory.create( 'insertTable' );
  42. } );
  43. it( 'should register insertTable buton', () => {
  44. expect( insertTable ).to.be.instanceOf( ButtonView );
  45. expect( insertTable.isOn ).to.be.false;
  46. expect( insertTable.label ).to.equal( 'Insert table' );
  47. expect( insertTable.icon ).to.match( /<svg / );
  48. } );
  49. it( 'should bind to insertTable command', () => {
  50. const command = editor.commands.get( 'insertTable' );
  51. command.isEnabled = true;
  52. expect( insertTable.isOn ).to.be.false;
  53. expect( insertTable.isEnabled ).to.be.true;
  54. command.isEnabled = false;
  55. expect( insertTable.isEnabled ).to.be.false;
  56. } );
  57. it( 'should execute insertTable command on button execute event', () => {
  58. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  59. insertTable.fire( 'execute' );
  60. sinon.assert.calledOnce( executeSpy );
  61. sinon.assert.calledWithExactly( executeSpy, 'insertTable' );
  62. } );
  63. } );
  64. describe( 'tableRow dropdown', () => {
  65. let dropdown;
  66. beforeEach( () => {
  67. dropdown = editor.ui.componentFactory.create( 'tableRow' );
  68. } );
  69. it( 'have button with proper properties set', () => {
  70. expect( dropdown ).to.be.instanceOf( DropdownView );
  71. const button = dropdown.buttonView;
  72. expect( button.isOn ).to.be.false;
  73. expect( button.tooltip ).to.be.true;
  74. expect( button.label ).to.equal( 'Row' );
  75. expect( button.icon ).to.match( /<svg / );
  76. } );
  77. it( 'should have proper items in panel', () => {
  78. const listView = dropdown.listView;
  79. const labels = listView.items.map( ( { label } ) => label );
  80. expect( labels ).to.deep.equal( [ 'Insert row below', 'Insert row above', 'Delete row' ] );
  81. } );
  82. it( 'should bind items in panel to proper commands', () => {
  83. const items = dropdown.listView.items;
  84. const insertRowBelowCommand = editor.commands.get( 'insertRowBelow' );
  85. const insertRowAboveCommand = editor.commands.get( 'insertRowAbove' );
  86. const removeRowCommand = editor.commands.get( 'removeRow' );
  87. insertRowBelowCommand.isEnabled = true;
  88. insertRowAboveCommand.isEnabled = true;
  89. removeRowCommand.isEnabled = true;
  90. expect( items.get( 0 ).isEnabled ).to.be.true;
  91. expect( items.get( 1 ).isEnabled ).to.be.true;
  92. expect( items.get( 2 ).isEnabled ).to.be.true;
  93. expect( dropdown.buttonView.isEnabled ).to.be.true;
  94. insertRowBelowCommand.isEnabled = false;
  95. expect( items.get( 0 ).isEnabled ).to.be.false;
  96. expect( dropdown.buttonView.isEnabled ).to.be.true;
  97. insertRowAboveCommand.isEnabled = false;
  98. expect( items.get( 1 ).isEnabled ).to.be.false;
  99. expect( dropdown.buttonView.isEnabled ).to.be.true;
  100. removeRowCommand.isEnabled = false;
  101. expect( items.get( 2 ).isEnabled ).to.be.false;
  102. expect( dropdown.buttonView.isEnabled ).to.be.false;
  103. } );
  104. } );
  105. describe( 'tableColumn button', () => {
  106. let dropdown;
  107. beforeEach( () => {
  108. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  109. } );
  110. it( 'have button with proper properties set', () => {
  111. expect( dropdown ).to.be.instanceOf( DropdownView );
  112. const button = dropdown.buttonView;
  113. expect( button.isOn ).to.be.false;
  114. expect( button.tooltip ).to.be.true;
  115. expect( button.label ).to.equal( 'Column' );
  116. expect( button.icon ).to.match( /<svg / );
  117. } );
  118. it( 'should have proper items in panel', () => {
  119. const listView = dropdown.listView;
  120. const labels = listView.items.map( ( { label } ) => label );
  121. expect( labels ).to.deep.equal( [ 'Insert column before', 'Insert column after', 'Delete column' ] );
  122. } );
  123. it( 'should bind items in panel to proper commands', () => {
  124. const items = dropdown.listView.items;
  125. const insertColumnBeforeCommand = editor.commands.get( 'insertColumnBefore' );
  126. const insertColumnAfterCommand = editor.commands.get( 'insertColumnAfter' );
  127. const removeColumnCommand = editor.commands.get( 'removeColumn' );
  128. insertColumnBeforeCommand.isEnabled = true;
  129. insertColumnAfterCommand.isEnabled = true;
  130. removeColumnCommand.isEnabled = true;
  131. expect( items.get( 0 ).isEnabled ).to.be.true;
  132. expect( items.get( 1 ).isEnabled ).to.be.true;
  133. expect( items.get( 2 ).isEnabled ).to.be.true;
  134. expect( dropdown.buttonView.isEnabled ).to.be.true;
  135. insertColumnBeforeCommand.isEnabled = false;
  136. expect( items.get( 0 ).isEnabled ).to.be.false;
  137. expect( dropdown.buttonView.isEnabled ).to.be.true;
  138. insertColumnAfterCommand.isEnabled = false;
  139. expect( items.get( 1 ).isEnabled ).to.be.false;
  140. expect( dropdown.buttonView.isEnabled ).to.be.true;
  141. removeColumnCommand.isEnabled = false;
  142. expect( items.get( 2 ).isEnabled ).to.be.false;
  143. expect( dropdown.buttonView.isEnabled ).to.be.false;
  144. } );
  145. } );
  146. describe( 'mergeCell button', () => {
  147. let dropdown;
  148. beforeEach( () => {
  149. dropdown = editor.ui.componentFactory.create( 'mergeCell' );
  150. } );
  151. it( 'have button with proper properties set', () => {
  152. expect( dropdown ).to.be.instanceOf( DropdownView );
  153. const button = dropdown.buttonView;
  154. expect( button.isOn ).to.be.false;
  155. expect( button.tooltip ).to.be.true;
  156. expect( button.label ).to.equal( 'Merge cell' );
  157. expect( button.icon ).to.match( /<svg / );
  158. } );
  159. it( 'should have proper items in panel', () => {
  160. const listView = dropdown.listView;
  161. const labels = listView.items.map( ( { label } ) => label );
  162. expect( labels ).to.deep.equal( [ 'Merge cell up', 'Merge cell right', 'Merge cell down', 'Merge cell left' ] );
  163. } );
  164. it( 'should bind items in panel to proper commands', () => {
  165. const items = dropdown.listView.items;
  166. const mergeCellUpCommand = editor.commands.get( 'mergeCellUp' );
  167. const mergeCellRightCommand = editor.commands.get( 'mergeCellRight' );
  168. const mergeCellDownCommand = editor.commands.get( 'mergeCellDown' );
  169. const mergeCellLeftCommand = editor.commands.get( 'mergeCellLeft' );
  170. mergeCellUpCommand.isEnabled = true;
  171. mergeCellRightCommand.isEnabled = true;
  172. mergeCellDownCommand.isEnabled = true;
  173. mergeCellLeftCommand.isEnabled = true;
  174. expect( items.get( 0 ).isEnabled ).to.be.true;
  175. expect( items.get( 1 ).isEnabled ).to.be.true;
  176. expect( items.get( 2 ).isEnabled ).to.be.true;
  177. expect( items.get( 3 ).isEnabled ).to.be.true;
  178. expect( dropdown.buttonView.isEnabled ).to.be.true;
  179. mergeCellUpCommand.isEnabled = false;
  180. expect( items.get( 0 ).isEnabled ).to.be.false;
  181. expect( dropdown.buttonView.isEnabled ).to.be.true;
  182. mergeCellRightCommand.isEnabled = false;
  183. expect( items.get( 1 ).isEnabled ).to.be.false;
  184. expect( dropdown.buttonView.isEnabled ).to.be.true;
  185. mergeCellDownCommand.isEnabled = false;
  186. expect( items.get( 2 ).isEnabled ).to.be.false;
  187. mergeCellLeftCommand.isEnabled = false;
  188. expect( items.get( 3 ).isEnabled ).to.be.false;
  189. expect( dropdown.buttonView.isEnabled ).to.be.false;
  190. } );
  191. } );
  192. } );