renameoperation.js 4.2 KB

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