inserttablecommand.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. import TableEditing from '../../src/tableediting';
  10. import { modelTable } from '../_utils/utils';
  11. import InsertTableCommand from '../../src/commands/inserttablecommand';
  12. describe( 'InsertTableCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ Paragraph, TableEditing ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. command = new InsertTableCommand( editor );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. describe( 'when selection is collapsed', () => {
  30. it( 'should be true if in paragraph', () => {
  31. setData( model, '<paragraph>foo[]</paragraph>' );
  32. expect( command.isEnabled ).to.be.true;
  33. } );
  34. it( 'should be false if in table', () => {
  35. setData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  36. expect( command.isEnabled ).to.be.false;
  37. } );
  38. } );
  39. } );
  40. describe( 'execute()', () => {
  41. it( 'should create a single batch', () => {
  42. setData( model, '<paragraph>foo[]</paragraph>' );
  43. const spy = sinon.spy();
  44. model.document.on( 'change', spy );
  45. command.execute( { rows: 3, columns: 4 } );
  46. sinon.assert.calledOnce( spy );
  47. } );
  48. describe( 'collapsed selection', () => {
  49. it( 'should insert table in empty root', () => {
  50. setData( model, '[]' );
  51. command.execute();
  52. assertEqualMarkup( getData( model ), modelTable( [
  53. [ '[]', '' ],
  54. [ '', '' ]
  55. ] ) );
  56. } );
  57. it( 'should insert table with two rows and two columns after non-empty paragraph if selection is at the end', () => {
  58. setData( model, '<paragraph>foo[]</paragraph>' );
  59. command.execute();
  60. assertEqualMarkup( getData( model ),
  61. '<paragraph>foo</paragraph>' +
  62. modelTable( [
  63. [ '[]', '' ],
  64. [ '', '' ]
  65. ] )
  66. );
  67. } );
  68. it( 'should insert table with given rows and columns after non-empty paragraph', () => {
  69. setData( model, '<paragraph>foo[]</paragraph>' );
  70. command.execute( { rows: 3, columns: 4 } );
  71. assertEqualMarkup( getData( model ),
  72. '<paragraph>foo</paragraph>' +
  73. modelTable( [
  74. [ '[]', '', '', '' ],
  75. [ '', '', '', '' ],
  76. [ '', '', '', '' ]
  77. ] )
  78. );
  79. } );
  80. it( 'should insert table with given heading rows and heading columns after non-empty paragraph', () => {
  81. setData( model, '<paragraph>foo[]</paragraph>' );
  82. command.execute( { rows: 3, columns: 4, headingRows: 1, headingColumns: 2 } );
  83. assertEqualMarkup( getData( model ),
  84. '<paragraph>foo</paragraph>' +
  85. modelTable( [
  86. [ '[]', '', '', '' ],
  87. [ '', '', '', '' ],
  88. [ '', '', '', '' ]
  89. ], { headingRows: 1, headingColumns: 2 } )
  90. );
  91. } );
  92. it( 'should insert table before after non-empty paragraph if selection is inside', () => {
  93. setData( model, '<paragraph>f[]oo</paragraph>' );
  94. command.execute();
  95. assertEqualMarkup( getData( model ),
  96. modelTable( [
  97. [ '[]', '' ],
  98. [ '', '' ]
  99. ] ) +
  100. '<paragraph>foo</paragraph>'
  101. );
  102. } );
  103. it( 'should replace empty paragraph with table', () => {
  104. setData( model, '<paragraph>[]</paragraph>' );
  105. command.execute( { rows: 3, columns: 4 } );
  106. assertEqualMarkup( getData( model ),
  107. modelTable( [
  108. [ '[]', '', '', '' ],
  109. [ '', '', '', '' ],
  110. [ '', '', '', '' ]
  111. ] )
  112. );
  113. } );
  114. } );
  115. } );
  116. } );