alignmentcommand.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 '../../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.registerItem( 'heading', '$block' );
  22. doc.schema.allow( { name: '$block', inside: '$root', attributes: 'alignment' } );
  23. } );
  24. } );
  25. afterEach( () => {
  26. editor.destroy();
  27. } );
  28. it( 'is a command', () => {
  29. expect( AlignmentCommand.prototype ).to.be.instanceOf( Command );
  30. expect( command ).to.be.instanceOf( Command );
  31. } );
  32. describe( 'value', () => {
  33. it( 'is true when selection is in block with commend type alignment', () => {
  34. setModelData( doc, '<paragraph alignment="center">x[]x</paragraph>' );
  35. expect( command ).to.have.property( 'value', true );
  36. } );
  37. it( 'is false when selection is not block that has different alignment', () => {
  38. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  39. expect( command ).to.have.property( 'value', false );
  40. } );
  41. it( 'is true when selection is in block with default alignment', () => {
  42. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  43. expect( defaultAlignmentCommand ).to.have.property( 'value', true );
  44. } );
  45. it( 'is false when selection is not block that has different alignment', () => {
  46. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  47. expect( defaultAlignmentCommand ).to.have.property( 'value', false );
  48. } );
  49. } );
  50. describe( 'isEnabled', () => {
  51. it( 'is true when selection is in a block which can have added alignment', () => {
  52. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  53. expect( command ).to.have.property( 'isEnabled', true );
  54. } );
  55. } );
  56. describe( 'execute()', () => {
  57. describe( 'applying alignment', () => {
  58. it( 'add should alignment to block element', () => {
  59. setModelData( doc, '<paragraph>x[]x</paragraph>' );
  60. editor.execute( 'alignCenter' );
  61. expect( getModelData( doc ) ).to.equal(
  62. '<paragraph alignment="center">x[]x</paragraph>'
  63. );
  64. } );
  65. it( 'add should alignment to all selected blocks', () => {
  66. setModelData( doc, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  67. editor.execute( 'alignCenter' );
  68. expect( getModelData( doc ) ).to.equal(
  69. '<paragraph alignment="center">x[x</paragraph>' +
  70. '<paragraph alignment="center">xx</paragraph>' +
  71. '<paragraph alignment="center">x]x</paragraph>'
  72. );
  73. } );
  74. describe( 'applying default alignment', () => {
  75. it( 'add should remove alignment from block element', () => {
  76. setModelData( doc, '<paragraph alignment="justify">x[]x</paragraph>' );
  77. editor.execute( 'alignLeft' );
  78. expect( getModelData( doc ) ).to.equal(
  79. '<paragraph>x[]x</paragraph>'
  80. );
  81. } );
  82. it( 'add remove alignment from all selected blocks', () => {
  83. setModelData( doc, '<paragraph alignment="center">x[x</paragraph>' +
  84. '<paragraph alignment="center">xx</paragraph>' +
  85. '<paragraph alignment="center">x]x</paragraph>' );
  86. editor.execute( 'alignLeft' );
  87. expect( getModelData( doc ) ).to.equal(
  88. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  89. );
  90. } );
  91. } );
  92. } );
  93. } );
  94. } );