history.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 = getOperations();
  35. for ( const op of ops ) {
  36. history.addOperation( op );
  37. }
  38. const historyOperations = history.getOperations();
  39. expect( historyOperations ).to.deep.equal( ops );
  40. } );
  41. } );
  42. describe( 'getOperation', () => {
  43. it( 'should return operation with given base version', () => {
  44. const op0 = new Operation( 0 );
  45. const op1 = new Operation( 1 );
  46. const op2 = new Operation( 2 );
  47. history.addOperation( op0 );
  48. history.addOperation( op1 );
  49. history.addOperation( op2 );
  50. const historyOperation = history.getOperation( 1 );
  51. expect( historyOperation ).to.equal( op1 );
  52. } );
  53. it( 'should return undefined if operation has not been found in history', () => {
  54. const op0 = new Operation( 0 );
  55. history.addOperation( op0 );
  56. expect( history.getOperation( -1 ) ).to.be.undefined;
  57. expect( history.getOperation( 10 ) ).to.be.undefined;
  58. } );
  59. } );
  60. describe( 'getOperations', () => {
  61. let ops;
  62. beforeEach( () => {
  63. ops = getOperations();
  64. for ( const op of ops ) {
  65. history.addOperation( op );
  66. }
  67. } );
  68. it( 'should return only operations from given base version', () => {
  69. const historyOperations = Array.from( history.getOperations( 1 ) );
  70. expect( historyOperations ).to.deep.equal( ops.slice( 1 ) );
  71. } );
  72. it( 'should return only operations up to given base version', () => {
  73. const historyOperations = Array.from( history.getOperations( 1, 2 ) );
  74. expect( historyOperations ).to.deep.equal( ops.slice( 1, 2 ) );
  75. } );
  76. it( 'should return empty array if no operations match', () => {
  77. expect( Array.from( history.getOperations( 20 ) ).length ).to.equal( 0 );
  78. expect( Array.from( history.getOperations( -1 ) ).length ).to.equal( 0 );
  79. } );
  80. } );
  81. describe( 'isUndoingOperation', () => {
  82. let undoing, undone;
  83. beforeEach( () => {
  84. undone = new Operation( 0 );
  85. undoing = new Operation( 1 );
  86. history.addOperation( undone );
  87. history.addOperation( undoing );
  88. history.setOperationAsUndone( undone, undoing );
  89. } );
  90. it( 'should return true if operation is an undoing operation', () => {
  91. expect( history.isUndoingOperation( undoing ) ).to.be.true;
  92. } );
  93. it( 'should return false if operation is not an undoing operation', () => {
  94. const operation = new Operation();
  95. expect( history.isUndoingOperation( operation ) ).to.be.false;
  96. } );
  97. } );
  98. describe( 'isUndoneOperation', () => {
  99. let undoing, undone;
  100. beforeEach( () => {
  101. undone = new Operation( 0 );
  102. undoing = new Operation( 1 );
  103. history.addOperation( undone );
  104. history.addOperation( undoing );
  105. history.setOperationAsUndone( undone, undoing );
  106. } );
  107. it( 'should return true if operation has been set as undone', () => {
  108. expect( history.isUndoneOperation( undone ) ).to.be.true;
  109. } );
  110. it( 'should return false if operation is not an undone', () => {
  111. const operation = new Operation();
  112. expect( history.isUndoneOperation( operation ) ).to.be.false;
  113. } );
  114. } );
  115. describe( 'getUndoneOperation', () => {
  116. let undoing, undone;
  117. beforeEach( () => {
  118. undone = new Operation( 0 );
  119. undoing = new Operation( 1 );
  120. history.addOperation( undone );
  121. history.addOperation( undoing );
  122. history.setOperationAsUndone( undone, undoing );
  123. } );
  124. it( 'should return undone operation basing on undoing operation', () => {
  125. expect( history.getUndoneOperation( undoing ) ).to.equal( undone );
  126. } );
  127. it( 'should return undefined if given operation is not an undoing operation', () => {
  128. const op = new Operation( 0 );
  129. expect( history.getUndoneOperation( undone ) ).to.be.undefined;
  130. expect( history.getUndoneOperation( op ) ).to.be.undefined;
  131. } );
  132. } );
  133. } );
  134. function getOperations() {
  135. const ops = [];
  136. ops.push( new Operation( 0 ) );
  137. ops.push( new Operation( 1 ) );
  138. ops.push( new Operation( 2 ) );
  139. return ops;
  140. }