8
0

inserttablecommand.js 3.4 KB

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