reinsertoperation.js 2.9 KB

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