8
0

removeoperation.js 3.1 KB

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