setheadercolumncommand.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  8. import { defaultConversion, defaultSchema, formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  9. import TableUtils from '../../src/tableutils';
  10. describe( 'HeaderColumnCommand', () => {
  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 SetHeaderColumnCommand( editor );
  21. defaultSchema( model.schema );
  22. defaultConversion( editor.conversion );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. it( 'should be false if selection is not in a table', () => {
  30. setData( model, '<paragraph>foo[]</paragraph>' );
  31. expect( command.isEnabled ).to.be.false;
  32. } );
  33. it( 'should be true if selection is in table', () => {
  34. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  35. expect( command.isEnabled ).to.be.true;
  36. } );
  37. } );
  38. describe( 'value', () => {
  39. it( 'should be false if selection is not in a heading column', () => {
  40. setData( model, modelTable( [
  41. [ '01', '02' ],
  42. [ '11', '12[]' ]
  43. ], { headingColumns: 1 } ) );
  44. expect( command.value ).to.be.false;
  45. } );
  46. it( 'should be true if selection is in a heading column', () => {
  47. setData( model, modelTable( [
  48. [ '01[]', '02' ],
  49. [ '11', '12' ]
  50. ], { headingColumns: 1 } ) );
  51. expect( command.value ).to.be.true;
  52. } );
  53. it( 'should be false if selection is in a heading row', () => {
  54. setData( model, modelTable( [
  55. [ '01', '02[]' ],
  56. [ '11', '12' ]
  57. ], { headingRows: 1, headingColumns: 1 } ) );
  58. expect( command.value ).to.be.false;
  59. } );
  60. } );
  61. describe( 'execute()', () => {
  62. it( 'should set heading columns attribute that cover column in which is selection', () => {
  63. setData( model, modelTable( [
  64. [ '00', '01' ],
  65. [ '[]10', '11' ],
  66. [ '20', '21' ]
  67. ] ) );
  68. command.execute();
  69. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  70. [ '00', '01' ],
  71. [ '[]10', '11' ],
  72. [ '20', '21' ]
  73. ], { headingColumns: 1 } ) );
  74. } );
  75. it(
  76. 'should set heading columns attribute if currently selected column is a heading so the heading section is before this column',
  77. () => {
  78. setData( model, modelTable( [
  79. [ '00', '01' ],
  80. [ '[]10', '11' ],
  81. [ '20', '21' ]
  82. ], { headingColumns: 2 } ) );
  83. command.execute();
  84. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  85. [ '00', '01' ],
  86. [ '[]10', '11' ],
  87. [ '20', '21' ]
  88. ], { headingColumns: 1 } ) );
  89. }
  90. );
  91. it( 'should toggle of selected column', () => {
  92. setData( model, modelTable( [
  93. [ '00', '01' ],
  94. [ '10', '11[]' ],
  95. [ '20', '21' ]
  96. ], { headingColumns: 2 } ) );
  97. command.execute();
  98. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  99. [ '00', '01' ],
  100. [ '10', '11[]' ],
  101. [ '20', '21' ]
  102. ], { headingColumns: 1 } ) );
  103. command.execute();
  104. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  105. [ '00', '01' ],
  106. [ '10', '11[]' ],
  107. [ '20', '21' ]
  108. ], { headingColumns: 2 } ) );
  109. } );
  110. } );
  111. } );