removeoperation.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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( 'RemoveOperation', () => {
  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/removeoperation' ];
  21. Position = modules[ 'document/position' ];
  22. } );
  23. let doc, root, graveyard;
  24. beforeEach( () => {
  25. doc = new Document();
  26. root = doc.createRoot( 'root' );
  27. graveyard = doc._graveyard;
  28. } );
  29. it( 'should extend MoveOperation class', () => {
  30. let operation = new RemoveOperation(
  31. new Position( [ 2 ], root ),
  32. 2,
  33. doc.version
  34. );
  35. expect( operation ).to.be.instanceof( MoveOperation );
  36. } );
  37. it( 'should remove set of nodes and append them to graveyard root', () => {
  38. root.insertChildren( 0, 'fozbar' );
  39. let z = root.getChild( 2 );
  40. let b = root.getChild( 3 );
  41. let a = root.getChild( 4 );
  42. doc.applyOperation(
  43. new RemoveOperation(
  44. new Position( [ 2 ], root ),
  45. 2,
  46. doc.version
  47. )
  48. );
  49. expect( doc.version ).to.equal( 1 );
  50. expect( root.getChildCount() ).to.equal( 4 );
  51. expect( root.getChild( 2 ) ).to.equal( a );
  52. expect( graveyard.getChildCount() ).to.equal( 2 );
  53. expect( graveyard.getChild( 0 ) ).to.equal( z );
  54. expect( graveyard.getChild( 1 ) ).to.equal( b );
  55. } );
  56. it( 'should create a reinsert operation as a reverse', () => {
  57. let position = new Position( [ 0 ], root );
  58. let operation = new RemoveOperation( position, 2, 0 );
  59. let reverse = operation.getReversed();
  60. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  61. expect( reverse.baseVersion ).to.equal( 1 );
  62. expect( reverse.howMany ).to.equal( 2 );
  63. expect( reverse.sourcePosition ).to.equal( operation.targetPosition );
  64. expect( reverse.targetPosition ).to.equal( position );
  65. } );
  66. it( 'should undo remove set of nodes by applying reverse operation', () => {
  67. let position = new Position( [ 0 ], root );
  68. let operation = new RemoveOperation( position, 3, 0 );
  69. let reverse = operation.getReversed();
  70. root.insertChildren( 0, 'bar' );
  71. doc.applyOperation( operation );
  72. expect( doc.version ).to.equal( 1 );
  73. expect( root.getChildCount() ).to.equal( 0 );
  74. doc.applyOperation( reverse );
  75. expect( doc.version ).to.equal( 2 );
  76. expect( root.getChildCount() ).to.equal( 3 );
  77. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  78. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  79. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  80. } );
  81. } );