8
0

renameoperation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import Element from '../../../src/model/element';
  7. import RenameOperation from '../../../src/model/operation/renameoperation';
  8. import Position from '../../../src/model/position';
  9. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'RenameOperation', () => {
  11. const oldName = 'oldName';
  12. const newName = 'newName';
  13. let model, doc, root, element, position;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. element = new Element( oldName );
  19. root._appendChild( 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. model.applyOperation( 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. model.applyOperation( op );
  44. model.applyOperation( reverse );
  45. expect( doc.version ).to.equal( 2 );
  46. expect( element.name ).to.equal( oldName );
  47. } );
  48. describe( '_validate()', () => {
  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. expectToThrowCKEditorError( () => {
  52. op._validate();
  53. }, /rename-operation-wrong-position/, model );
  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. expectToThrowCKEditorError( () => {
  58. op._validate();
  59. }, /rename-operation-wrong-name/, model );
  60. } );
  61. it( 'should not throw when new name is the same as previous', () => {
  62. const op = new RenameOperation( position, oldName, oldName, doc.version );
  63. expect( () => {
  64. op._validate();
  65. } ).to.not.throw();
  66. } );
  67. } );
  68. it( 'should create a RenameOperation with the same parameters when cloned', () => {
  69. const op = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version );
  70. const clone = op.clone();
  71. // New instance rather than a pointer to the old instance.
  72. expect( clone ).not.to.equal( op );
  73. expect( clone ).to.be.instanceof( RenameOperation );
  74. expect( clone.baseVersion ).to.equal( op.baseVersion );
  75. expect( clone.position.isEqual( op.position ) ).to.be.true;
  76. expect( clone.oldName ).to.equal( oldName );
  77. expect( clone.newName ).to.equal( newName );
  78. } );
  79. describe( 'toJSON', () => {
  80. it( 'should create proper serialized object', () => {
  81. const op = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version );
  82. const serialized = op.toJSON();
  83. expect( serialized ).to.deep.equal( {
  84. __className: 'RenameOperation',
  85. baseVersion: 0,
  86. position: op.position.toJSON(),
  87. newName: 'newName',
  88. oldName: 'oldName'
  89. } );
  90. } );
  91. } );
  92. describe( 'fromJSON', () => {
  93. it( 'should create proper AttributeOperation from json object', () => {
  94. const op = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version );
  95. const serialized = op.toJSON();
  96. const deserialized = RenameOperation.fromJSON( serialized, doc );
  97. expect( deserialized ).to.deep.equal( op );
  98. } );
  99. } );
  100. } );