history.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import History from '../../src/model/history';
  6. import Delta from '../../src/model/delta/delta';
  7. import Operation from '../../src/model/operation/operation';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  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. const 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. const 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. const deltas = getDeltaSet();
  39. for ( const 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. const 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 delta with given base version', () => {
  53. const delta = getDelta( 0 );
  54. history.addDelta( delta );
  55. const historyDelta = history.getDelta( 0 );
  56. expect( historyDelta ).to.equal( delta );
  57. } );
  58. it( 'should return null if delta has not been found in history', () => {
  59. expect( history.getDelta( -1 ) ).to.be.null;
  60. expect( history.getDelta( 2 ) ).to.be.null;
  61. expect( history.getDelta( 20 ) ).to.be.null;
  62. } );
  63. } );
  64. describe( 'getDeltas', () => {
  65. let deltas;
  66. beforeEach( () => {
  67. deltas = getDeltaSet();
  68. for ( const delta of deltas ) {
  69. history.addDelta( delta );
  70. }
  71. } );
  72. it( 'should return only history deltas from given base version', () => {
  73. const historyDeltas = Array.from( history.getDeltas( 3 ) );
  74. expect( historyDeltas ).to.deep.equal( deltas.slice( 1 ) );
  75. } );
  76. it( 'should return only history deltas to given base version', () => {
  77. const historyDeltas = Array.from( history.getDeltas( 3, 6 ) );
  78. expect( historyDeltas ).to.deep.equal( deltas.slice( 1, 2 ) );
  79. } );
  80. it( 'should return empty (finished) iterator if given history point is too high or negative', () => {
  81. expect( Array.from( history.getDeltas( 20 ) ).length ).to.equal( 0 );
  82. expect( Array.from( history.getDeltas( -1 ) ).length ).to.equal( 0 );
  83. } );
  84. it( 'should throw if given history point is "inside" delta', () => {
  85. expect( () => {
  86. Array.from( history.getDeltas( 2 ) );
  87. } ).to.throw( CKEditorError, /model-history-wrong-version/ );
  88. } );
  89. } );
  90. describe( 'isUndoingDelta', () => {
  91. let undoing, undone;
  92. beforeEach( () => {
  93. undoing = new Delta();
  94. undone = new Delta();
  95. history.addDelta( undone );
  96. history.addDelta( undoing );
  97. history.setDeltaAsUndone( undone, undoing );
  98. } );
  99. it( 'should return true if delta is an undoing delta', () => {
  100. expect( history.isUndoingDelta( undoing ) ).to.be.true;
  101. } );
  102. it( 'should return false if delta is not an undoing delta', () => {
  103. const delta = new Delta();
  104. expect( history.isUndoingDelta( undone ) ).to.be.false;
  105. expect( history.isUndoingDelta( delta ) ).to.be.false;
  106. } );
  107. } );
  108. describe( 'isUndoneDelta', () => {
  109. let undoing, undone;
  110. beforeEach( () => {
  111. undoing = new Delta();
  112. undone = new Delta();
  113. history.addDelta( undone );
  114. history.addDelta( undoing );
  115. history.setDeltaAsUndone( undone, undoing );
  116. } );
  117. it( 'should return true if delta has been set as undone', () => {
  118. expect( history.isUndoneDelta( undone ) ).to.be.true;
  119. } );
  120. it( 'should return false if delta has not been set as undone', () => {
  121. const delta = new Delta();
  122. expect( history.isUndoneDelta( undoing ) ).to.be.false;
  123. expect( history.isUndoneDelta( delta ) ).to.be.false;
  124. } );
  125. } );
  126. describe( 'getUndoneDelta', () => {
  127. let undoing, undone;
  128. beforeEach( () => {
  129. undoing = new Delta();
  130. undone = new Delta();
  131. history.addDelta( undone );
  132. history.addDelta( undoing );
  133. history.setDeltaAsUndone( undone, undoing );
  134. } );
  135. it( 'should return undone delta basing on undoing delta', () => {
  136. expect( history.getUndoneDelta( undoing ) ).to.equal( undone );
  137. } );
  138. it( 'should return undefined if given delta is not an undoing delta', () => {
  139. const delta = new Delta();
  140. expect( history.getUndoneDelta( undone ) ).to.be.undefined;
  141. expect( history.getUndoneDelta( delta ) ).to.be.undefined;
  142. } );
  143. } );
  144. } );
  145. function getDeltaSet() {
  146. const deltas = [];
  147. deltas.push( getDelta( 0 ) );
  148. deltas.push( getDelta( 3 ) );
  149. deltas.push( getDelta( 6 ) );
  150. return deltas;
  151. }
  152. function getDelta( baseVersion ) {
  153. const delta = new Delta();
  154. for ( let i = 0; i < 3; i++ ) {
  155. delta.addOperation( new Operation( i + baseVersion ) );
  156. }
  157. return delta;
  158. }