setheadercolumncommand.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  8. import { defaultConversion, defaultSchema, formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  9. import TableUtils from '../../src/tableutils';
  10. describe( 'SetHeaderColumnCommand', () => {
  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><paragraph>foo[]</paragraph></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[]', '02', '03' ]
  65. ] ) );
  66. command.execute();
  67. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  68. [ '00', '01[]', '02', '03' ]
  69. ], { headingColumns: 2 } ) );
  70. } );
  71. it( 'should set heading columns attribute below current selection column', () => {
  72. setData( model, modelTable( [
  73. [ '00', '01[]', '02', '03' ]
  74. ], { headingColumns: 3 } ) );
  75. command.execute();
  76. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  77. [ '00', '01[]', '02', '03' ]
  78. ], { headingColumns: 1 } ) );
  79. } );
  80. it( 'should toggle of selected column', () => {
  81. setData( model, modelTable( [
  82. [ '00', '01[]', '02', '03' ]
  83. ], { headingColumns: 2 } ) );
  84. command.execute();
  85. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  86. [ '00', '01[]', '02', '03' ]
  87. ], { headingColumns: 1 } ) );
  88. command.execute();
  89. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  90. [ '00', '01[]', '02', '03' ]
  91. ], { headingColumns: 2 } ) );
  92. } );
  93. it( 'should respect forceValue parameter #1', () => {
  94. setData( model, modelTable( [
  95. [ '00', '01[]', '02', '03' ]
  96. ], { headingColumns: 3 } ) );
  97. command.execute( { forceValue: true } );
  98. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  99. [ '00', '01[]', '02', '03' ]
  100. ], { headingColumns: 3 } ) );
  101. } );
  102. it( 'should respect forceValue parameter #2', () => {
  103. setData( model, modelTable( [
  104. [ '00', '01[]', '02', '03' ]
  105. ], { headingColumns: 1 } ) );
  106. command.execute( { forceValue: false } );
  107. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  108. [ '00', '01[]', '02', '03' ]
  109. ], { headingColumns: 1 } ) );
  110. } );
  111. } );
  112. } );