reinsertoperation.js 2.3 KB

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