renameoperation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. describe( 'toJSON', () => {
  72. it( 'should create proper serialized object', () => {
  73. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  74. const serialized = jsonParseStringify( op );
  75. expect( serialized ).to.deep.equal( {
  76. __className: 'engine.model.operation.RenameOperation',
  77. baseVersion: 0,
  78. position: jsonParseStringify( op.position ),
  79. newName: 'newName',
  80. oldName: 'oldName'
  81. } );
  82. } );
  83. } );
  84. describe( 'fromJSON', () => {
  85. it( 'should create proper AttributeOperation from json object', () => {
  86. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  87. const serialized = jsonParseStringify( op );
  88. const deserialized = RenameOperation.fromJSON( serialized, doc );
  89. expect( deserialized ).to.deep.equal( op );
  90. } );
  91. } );
  92. } );