renameoperation.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Element from '../../../src/model/element';
  7. import RenameOperation from '../../../src/model/operation/renameoperation';
  8. import Position from '../../../src/model/position';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  11. describe( 'RenameOperation', () => {
  12. const oldName = 'oldName';
  13. const newName = 'newName';
  14. let doc, root, element, position;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. element = new Element( oldName );
  19. root.appendChildren( element );
  20. position = Position.createBefore( element );
  21. } );
  22. it( 'should have type equal to rename', () => {
  23. const op = new RenameOperation( position, oldName, newName, doc.version );
  24. expect( op.type ).to.equal( 'rename' );
  25. } );
  26. it( 'should change name of given element', () => {
  27. const op = new RenameOperation( position, oldName, newName, doc.version );
  28. doc.applyOperation( wrapInDelta( op ) );
  29. expect( element.name ).to.equal( newName );
  30. } );
  31. it( 'should create a RenameOperation as a reverse', () => {
  32. const op = new RenameOperation( position, oldName, newName, doc.version );
  33. const reverse = op.getReversed();
  34. expect( reverse ).to.be.an.instanceof( RenameOperation );
  35. expect( reverse.baseVersion ).to.equal( 1 );
  36. expect( reverse.position.isEqual( position ) ).to.be.true;
  37. expect( reverse.oldName ).to.equal( newName );
  38. expect( reverse.newName ).to.equal( oldName );
  39. } );
  40. it( 'should undo renaming element by applying reverse operation', () => {
  41. const op = new RenameOperation( position, oldName, newName, doc.version );
  42. const reverse = op.getReversed();
  43. doc.applyOperation( wrapInDelta( op ) );
  44. doc.applyOperation( wrapInDelta( reverse ) );
  45. expect( doc.version ).to.equal( 2 );
  46. expect( element.name ).to.equal( oldName );
  47. } );
  48. it( 'should throw an error if position is not before an element', () => {
  49. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  50. expect( () => {
  51. doc.applyOperation( wrapInDelta( op ) );
  52. } ).to.throw( CKEditorError, /rename-operation-wrong-position/ );
  53. } );
  54. it( 'should throw an error if oldName is different than renamed element name', () => {
  55. const op = new RenameOperation( position, 'foo', newName, doc.version );
  56. expect( () => {
  57. doc.applyOperation( wrapInDelta( op ) );
  58. } ).to.throw( CKEditorError, /rename-operation-wrong-name/ );
  59. } );
  60. it( 'should create a RenameOperation with the same parameters when cloned', () => {
  61. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  62. const clone = op.clone();
  63. // New instance rather than a pointer to the old instance.
  64. expect( clone ).not.to.be.equal( op );
  65. expect( clone ).to.be.instanceof( RenameOperation );
  66. expect( clone.baseVersion ).to.equal( op.baseVersion );
  67. expect( clone.position.isEqual( op.position ) ).to.be.true;
  68. expect( clone.oldName ).to.equal( oldName );
  69. expect( clone.newName ).to.equal( newName );
  70. } );
  71. it( 'should do nothing when new name is the same as previous', () => {
  72. const op = new RenameOperation( position, oldName, oldName, doc.version );
  73. expect( () => {
  74. doc.applyOperation( wrapInDelta( op ) );
  75. } ).to.not.throw();
  76. } );
  77. describe( 'isDocumentOperation', () => {
  78. it( 'should be true when target item is in the document', () => {
  79. const op = new RenameOperation( position, oldName, newName, doc.version );
  80. expect( op.isDocumentOperation ).to.true;
  81. } );
  82. it( 'should be false when target item is not in the document', () => {
  83. const batch = doc.batch();
  84. const docFrag = batch.createDocumentFragment();
  85. batch.appendElement( 'element', null, docFrag );
  86. const op = new RenameOperation( Position.createAt( docFrag ), oldName, newName, doc.version );
  87. expect( op.isDocumentOperation ).to.false;
  88. } );
  89. } );
  90. describe( 'toJSON', () => {
  91. it( 'should create proper serialized object', () => {
  92. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  93. const serialized = jsonParseStringify( op );
  94. expect( serialized ).to.deep.equal( {
  95. __className: 'engine.model.operation.RenameOperation',
  96. baseVersion: 0,
  97. position: jsonParseStringify( op.position ),
  98. newName: 'newName',
  99. oldName: 'oldName'
  100. } );
  101. } );
  102. } );
  103. describe( 'fromJSON', () => {
  104. it( 'should create proper AttributeOperation from json object', () => {
  105. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  106. const serialized = jsonParseStringify( op );
  107. const deserialized = RenameOperation.fromJSON( serialized, doc );
  108. expect( deserialized ).to.deep.equal( op );
  109. } );
  110. } );
  111. } );