renameoperation.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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. model.applyOperation( wrapInDelta( op ) );
  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. model.applyOperation( wrapInDelta( op ) );
  60. } ).to.throw( CKEditorError, /rename-operation-wrong-name/ );
  61. } );
  62. it( 'should create a RenameOperation with the same parameters when cloned', () => {
  63. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  64. const clone = op.clone();
  65. // New instance rather than a pointer to the old instance.
  66. expect( clone ).not.to.be.equal( op );
  67. expect( clone ).to.be.instanceof( RenameOperation );
  68. expect( clone.baseVersion ).to.equal( op.baseVersion );
  69. expect( clone.position.isEqual( op.position ) ).to.be.true;
  70. expect( clone.oldName ).to.equal( oldName );
  71. expect( clone.newName ).to.equal( newName );
  72. } );
  73. it( 'should do nothing when new name is the same as previous', () => {
  74. const op = new RenameOperation( position, oldName, oldName, doc.version );
  75. expect( () => {
  76. model.applyOperation( wrapInDelta( op ) );
  77. } ).to.not.throw();
  78. } );
  79. describe( 'isDocumentOperation', () => {
  80. it( 'should be true when target item is in the document', () => {
  81. const op = new RenameOperation( position, oldName, newName, doc.version );
  82. expect( op.isDocumentOperation ).to.true;
  83. } );
  84. it( 'should be false when target item is not in the document', () => {
  85. const docFrag = new DocumentFragment( [ new Element( 'element' ) ] );
  86. const op = new RenameOperation( Position.createAt( docFrag ), oldName, newName, doc.version );
  87. expect( op.isDocumentOperation ).to.false;
  88. } );
  89. } );
  90. describe( 'toJSON', () => {
  91. it( 'should create proper serialized object', () => {
  92. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  93. const serialized = jsonParseStringify( op );
  94. expect( serialized ).to.deep.equal( {
  95. __className: 'engine.model.operation.RenameOperation',
  96. baseVersion: 0,
  97. position: jsonParseStringify( op.position ),
  98. newName: 'newName',
  99. oldName: 'oldName'
  100. } );
  101. } );
  102. } );
  103. describe( 'fromJSON', () => {
  104. it( 'should create proper AttributeOperation from json object', () => {
  105. const op = new RenameOperation( Position.createAt( root, 'end' ), oldName, newName, doc.version );
  106. const serialized = jsonParseStringify( op );
  107. const deserialized = RenameOperation.fromJSON( serialized, doc );
  108. expect( deserialized ).to.deep.equal( op );
  109. } );
  110. } );
  111. } );