setheadercolumncommand.js 3.9 KB

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