history.js 6.3 KB

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