inserttablecommand.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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, formatTable, formattedModelTable } from '../_utils/utils';
  10. describe( 'InsertTableCommand', () => {
  11. let editor, model, command;
  12. beforeEach( () => {
  13. return ModelTestEditor
  14. .create( {
  15. plugins: [ TableUtils ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. command = new InsertTableCommand( editor );
  21. defaultSchema( model.schema );
  22. defaultConversion( editor.conversion );
  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. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  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. expect( formatTable( getData( model ) ) ).to.equal(
  61. '<paragraph>foo</paragraph>' +
  62. formattedModelTable( [
  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. expect( formatTable( getData( model ) ) ).to.equal(
  72. '<paragraph>foo</paragraph>' +
  73. formattedModelTable( [
  74. [ '[]', '', '', '' ],
  75. [ '', '', '', '' ],
  76. [ '', '', '', '' ]
  77. ] )
  78. );
  79. } );
  80. it( 'should insert table before after non-empty paragraph if selection is inside', () => {
  81. setData( model, '<paragraph>f[]oo</paragraph>' );
  82. command.execute();
  83. expect( formatTable( getData( model ) ) ).to.equal(
  84. formattedModelTable( [
  85. [ '[]', '' ],
  86. [ '', '' ]
  87. ] ) +
  88. '<paragraph>foo</paragraph>'
  89. );
  90. } );
  91. it( 'should replace empty paragraph with table', () => {
  92. setData( model, '<paragraph>[]</paragraph>' );
  93. command.execute( { rows: 3, columns: 4 } );
  94. expect( formatTable( getData( model ) ) ).to.equal(
  95. formattedModelTable( [
  96. [ '[]', '', '', '' ],
  97. [ '', '', '', '' ],
  98. [ '', '', '', '' ]
  99. ] )
  100. );
  101. } );
  102. } );
  103. } );
  104. } );