tableborderstylecommand.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, setTableWithObjectAttributes } from '../../_utils/utils';
  9. import TablePropertiesEditing from '../../../src/tableproperties/tablepropertiesediting';
  10. import TableBorderStyleCommand from '../../../src/tableproperties/commands/tableborderstylecommand';
  11. describe( 'table properties', () => {
  12. describe( 'commands', () => {
  13. describe( 'TableBorderStyleCommand', () => {
  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 TableBorderStyleCommand( 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 borderStyle property', () => {
  50. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  51. expect( command.value ).to.be.undefined;
  52. } );
  53. it( 'should be set if selected table has borderStyle property (single string)', () => {
  54. setData( model, modelTable( [ [ '[]foo' ] ], { borderStyle: 'ridge' } ) );
  55. expect( command.value ).to.equal( 'ridge' );
  56. } );
  57. it( 'should be set if selected table has borderStyle property object with same values', () => {
  58. setTableWithObjectAttributes( model, {
  59. borderStyle: {
  60. top: 'ridge',
  61. right: 'ridge',
  62. bottom: 'ridge',
  63. left: 'ridge'
  64. }
  65. }, '[]foo' );
  66. expect( command.value ).to.equal( 'ridge' );
  67. } );
  68. it( 'should be undefined if selected table has borderStyle property object with different values', () => {
  69. setTableWithObjectAttributes( model, {
  70. borderStyle: {
  71. top: 'ridge',
  72. right: 'dashed',
  73. bottom: 'ridge',
  74. left: 'ridge'
  75. }
  76. }, '[]foo' );
  77. expect( command.value ).to.be.undefined;
  78. } );
  79. } );
  80. describe( 'non-collapsed selection', () => {
  81. it( 'should be false if selection does not have table', () => {
  82. setData( model, '<paragraph>f[oo]</paragraph>' );
  83. expect( command.value ).to.be.undefined;
  84. } );
  85. it( 'should be true is selection has table', () => {
  86. setData( model, modelTable( [ [ 'f[o]o' ] ], { borderStyle: 'ridge' } ) );
  87. expect( command.value ).to.equal( 'ridge' );
  88. } );
  89. } );
  90. } );
  91. describe( 'execute()', () => {
  92. it( 'should use provided batch', () => {
  93. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  94. const batch = model.createBatch();
  95. const spy = sinon.spy( model, 'enqueueChange' );
  96. command.execute( { value: 'solid', batch } );
  97. sinon.assert.calledWith( spy, batch );
  98. } );
  99. describe( 'collapsed selection', () => {
  100. it( 'should set selected table borderStyle to a passed value', () => {
  101. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  102. command.execute( { value: 'solid' } );
  103. assertTableStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  104. } );
  105. it( 'should change selected table borderStyle to a passed value', () => {
  106. setData( model, modelTable( [ [ '[]foo' ] ], { borderStyle: 'ridge' } ) );
  107. command.execute( { value: 'solid' } );
  108. assertTableStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  109. } );
  110. it( 'should remove borderStyle from a selected table if no value is passed', () => {
  111. setData( model, modelTable( [ [ '[]foo' ] ], { borderStyle: 'ridge' } ) );
  112. command.execute();
  113. assertTableStyle( editor, '' );
  114. } );
  115. } );
  116. describe( 'non-collapsed selection', () => {
  117. it( 'should set selected table borderStyle to a passed value', () => {
  118. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  119. command.execute( { value: 'solid' } );
  120. assertTableStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  121. } );
  122. it( 'should change selected table borderStyle to a passed value', () => {
  123. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  124. command.execute( { value: 'solid' } );
  125. assertTableStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  126. } );
  127. it( 'should remove borderStyle from a selected table if no value is passed', () => {
  128. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  129. command.execute();
  130. assertTableStyle( editor, '' );
  131. } );
  132. } );
  133. } );
  134. } );
  135. } );
  136. } );