removerowcommand.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import RemoveRowCommand from '../../src/commands/removerowcommand';
  8. import { defaultConversion, defaultSchema, formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  9. describe( 'RemoveRowCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. command = new RemoveRowCommand( editor );
  17. defaultSchema( model.schema );
  18. defaultConversion( editor.conversion );
  19. } );
  20. } );
  21. afterEach( () => {
  22. return editor.destroy();
  23. } );
  24. describe( 'isEnabled', () => {
  25. it( 'should be true if selection is inside table cell', () => {
  26. setData( model, modelTable( [
  27. [ '00[]', '01' ],
  28. [ '10', '11' ]
  29. ] ) );
  30. expect( command.isEnabled ).to.be.true;
  31. } );
  32. it( 'should be false if selection is inside table with one row only', () => {
  33. setData( model, modelTable( [
  34. [ '00[]', '01' ]
  35. ] ) );
  36. expect( command.isEnabled ).to.be.false;
  37. } );
  38. it( 'should be false if selection is outside a table', () => {
  39. setData( model, '<paragraph>11[]</paragraph>' );
  40. expect( command.isEnabled ).to.be.false;
  41. } );
  42. } );
  43. describe( 'execute()', () => {
  44. it( 'should remove a given row', () => {
  45. setData( model, modelTable( [
  46. [ '00', '01' ],
  47. [ '[]10', '11' ],
  48. [ '20', '21' ]
  49. ] ) );
  50. command.execute();
  51. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  52. [ '00', '<paragraph>01[]</paragraph>' ],
  53. [ '20', '21' ]
  54. ] ) );
  55. } );
  56. it( 'should remove a given row from a table start', () => {
  57. setData( model, modelTable( [
  58. [ '[]00', '01' ],
  59. [ '10', '11' ],
  60. [ '20', '21' ]
  61. ] ) );
  62. command.execute();
  63. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  64. [ '<paragraph>[]10</paragraph>', '11' ],
  65. [ '20', '21' ]
  66. ] ) );
  67. } );
  68. it( 'should change heading rows if removing a heading row', () => {
  69. setData( model, modelTable( [
  70. [ '00', '01' ],
  71. [ '[]10', '11' ],
  72. [ '20', '21' ]
  73. ], { headingRows: 2 } ) );
  74. command.execute();
  75. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  76. [ '00', '<paragraph>01[]</paragraph>' ],
  77. [ '20', '21' ]
  78. ], { headingRows: 1 } ) );
  79. } );
  80. it( 'should decrease rowspan of table cells from previous rows', () => {
  81. setData( model, modelTable( [
  82. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  83. [ { rowspan: 2, contents: '13' }, '14' ],
  84. [ '22[]', '23', '24' ],
  85. [ '30', '31', '32', '33', '34' ]
  86. ] ) );
  87. command.execute();
  88. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  89. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  90. [ '13', '<paragraph>14[]</paragraph>' ],
  91. [ '30', '31', '32', '33', '34' ]
  92. ] ) );
  93. } );
  94. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  95. setData( model, modelTable( [
  96. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  97. [ '12' ],
  98. [ '22' ],
  99. [ '30', '31', '32' ]
  100. ] ) );
  101. command.execute();
  102. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  103. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  104. [ '22' ],
  105. [ '30', '31', '32' ]
  106. ] ) );
  107. } );
  108. } );
  109. } );