insertoperation.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /* global describe, before, beforeEach, it, expect */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'document/document',
  10. 'document/node',
  11. 'document/nodelist',
  12. 'document/operation/insertoperation',
  13. 'document/operation/removeoperation',
  14. 'document/position',
  15. 'document/character',
  16. 'document/nodelist'
  17. );
  18. describe( 'InsertOperation', () => {
  19. let Document, Node, NodeList, InsertOperation, RemoveOperation, Position, Character;
  20. before( () => {
  21. Document = modules[ 'document/document' ];
  22. Node = modules[ 'document/node' ];
  23. NodeList = modules[ 'document/nodelist' ];
  24. InsertOperation = modules[ 'document/operation/insertoperation' ];
  25. RemoveOperation = modules[ 'document/operation/removeoperation' ];
  26. Position = modules[ 'document/position' ];
  27. Character = modules[ 'document/character' ];
  28. } );
  29. let doc, root;
  30. beforeEach( () => {
  31. doc = new Document();
  32. root = doc.createRoot( 'root' );
  33. } );
  34. it( 'should have proper type', () => {
  35. const opp = new InsertOperation(
  36. new Position( [ 0 ], root ),
  37. new Character( 'x' ),
  38. doc.version
  39. );
  40. expect( opp.type ).to.equal( 'insert' );
  41. } );
  42. it( 'should insert node', () => {
  43. doc.applyOperation(
  44. new InsertOperation(
  45. new Position( [ 0 ], root ),
  46. new Character( 'x' ),
  47. doc.version
  48. )
  49. );
  50. expect( doc.version ).to.equal( 1 );
  51. expect( root.getChildCount() ).to.equal( 1 );
  52. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  53. } );
  54. it( 'should insert set of nodes', () => {
  55. doc.applyOperation(
  56. new InsertOperation(
  57. new Position( [ 0 ], root ),
  58. 'bar',
  59. doc.version
  60. )
  61. );
  62. expect( doc.version ).to.equal( 1 );
  63. expect( root.getChildCount() ).to.equal( 3 );
  64. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  65. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  66. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  67. } );
  68. it( 'should insert between existing nodes', () => {
  69. root.insertChildren( 0, 'xy' );
  70. doc.applyOperation(
  71. new InsertOperation(
  72. new Position( [ 1 ], root ),
  73. 'bar',
  74. doc.version
  75. )
  76. );
  77. expect( doc.version ).to.equal( 1 );
  78. expect( root.getChildCount() ).to.equal( 5 );
  79. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  80. expect( root.getChild( 1 ).character ).to.equal( 'b' );
  81. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  82. expect( root.getChild( 3 ).character ).to.equal( 'r' );
  83. expect( root.getChild( 4 ).character ).to.equal( 'y' );
  84. } );
  85. it( 'should insert text', () => {
  86. doc.applyOperation(
  87. new InsertOperation(
  88. new Position( [ 0 ], root ),
  89. [ 'foo', new Character( 'x' ), 'bar' ],
  90. doc.version
  91. )
  92. );
  93. expect( doc.version ).to.equal( 1 );
  94. expect( root.getChildCount() ).to.equal( 7 );
  95. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  96. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  97. expect( root.getChild( 2 ).character ).to.equal( 'o' );
  98. expect( root.getChild( 3 ).character ).to.equal( 'x' );
  99. expect( root.getChild( 4 ).character ).to.equal( 'b' );
  100. expect( root.getChild( 5 ).character ).to.equal( 'a' );
  101. expect( root.getChild( 6 ).character ).to.equal( 'r' );
  102. } );
  103. it( 'should create a remove operation as a reverse', () => {
  104. let position = new Position( [ 0 ], root );
  105. let operation = new InsertOperation(
  106. position,
  107. [ 'foo', new Character( 'x' ), 'bar' ],
  108. 0
  109. );
  110. let reverse = operation.getReversed();
  111. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  112. expect( reverse.baseVersion ).to.equal( 1 );
  113. expect( reverse.sourcePosition ).to.equal( position );
  114. expect( reverse.howMany ).to.equal( 7 );
  115. } );
  116. it( 'should undo insert node by applying reverse operation', () => {
  117. let operation = new InsertOperation(
  118. new Position( [ 0 ], root ),
  119. new Character( 'x' ),
  120. doc.version
  121. );
  122. let reverse = operation.getReversed();
  123. doc.applyOperation( operation );
  124. expect( doc.version ).to.equal( 1 );
  125. doc.applyOperation( reverse );
  126. expect( doc.version ).to.equal( 2 );
  127. expect( root.getChildCount() ).to.equal( 0 );
  128. } );
  129. it( 'should undo insert set of nodes by applying reverse operation', () => {
  130. let operation = new InsertOperation(
  131. new Position( [ 0 ], root ),
  132. 'bar',
  133. doc.version
  134. );
  135. let reverse = operation.getReversed();
  136. doc.applyOperation( operation );
  137. expect( doc.version ).to.equal( 1 );
  138. doc.applyOperation( reverse );
  139. expect( doc.version ).to.equal( 2 );
  140. expect( root.getChildCount() ).to.equal( 0 );
  141. } );
  142. it( 'should create operation with the same parameters when cloned', () => {
  143. let position = new Position( [ 0 ], root );
  144. let nodeA = new Node();
  145. let nodeB = new Node();
  146. let nodes = new NodeList( [ nodeA, nodeB ] );
  147. let baseVersion = doc.version;
  148. let op = new InsertOperation( position, nodes, baseVersion );
  149. let clone = op.clone();
  150. // New instance rather than a pointer to the old instance.
  151. expect( clone ).not.to.be.equal( op );
  152. expect( clone ).to.be.instanceof( InsertOperation );
  153. expect( clone.position.isEqual( position ) ).to.be.true;
  154. expect( clone.nodeList.get( 0 ) ).to.equal( nodeA );
  155. expect( clone.nodeList.get( 1 ) ).to.equal( nodeB );
  156. expect( clone.nodeList.length ).to.equal( 2 );
  157. expect( clone.baseVersion ).to.equal( baseVersion );
  158. } );
  159. } );