removeoperation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  7. import NoOperation from '../../../src/model/operation/nooperation';
  8. import RemoveOperation from '../../../src/model/operation/removeoperation';
  9. import MoveOperation from '../../../src/model/operation/moveoperation';
  10. import Position from '../../../src/model/position';
  11. import Text from '../../../src/model/text';
  12. import Element from '../../../src/model/element';
  13. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  14. describe( 'RemoveOperation', () => {
  15. let doc, root, graveyard;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot();
  19. graveyard = doc.graveyard;
  20. } );
  21. it( 'should have proper type', () => {
  22. const op = new RemoveOperation(
  23. new Position( root, [ 2 ] ),
  24. 2,
  25. new Position( doc.graveyard, [ 0 ] ),
  26. doc.version
  27. );
  28. expect( op.type ).to.equal( 'remove' );
  29. } );
  30. it( 'should not be sticky', () => {
  31. const op = new RemoveOperation(
  32. new Position( root, [ 2 ] ),
  33. 2,
  34. new Position( doc.graveyard, [ 0 ] ),
  35. doc.version
  36. );
  37. expect( op.isSticky ).to.be.false;
  38. } );
  39. it( 'should extend MoveOperation class', () => {
  40. const operation = new RemoveOperation(
  41. new Position( root, [ 2 ] ),
  42. 2,
  43. new Position( doc.graveyard, [ 0 ] ),
  44. doc.version
  45. );
  46. expect( operation ).to.be.instanceof( MoveOperation );
  47. } );
  48. it( 'should be able to remove set of nodes and append them to graveyard root', () => {
  49. root.insertChildren( 0, new Text( 'fozbar' ) );
  50. doc.applyOperation( wrapInDelta(
  51. new RemoveOperation(
  52. new Position( root, [ 2 ] ),
  53. 2,
  54. new Position( doc.graveyard, [ 0 ] ),
  55. doc.version
  56. )
  57. ) );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.maxOffset ).to.equal( 4 );
  60. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  61. expect( graveyard.maxOffset ).to.equal( 2 );
  62. expect( graveyard.getChild( 0 ).data ).to.equal( 'zb' );
  63. } );
  64. it( 'should create RemoveOperation with same parameters when cloned', () => {
  65. const pos = new Position( root, [ 2 ] );
  66. const operation = new RemoveOperation( pos, 2, new Position( doc.graveyard, [ 0 ] ), doc.version );
  67. const clone = operation.clone();
  68. expect( clone ).to.be.instanceof( RemoveOperation );
  69. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  70. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  71. expect( clone.howMany ).to.equal( operation.howMany );
  72. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  73. } );
  74. it( 'should create ReinsertOperation when reversed', () => {
  75. const position = new Position( root, [ 0 ] );
  76. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  77. const reverse = operation.getReversed();
  78. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  79. expect( reverse.baseVersion ).to.equal( 1 );
  80. expect( reverse.howMany ).to.equal( 2 );
  81. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  82. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  83. } );
  84. it( 'should create NoOperation when reversed if was permanent', () => {
  85. const position = new Position( root, [ 0 ] );
  86. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  87. operation.isPermanent = true;
  88. const reverse = operation.getReversed();
  89. expect( reverse ).to.be.an.instanceof( NoOperation );
  90. expect( reverse.baseVersion ).to.equal( 1 );
  91. } );
  92. it( 'should undo remove set of nodes by applying reverse operation', () => {
  93. const position = new Position( root, [ 0 ] );
  94. const operation = new RemoveOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), 0 );
  95. const reverse = operation.getReversed();
  96. root.insertChildren( 0, new Text( 'bar' ) );
  97. doc.applyOperation( wrapInDelta( operation ) );
  98. expect( doc.version ).to.equal( 1 );
  99. expect( root.maxOffset ).to.equal( 0 );
  100. doc.applyOperation( wrapInDelta( reverse ) );
  101. expect( doc.version ).to.equal( 2 );
  102. expect( root.maxOffset ).to.equal( 3 );
  103. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  104. } );
  105. it( 'should properly remove a node that is already in a graveyard', () => {
  106. doc.graveyard.appendChildren( [ new Element( 'x' ), new Element( 'y' ), new Element( 'z' ) ] );
  107. const position = new Position( doc.graveyard, [ 2 ] );
  108. const operation = new RemoveOperation( position, 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  109. doc.applyOperation( wrapInDelta( operation ) );
  110. expect( doc.graveyard.childCount ).to.equal( 3 );
  111. expect( doc.graveyard.getChild( 0 ).name ).to.equal( 'z' );
  112. expect( doc.graveyard.getChild( 1 ).name ).to.equal( 'x' );
  113. expect( doc.graveyard.getChild( 2 ).name ).to.equal( 'y' );
  114. } );
  115. describe( 'toJSON', () => {
  116. it( 'should create proper json object', () => {
  117. const op = new RemoveOperation(
  118. new Position( root, [ 2 ] ),
  119. 2,
  120. new Position( doc.graveyard, [ 0 ] ),
  121. doc.version
  122. );
  123. const serialized = jsonParseStringify( op );
  124. expect( serialized ).to.deep.equal( {
  125. __className: 'engine.model.operation.RemoveOperation',
  126. baseVersion: 0,
  127. howMany: 2,
  128. isSticky: false,
  129. isPermanent: false,
  130. sourcePosition: jsonParseStringify( op.sourcePosition ),
  131. targetPosition: jsonParseStringify( op.targetPosition )
  132. } );
  133. } );
  134. } );
  135. describe( 'fromJSON', () => {
  136. it( 'should create proper RemoveOperation from json object', () => {
  137. const op = new RemoveOperation(
  138. new Position( root, [ 2 ] ),
  139. 2,
  140. new Position( doc.graveyard, [ 0 ] ),
  141. doc.version
  142. );
  143. const serialized = jsonParseStringify( op );
  144. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  145. expect( deserialized ).to.deep.equal( op );
  146. } );
  147. } );
  148. } );