8
0

history.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import History from '../../src/model/history';
  6. import Operation from '../../src/model/operation/operation';
  7. describe( 'History', () => {
  8. let history;
  9. beforeEach( () => {
  10. history = new History();
  11. } );
  12. describe( 'constructor()', () => {
  13. it( 'should create an empty History instance', () => {
  14. expect( Array.from( history.getOperations() ).length ).to.equal( 0 );
  15. } );
  16. } );
  17. describe( 'addOperation', () => {
  18. it( 'should save operation in the history', () => {
  19. const op = new Operation( 0 );
  20. history.addOperation( op );
  21. const ops = Array.from( history.getOperations() );
  22. expect( ops.length ).to.equal( 1 );
  23. expect( ops[ 0 ] ).to.equal( op );
  24. } );
  25. it( 'should save each operation only once', () => {
  26. const op = new Operation( 0 );
  27. history.addOperation( op );
  28. history.addOperation( op );
  29. const ops = Array.from( history.getOperations() );
  30. expect( ops.length ).to.equal( 1 );
  31. expect( ops[ 0 ] ).to.equal( op );
  32. } );
  33. it( 'should save multiple operations and keep their order', () => {
  34. const ops = [];
  35. ops.push( new Operation( 0 ) );
  36. ops.push( new Operation( 1 ) );
  37. ops.push( new Operation( 2 ) );
  38. for ( const op of ops ) {
  39. history.addOperation( op );
  40. }
  41. const historyOperations = history.getOperations();
  42. expect( historyOperations ).to.deep.equal( ops );
  43. } );
  44. } );
  45. describe( 'getOperation', () => {
  46. it( 'should return operation with given base version', () => {
  47. const op0 = new Operation( 0 );
  48. const op1 = new Operation( 1 );
  49. const op2 = new Operation( 2 );
  50. history.addOperation( op0 );
  51. history.addOperation( op1 );
  52. history.addOperation( op2 );
  53. const historyOperation = history.getOperation( 1 );
  54. expect( historyOperation ).to.equal( op1 );
  55. } );
  56. it( 'should return undefined if operation has not been found in history', () => {
  57. const op0 = new Operation( 0 );
  58. history.addOperation( op0 );
  59. expect( history.getOperation( -1 ) ).to.be.undefined;
  60. expect( history.getOperation( 10 ) ).to.be.undefined;
  61. } );
  62. } );
  63. describe( 'getOperations', () => {
  64. it( 'should return only operations from given base version', () => {
  65. history.addOperation( new Operation( 0 ) );
  66. history.addOperation( new Operation( 1 ) );
  67. history.addOperation( new Operation( 2 ) );
  68. const ops = history.getOperations( 1 );
  69. expect( ops.length ).to.equal( 2 );
  70. expect( ops[ 0 ].baseVersion ).to.equal( 1 );
  71. expect( ops[ 1 ].baseVersion ).to.equal( 2 );
  72. } );
  73. it( 'should return only operations up to given base version', () => {
  74. history.addOperation( new Operation( 0 ) );
  75. history.addOperation( new Operation( 1 ) );
  76. history.addOperation( new Operation( 2 ) );
  77. const ops = history.getOperations( 1, 2 );
  78. expect( ops.length ).to.equal( 1 );
  79. expect( ops[ 0 ].baseVersion ).to.equal( 1 );
  80. } );
  81. it( 'should return empty array if no operations match', () => {
  82. history.addOperation( new Operation( 0 ) );
  83. history.addOperation( new Operation( 1 ) );
  84. expect( history.getOperations( 20 ).length ).to.equal( 0 );
  85. expect( history.getOperations( -3, 0 ).length ).to.equal( 0 );
  86. } );
  87. it( 'should return correct values if history holds operations with negative base version', () => {
  88. history.addOperation( new Operation( -2 ) );
  89. history.addOperation( new Operation( -1 ) );
  90. history.addOperation( new Operation( 0 ) );
  91. history.addOperation( new Operation( 1 ) );
  92. history.addOperation( new Operation( 2 ) );
  93. expect( history.getOperations( -1, 2 ).map( op => op.baseVersion ) ).to.deep.equal( [ -1, 0, 1 ] );
  94. } );
  95. it( 'should return correct values if history holds operations with base versions that differ by more than one', () => {
  96. history.addOperation( new Operation( 0 ) );
  97. history.addOperation( new Operation( 4 ) );
  98. history.addOperation( new Operation( 6 ) );
  99. history.addOperation( new Operation( 9 ) );
  100. history.addOperation( new Operation( 13 ) );
  101. expect( history.getOperations( 2, 11 ).map( op => op.baseVersion ) ).to.deep.equal( [ 4, 6, 9 ] );
  102. } );
  103. } );
  104. describe( 'isUndoingOperation', () => {
  105. let undoing, undone;
  106. beforeEach( () => {
  107. undone = new Operation( 0 );
  108. undoing = new Operation( 1 );
  109. history.addOperation( undone );
  110. history.addOperation( undoing );
  111. history.setOperationAsUndone( undone, undoing );
  112. } );
  113. it( 'should return true if operation is an undoing operation', () => {
  114. expect( history.isUndoingOperation( undoing ) ).to.be.true;
  115. } );
  116. it( 'should return false if operation is not an undoing operation', () => {
  117. const operation = new Operation();
  118. expect( history.isUndoingOperation( operation ) ).to.be.false;
  119. } );
  120. } );
  121. describe( 'isUndoneOperation', () => {
  122. let undoing, undone;
  123. beforeEach( () => {
  124. undone = new Operation( 0 );
  125. undoing = new Operation( 1 );
  126. history.addOperation( undone );
  127. history.addOperation( undoing );
  128. history.setOperationAsUndone( undone, undoing );
  129. } );
  130. it( 'should return true if operation has been set as undone', () => {
  131. expect( history.isUndoneOperation( undone ) ).to.be.true;
  132. } );
  133. it( 'should return false if operation is not an undone', () => {
  134. const operation = new Operation();
  135. expect( history.isUndoneOperation( operation ) ).to.be.false;
  136. } );
  137. } );
  138. describe( 'getUndoneOperation', () => {
  139. let undoing, undone;
  140. beforeEach( () => {
  141. undone = new Operation( 0 );
  142. undoing = new Operation( 1 );
  143. history.addOperation( undone );
  144. history.addOperation( undoing );
  145. history.setOperationAsUndone( undone, undoing );
  146. } );
  147. it( 'should return undone operation basing on undoing operation', () => {
  148. expect( history.getUndoneOperation( undoing ) ).to.equal( undone );
  149. } );
  150. it( 'should return undefined if given operation is not an undoing operation', () => {
  151. const op = new Operation( 0 );
  152. expect( history.getUndoneOperation( undone ) ).to.be.undefined;
  153. expect( history.getUndoneOperation( op ) ).to.be.undefined;
  154. } );
  155. } );
  156. } );