history.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import History from '/ckeditor5/engine/model/history.js';
  6. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  7. import Operation from '/ckeditor5/engine/model/operation/operation.js';
  8. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  9. describe( 'History', () => {
  10. let history;
  11. beforeEach( () => {
  12. history = new History();
  13. } );
  14. describe( 'constructor', () => {
  15. it( 'should create an empty History instance', () => {
  16. expect( Array.from( history.getDeltas() ).length ).to.equal( 0 );
  17. } );
  18. } );
  19. describe( 'addDelta', () => {
  20. it( 'should save delta in the history', () => {
  21. let delta = new Delta();
  22. delta.addOperation( new Operation( 0 ) );
  23. history.addDelta( delta );
  24. const deltas = Array.from( history.getDeltas() );
  25. expect( deltas.length ).to.equal( 1 );
  26. expect( deltas[ 0 ] ).to.equal( delta );
  27. } );
  28. it( 'should save each delta only once', () => {
  29. let delta = new Delta();
  30. delta.addOperation( new Operation( 0 ) );
  31. history.addDelta( delta );
  32. history.addDelta( delta );
  33. const deltas = Array.from( history.getDeltas() );
  34. expect( deltas.length ).to.equal( 1 );
  35. expect( deltas[ 0 ] ).to.equal( delta );
  36. } );
  37. it( 'should save multiple deltas and keep their order', () => {
  38. let deltas = getDeltaSet();
  39. for ( let delta of deltas ) {
  40. history.addDelta( delta );
  41. }
  42. const historyDeltas = Array.from( history.getDeltas() );
  43. expect( historyDeltas ).to.deep.equal( deltas );
  44. } );
  45. it( 'should skip deltas that does not have operations', () => {
  46. let delta = new Delta();
  47. history.addDelta( delta );
  48. expect( Array.from( history.getDeltas() ).length ).to.equal( 0 );
  49. } );
  50. } );
  51. describe( 'getDelta', () => {
  52. it( 'should return array with one delta with given base version', () => {
  53. let delta = getDelta( 0 );
  54. history.addDelta( delta );
  55. const historyDelta = history.getDelta( 0 );
  56. expect( historyDelta ).to.deep.equal( [ delta ] );
  57. } );
  58. it( 'should return array with all updated deltas of delta with given base version', () => {
  59. let delta = getDelta( 0 );
  60. history.addDelta( delta );
  61. let deltas = getDeltaSet();
  62. history.updateDelta( 0, deltas );
  63. const historyDelta = history.getDelta( 0 );
  64. expect( historyDelta ).to.deep.equal( deltas );
  65. } );
  66. it( 'should return null if delta has not been found in history', () => {
  67. expect( history.getDelta( -1 ) ).to.be.null;
  68. expect( history.getDelta( 2 ) ).to.be.null;
  69. expect( history.getDelta( 20 ) ).to.be.null;
  70. } );
  71. it( 'should return null if delta has been removed by removeDelta', () => {
  72. let delta = getDelta( 0 );
  73. history.addDelta( delta );
  74. history.removeDelta( 0 );
  75. expect( history.getDelta( 0 ) ).to.be.null;
  76. } );
  77. } );
  78. describe( 'getDeltas', () => {
  79. let deltas;
  80. beforeEach( () => {
  81. deltas = getDeltaSet();
  82. for ( let delta of deltas ) {
  83. history.addDelta( delta );
  84. }
  85. } );
  86. it( 'should return only history deltas from given base version', () => {
  87. const historyDeltas = Array.from( history.getDeltas( 3 ) );
  88. expect( historyDeltas ).to.deep.equal( deltas.slice( 1 ) );
  89. } );
  90. it( 'should return only history deltas to given base version', () => {
  91. const historyDeltas = Array.from( history.getDeltas( 3, 6 ) );
  92. expect( historyDeltas ).to.deep.equal( deltas.slice( 1, 2 ) );
  93. } );
  94. it( 'should return empty (finished) iterator if given history point is too high or negative', () => {
  95. expect( Array.from( history.getDeltas( 20 ) ).length ).to.equal( 0 );
  96. expect( Array.from( history.getDeltas( -1 ) ).length ).to.equal( 0 );
  97. } );
  98. it( 'should throw if given history point is "inside" delta', () => {
  99. expect( () => {
  100. Array.from( history.getDeltas( 2 ) );
  101. } ).to.throw( CKEditorError, /model-history-wrong-version/ );
  102. } );
  103. } );
  104. describe( 'updateDelta', () => {
  105. it( 'should substitute delta from history by given deltas', () => {
  106. history.addDelta( getDelta( 0 ) );
  107. const deltas = getDeltaSet();
  108. history.updateDelta( 0, deltas );
  109. const historyDeltas = Array.from( history.getDeltas() );
  110. expect( historyDeltas ).to.deep.equal( deltas );
  111. } );
  112. it( 'should substitute all updated deltas by new deltas', () => {
  113. history.addDelta( getDelta( 0 ) );
  114. // Change original single delta to three deltas generated by `getDeltaSet`.
  115. // All those deltas should now be seen under base version 0.
  116. history.updateDelta( 0, getDeltaSet() );
  117. const deltas = getDeltaSet();
  118. // Change all three deltas from base version 0 to new set of deltas.
  119. history.updateDelta( 0, deltas );
  120. const historyDeltas = Array.from( history.getDeltas() );
  121. expect( historyDeltas ).to.deep.equal( deltas );
  122. } );
  123. it( 'should do nothing if deltas for given base version has not been found in history', () => {
  124. history.addDelta( getDelta( 0 ) );
  125. history.removeDelta( 0 );
  126. const deltas = getDeltaSet();
  127. history.updateDelta( 0, deltas );
  128. expect( Array.from( history.getDeltas() ).length ).to.equal( 0 );
  129. } );
  130. } );
  131. describe( 'removeDelta', () => {
  132. it( 'should remove deltas that do not have graveyard related operations', () => {
  133. for ( let delta of getDeltaSet() ) {
  134. history.addDelta( delta );
  135. }
  136. history.removeDelta( 3 );
  137. const deltas = Array.from( history.getDeltas() );
  138. expect( deltas.length ).to.equal( 2 );
  139. } );
  140. it( 'should remove multiple updated deltas', () => {
  141. let delta = getDelta( 0 );
  142. history.addDelta( delta );
  143. let updatedDeltas = getDeltaSet( 0 );
  144. history.updateDelta( 0, updatedDeltas );
  145. history.removeDelta( 0 );
  146. const deltas = Array.from( history.getDeltas() );
  147. expect( deltas.length ).to.equal( 0 );
  148. } );
  149. it( 'should do nothing if deltas for given base version has not been found in history', () => {
  150. const deltas = getDeltaSet();
  151. for ( let delta of deltas ) {
  152. history.addDelta( delta );
  153. }
  154. history.removeDelta( 12 );
  155. expect( Array.from( history.getDeltas() ) ).to.deep.equal( deltas );
  156. } );
  157. } );
  158. } );
  159. function getDeltaSet() {
  160. const deltas = [];
  161. deltas.push( getDelta( 0 ) );
  162. deltas.push( getDelta( 3 ) );
  163. deltas.push( getDelta( 6 ) );
  164. return deltas;
  165. }
  166. function getDelta( baseVersion ) {
  167. const delta = new Delta();
  168. for ( let i = 0; i < 3; i++ ) {
  169. delta.addOperation( new Operation( i + baseVersion ) );
  170. }
  171. return delta;
  172. }