tablewidthcommand.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 TableWidthCommand from '../../../src/tableproperties/commands/tablewidthcommand';
  11. describe( 'table properties', () => {
  12. describe( 'commands', () => {
  13. describe( 'TableWidthCommand', () => {
  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 TableWidthCommand( 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 width property', () => {
  50. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  51. expect( command.value ).to.be.undefined;
  52. } );
  53. it( 'should be set if selected table has width property', () => {
  54. setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
  55. expect( command.value ).to.equal( '100px' );
  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' ] ], { width: '100px' } ) );
  65. expect( command.value ).to.equal( '100px' );
  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: '25px', batch } );
  75. sinon.assert.calledWith( spy, batch );
  76. } );
  77. it( 'should add default unit for numeric values (number passed)', () => {
  78. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  79. command.execute( { value: 25 } );
  80. assertTableStyle( editor, null, 'width:25px;' );
  81. } );
  82. it( 'should add default unit for numeric values (string passed)', () => {
  83. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  84. command.execute( { value: 25 } );
  85. assertTableStyle( editor, null, 'width:25px;' );
  86. } );
  87. it( 'should not add default unit for numeric values with unit', () => {
  88. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  89. command.execute( { value: '25pt' } );
  90. assertTableStyle( editor, null, 'width:25pt;' );
  91. } );
  92. it( 'should add default unit to floats (number passed)', () => {
  93. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  94. command.execute( { value: 25.1 } );
  95. assertTableStyle( editor, null, 'width:25.1px;' );
  96. } );
  97. it( 'should add default unit to floats (string passed)', () => {
  98. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  99. command.execute( { value: '0.1' } );
  100. assertTableStyle( editor, null, 'width:0.1px;' );
  101. } );
  102. it( 'should pass invalid values', () => {
  103. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  104. command.execute( { value: 'bar' } );
  105. assertTableStyle( editor, null, 'width:bar;' );
  106. } );
  107. it( 'should pass invalid value (string passed, CSS float without leading 0)', () => {
  108. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  109. command.execute( { value: '.2' } );
  110. assertTableStyle( editor, null, 'width:.2;' );
  111. } );
  112. describe( 'collapsed selection', () => {
  113. it( 'should set selected table width to a passed value', () => {
  114. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  115. command.execute( { value: '25px' } );
  116. assertTableStyle( editor, null, 'width:25px;' );
  117. } );
  118. it( 'should change selected table width to a passed value', () => {
  119. setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
  120. command.execute( { value: '25px' } );
  121. assertTableStyle( editor, null, 'width:25px;' );
  122. } );
  123. it( 'should remove width from a selected table if no value is passed', () => {
  124. setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
  125. command.execute();
  126. assertTableStyle( editor, '' );
  127. } );
  128. } );
  129. describe( 'non-collapsed selection', () => {
  130. it( 'should set selected table width to a passed value', () => {
  131. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  132. command.execute( { value: '25px' } );
  133. assertTableStyle( editor, null, 'width:25px;' );
  134. } );
  135. it( 'should change selected table width to a passed value', () => {
  136. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  137. command.execute( { value: '25px' } );
  138. assertTableStyle( editor, null, 'width:25px;' );
  139. } );
  140. it( 'should remove width from a selected table if no value is passed', () => {
  141. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  142. command.execute();
  143. assertTableStyle( editor, '' );
  144. } );
  145. } );
  146. } );
  147. } );
  148. } );
  149. } );