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