tableui.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 dropdown', () => {
  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. it( 'should focus view after command execution', () => {
  105. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  106. dropdown.listView.items.get( 0 ).fire( 'execute' );
  107. sinon.assert.calledOnce( focusSpy );
  108. } );
  109. it( 'executes command when it\'s executed', () => {
  110. const spy = sinon.stub( editor, 'execute' );
  111. dropdown.listView.items.get( 0 ).fire( 'execute' );
  112. expect( spy.calledOnce ).to.be.true;
  113. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'insertRowBelow' );
  114. } );
  115. } );
  116. describe( 'tableColumn dropdown', () => {
  117. let dropdown;
  118. beforeEach( () => {
  119. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  120. } );
  121. it( 'have button with proper properties set', () => {
  122. expect( dropdown ).to.be.instanceOf( DropdownView );
  123. const button = dropdown.buttonView;
  124. expect( button.isOn ).to.be.false;
  125. expect( button.tooltip ).to.be.true;
  126. expect( button.label ).to.equal( 'Column' );
  127. expect( button.icon ).to.match( /<svg / );
  128. } );
  129. it( 'should have proper items in panel', () => {
  130. const listView = dropdown.listView;
  131. const labels = listView.items.map( ( { label } ) => label );
  132. expect( labels ).to.deep.equal( [ 'Insert column before', 'Insert column after', 'Delete column' ] );
  133. } );
  134. it( 'should bind items in panel to proper commands', () => {
  135. const items = dropdown.listView.items;
  136. const insertColumnBeforeCommand = editor.commands.get( 'insertColumnBefore' );
  137. const insertColumnAfterCommand = editor.commands.get( 'insertColumnAfter' );
  138. const removeColumnCommand = editor.commands.get( 'removeColumn' );
  139. insertColumnBeforeCommand.isEnabled = true;
  140. insertColumnAfterCommand.isEnabled = true;
  141. removeColumnCommand.isEnabled = true;
  142. expect( items.get( 0 ).isEnabled ).to.be.true;
  143. expect( items.get( 1 ).isEnabled ).to.be.true;
  144. expect( items.get( 2 ).isEnabled ).to.be.true;
  145. expect( dropdown.buttonView.isEnabled ).to.be.true;
  146. insertColumnBeforeCommand.isEnabled = false;
  147. expect( items.get( 0 ).isEnabled ).to.be.false;
  148. expect( dropdown.buttonView.isEnabled ).to.be.true;
  149. insertColumnAfterCommand.isEnabled = false;
  150. expect( items.get( 1 ).isEnabled ).to.be.false;
  151. expect( dropdown.buttonView.isEnabled ).to.be.true;
  152. removeColumnCommand.isEnabled = false;
  153. expect( items.get( 2 ).isEnabled ).to.be.false;
  154. expect( dropdown.buttonView.isEnabled ).to.be.false;
  155. } );
  156. it( 'should focus view after command execution', () => {
  157. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  158. dropdown.listView.items.get( 0 ).fire( 'execute' );
  159. sinon.assert.calledOnce( focusSpy );
  160. } );
  161. it( 'executes command when it\'s executed', () => {
  162. const spy = sinon.stub( editor, 'execute' );
  163. dropdown.listView.items.get( 0 ).fire( 'execute' );
  164. expect( spy.calledOnce ).to.be.true;
  165. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'insertColumnBefore' );
  166. } );
  167. } );
  168. describe( 'mergeCell dropdown', () => {
  169. let dropdown;
  170. beforeEach( () => {
  171. dropdown = editor.ui.componentFactory.create( 'mergeCell' );
  172. } );
  173. it( 'have button with proper properties set', () => {
  174. expect( dropdown ).to.be.instanceOf( DropdownView );
  175. const button = dropdown.buttonView;
  176. expect( button.isOn ).to.be.false;
  177. expect( button.tooltip ).to.be.true;
  178. expect( button.label ).to.equal( 'Merge cell' );
  179. expect( button.icon ).to.match( /<svg / );
  180. } );
  181. it( 'should have proper items in panel', () => {
  182. const listView = dropdown.listView;
  183. const labels = listView.items.map( ( { label } ) => label );
  184. expect( labels ).to.deep.equal( [ 'Merge cell up', 'Merge cell right', 'Merge cell down', 'Merge cell left' ] );
  185. } );
  186. it( 'should bind items in panel to proper commands', () => {
  187. const items = dropdown.listView.items;
  188. const mergeCellUpCommand = editor.commands.get( 'mergeCellUp' );
  189. const mergeCellRightCommand = editor.commands.get( 'mergeCellRight' );
  190. const mergeCellDownCommand = editor.commands.get( 'mergeCellDown' );
  191. const mergeCellLeftCommand = editor.commands.get( 'mergeCellLeft' );
  192. mergeCellUpCommand.isEnabled = true;
  193. mergeCellRightCommand.isEnabled = true;
  194. mergeCellDownCommand.isEnabled = true;
  195. mergeCellLeftCommand.isEnabled = true;
  196. expect( items.get( 0 ).isEnabled ).to.be.true;
  197. expect( items.get( 1 ).isEnabled ).to.be.true;
  198. expect( items.get( 2 ).isEnabled ).to.be.true;
  199. expect( items.get( 3 ).isEnabled ).to.be.true;
  200. expect( dropdown.buttonView.isEnabled ).to.be.true;
  201. mergeCellUpCommand.isEnabled = false;
  202. expect( items.get( 0 ).isEnabled ).to.be.false;
  203. expect( dropdown.buttonView.isEnabled ).to.be.true;
  204. mergeCellRightCommand.isEnabled = false;
  205. expect( items.get( 1 ).isEnabled ).to.be.false;
  206. expect( dropdown.buttonView.isEnabled ).to.be.true;
  207. mergeCellDownCommand.isEnabled = false;
  208. expect( items.get( 2 ).isEnabled ).to.be.false;
  209. mergeCellLeftCommand.isEnabled = false;
  210. expect( items.get( 3 ).isEnabled ).to.be.false;
  211. expect( dropdown.buttonView.isEnabled ).to.be.false;
  212. } );
  213. it( 'should focus view after command execution', () => {
  214. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  215. dropdown.listView.items.get( 0 ).fire( 'execute' );
  216. sinon.assert.calledOnce( focusSpy );
  217. } );
  218. it( 'executes command when it\'s executed', () => {
  219. const spy = sinon.stub( editor, 'execute' );
  220. dropdown.listView.items.get( 0 ).fire( 'execute' );
  221. expect( spy.calledOnce ).to.be.true;
  222. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeCellUp' );
  223. } );
  224. } );
  225. describe( 'splitCell dropdown', () => {
  226. let dropdown;
  227. beforeEach( () => {
  228. dropdown = editor.ui.componentFactory.create( 'splitCell' );
  229. } );
  230. it( 'have button with proper properties set', () => {
  231. expect( dropdown ).to.be.instanceOf( DropdownView );
  232. const button = dropdown.buttonView;
  233. expect( button.isOn ).to.be.false;
  234. expect( button.tooltip ).to.be.true;
  235. expect( button.label ).to.equal( 'Split cell' );
  236. expect( button.icon ).to.match( /<svg / );
  237. } );
  238. it( 'should have proper items in panel', () => {
  239. const listView = dropdown.listView;
  240. const labels = listView.items.map( ( { label } ) => label );
  241. expect( labels ).to.deep.equal( [ 'Split cell vertically', 'Split cell horizontally' ] );
  242. } );
  243. it( 'should bind items in panel to proper commands', () => {
  244. const items = dropdown.listView.items;
  245. const splitCellVerticallyCommand = editor.commands.get( 'splitCellVertically' );
  246. const splitCellHorizontallyCommand = editor.commands.get( 'splitCellHorizontally' );
  247. splitCellVerticallyCommand.isEnabled = true;
  248. splitCellHorizontallyCommand.isEnabled = true;
  249. expect( items.get( 0 ).isEnabled ).to.be.true;
  250. expect( items.get( 1 ).isEnabled ).to.be.true;
  251. expect( dropdown.buttonView.isEnabled ).to.be.true;
  252. splitCellVerticallyCommand.isEnabled = false;
  253. expect( items.get( 0 ).isEnabled ).to.be.false;
  254. expect( dropdown.buttonView.isEnabled ).to.be.true;
  255. splitCellHorizontallyCommand.isEnabled = false;
  256. expect( items.get( 1 ).isEnabled ).to.be.false;
  257. expect( dropdown.buttonView.isEnabled ).to.be.false;
  258. } );
  259. it( 'should focus view after command execution', () => {
  260. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  261. dropdown.listView.items.get( 0 ).fire( 'execute' );
  262. sinon.assert.calledOnce( focusSpy );
  263. } );
  264. it( 'executes command when it\'s executed', () => {
  265. const spy = sinon.stub( editor, 'execute' );
  266. dropdown.listView.items.get( 0 ).fire( 'execute' );
  267. expect( spy.calledOnce ).to.be.true;
  268. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'splitCellVertically' );
  269. } );
  270. } );
  271. } );