reinsertoperation.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import ReinsertOperation from '/ckeditor5/core/treemodel/operation/reinsertoperation.js';
  9. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  10. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. describe( 'ReinsertOperation', () => {
  13. let doc, root, graveyard, operation, graveyardPosition, rootPosition;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. graveyard = doc.graveyard;
  18. graveyardPosition = new Position( graveyard, [ 0 ] );
  19. rootPosition = new Position( root, [ 0 ] );
  20. operation = new ReinsertOperation(
  21. graveyardPosition,
  22. 2,
  23. rootPosition,
  24. doc.version
  25. );
  26. } );
  27. it( 'should have proper type', () => {
  28. expect( operation.type ).to.equal( 'reinsert' );
  29. } );
  30. it( 'should extend MoveOperation class', () => {
  31. expect( operation ).to.be.instanceof( MoveOperation );
  32. } );
  33. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  34. let clone = operation.clone();
  35. expect( clone ).to.be.instanceof( ReinsertOperation );
  36. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  37. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  38. expect( clone.howMany ).to.equal( operation.howMany );
  39. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  40. } );
  41. it( 'should create a RemoveOperation as a reverse', () => {
  42. let reverse = operation.getReversed();
  43. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  44. expect( reverse.baseVersion ).to.equal( 1 );
  45. expect( reverse.howMany ).to.equal( 2 );
  46. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  47. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  48. } );
  49. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  50. let reverse = operation.getReversed();
  51. graveyard.insertChildren( 0, 'bar' );
  52. doc.applyOperation( operation );
  53. expect( doc.version ).to.equal( 1 );
  54. expect( root.getChildCount() ).to.equal( 2 );
  55. doc.applyOperation( reverse );
  56. expect( doc.version ).to.equal( 2 );
  57. expect( root.getChildCount() ).to.equal( 0 );
  58. expect( graveyard.getChildCount() ).to.equal( 3 );
  59. expect( graveyard.getChild( 0 ).character ).to.equal( 'b' );
  60. expect( graveyard.getChild( 1 ).character ).to.equal( 'a' );
  61. expect( graveyard.getChild( 2 ).character ).to.equal( 'r' );
  62. } );
  63. } );