tablealignmentcommand.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { assertTableStyle, modelTable } from '../../_utils/utils';
  9. import TablePropertiesEditing from '../../../src/tableproperties/tablepropertiesediting';
  10. import TableHorizontalAlignmentCommand from '../../../src/tableproperties/commands/tablealignmentcommand';
  11. describe( 'table properties', () => {
  12. describe( 'commands', () => {
  13. describe( 'TableHorizontalAlignmentCommand', () => {
  14. let editor, model, command;
  15. beforeEach( async () => {
  16. editor = await ModelTestEditor.create( {
  17. plugins: [ Paragraph, TablePropertiesEditing ]
  18. } );
  19. model = editor.model;
  20. command = new TableHorizontalAlignmentCommand( editor );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. describe( 'isEnabled', () => {
  26. describe( 'collapsed selection', () => {
  27. it( 'should be false if selection does not have table', () => {
  28. setData( model, '<paragraph>foo[]</paragraph>' );
  29. expect( command.isEnabled ).to.be.false;
  30. } );
  31. it( 'should be true is selection has table', () => {
  32. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  33. expect( command.isEnabled ).to.be.true;
  34. } );
  35. } );
  36. describe( 'non-collapsed selection', () => {
  37. it( 'should be false if selection does not have table', () => {
  38. setData( model, '<paragraph>f[oo]</paragraph>' );
  39. expect( command.isEnabled ).to.be.false;
  40. } );
  41. it( 'should be true is selection has table', () => {
  42. setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
  43. expect( command.isEnabled ).to.be.true;
  44. } );
  45. } );
  46. } );
  47. describe( 'value', () => {
  48. describe( 'collapsed selection', () => {
  49. it( 'should be undefined if selected table has no alignment property', () => {
  50. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  51. expect( command.value ).to.be.undefined;
  52. } );
  53. it( 'should be set if selected table has alignment property', () => {
  54. setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
  55. expect( command.value ).to.equal( 'center' );
  56. } );
  57. } );
  58. describe( 'non-collapsed selection', () => {
  59. it( 'should be false if selection does not have table', () => {
  60. setData( model, '<paragraph>f[oo]</paragraph>' );
  61. expect( command.value ).to.be.undefined;
  62. } );
  63. it( 'should be true is selection has table', () => {
  64. setData( model, modelTable( [ [ 'f[o]o' ] ], { alignment: 'center' } ) );
  65. expect( command.value ).to.equal( 'center' );
  66. } );
  67. } );
  68. } );
  69. describe( 'execute()', () => {
  70. it( 'should use provided batch', () => {
  71. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  72. const batch = model.createBatch();
  73. const spy = sinon.spy( model, 'enqueueChange' );
  74. command.execute( { value: 'right', batch } );
  75. sinon.assert.calledWith( spy, batch );
  76. } );
  77. describe( 'collapsed selection', () => {
  78. it( 'should set selected table alignment to a passed value', () => {
  79. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  80. command.execute( { value: 'right' } );
  81. assertTableStyle( editor, null, 'float:right;' );
  82. } );
  83. it( 'should change selected table alignment to a passed value', () => {
  84. setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
  85. command.execute( { value: 'right' } );
  86. assertTableStyle( editor, null, 'float:right;' );
  87. } );
  88. it( 'should remove alignment from a selected table if no value is passed', () => {
  89. setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
  90. command.execute();
  91. assertTableStyle( editor, '' );
  92. } );
  93. } );
  94. describe( 'non-collapsed selection', () => {
  95. it( 'should set selected table alignment to a passed value', () => {
  96. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  97. command.execute( { value: 'right' } );
  98. assertTableStyle( editor, null, 'float:right;' );
  99. } );
  100. it( 'should change selected table alignment to a passed value', () => {
  101. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  102. command.execute( { value: 'right' } );
  103. assertTableStyle( editor, null, 'float:right;' );
  104. } );
  105. it( 'should remove alignment from a selected table if no value is passed', () => {
  106. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  107. command.execute();
  108. assertTableStyle( editor, '' );
  109. } );
  110. } );
  111. } );
  112. } );
  113. } );
  114. } );