renameoperation.js 4.1 KB

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