alignmentcommand.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AlignmentCommand from '../src/alignmentcommand';
  6. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import Command from '@ckeditor/ckeditor5-core/src/command';
  8. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  9. describe( 'AlignmentCommand', () => {
  10. let editor, doc, command, defaultAlignmentCommand;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. doc = newEditor.document;
  15. command = new AlignmentCommand( newEditor, 'center' );
  16. defaultAlignmentCommand = new AlignmentCommand( newEditor, 'left' );
  17. editor = newEditor;
  18. editor.commands.add( 'alignCenter', command );
  19. editor.commands.add( 'alignLeft', defaultAlignmentCommand );
  20. doc.schema.registerItem( 'paragraph', '$block' );
  21. doc.schema.allow( { name: '$block', inside: '$root', attributes: 'alignment' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. editor.destroy();
  26. } );
  27. it( 'is a command', () => {
  28. expect( AlignmentCommand.prototype ).to.be.instanceOf( Command );
  29. expect( command ).to.be.instanceOf( Command );
  30. } );
  31. describe( 'value', () => {
  32. it( 'is true when selection is in block with commend type alignment', () => {
  33. setModelData( doc, '<paragraph alignment="center">x[]x</paragraph>' );
  34. expect( command ).to.have.property( 'value', true );
  35. } );
  36. it( 'is false when selection is not block that has different alignment', () => {
  37. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  38. expect( command ).to.have.property( 'value', false );
  39. } );
  40. it( 'is true when selection is in block with default alignment', () => {
  41. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  42. expect( defaultAlignmentCommand ).to.have.property( 'value', true );
  43. } );
  44. it( 'is false when selection is not block that has different alignment', () => {
  45. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  46. expect( defaultAlignmentCommand ).to.have.property( 'value', false );
  47. } );
  48. } );
  49. describe( 'isEnabled', () => {
  50. it( 'is true when selection is in a block which can have added alignment', () => {
  51. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  52. expect( command ).to.have.property( 'isEnabled', true );
  53. } );
  54. } );
  55. describe( 'execute()', () => {
  56. describe( 'applying alignment', () => {
  57. it( 'adds alignment to block element', () => {
  58. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  59. editor.execute( 'alignCenter' );
  60. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="center">x[]x</paragraph>' );
  61. } );
  62. it( 'adds alignment to all selected blocks', () => {
  63. setModelData( doc, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  64. editor.execute( 'alignCenter' );
  65. expect( getModelData( doc ) ).to.equal(
  66. '<paragraph alignment="center">x[x</paragraph>' +
  67. '<paragraph alignment="center">xx</paragraph>' +
  68. '<paragraph alignment="center">x]x</paragraph>'
  69. );
  70. } );
  71. } );
  72. describe( 'applying default alignment', () => {
  73. it( 'removes alignment from block element', () => {
  74. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  75. editor.execute( 'alignLeft' );
  76. expect( getModelData( doc ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  77. } );
  78. it( 'removes alignment from all selected blocks', () => {
  79. setModelData( doc, '<paragraph alignment="center">x[x</paragraph>' +
  80. '<paragraph alignment="center">xx</paragraph>' +
  81. '<paragraph alignment="center">x]x</paragraph>' );
  82. editor.execute( 'alignLeft' );
  83. expect( getModelData( doc ) ).to.equal(
  84. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  85. );
  86. } );
  87. } );
  88. } );
  89. } );