8
0

history.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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( 'getDeltas', () => {
  53. let deltas;
  54. beforeEach( () => {
  55. deltas = getDeltaSet();
  56. for ( let delta of deltas ) {
  57. history.addDelta( delta );
  58. }
  59. } );
  60. it( 'should return only history deltas from given base version', () => {
  61. const historyDeltas = Array.from( history.getDeltas( 3 ) );
  62. expect( historyDeltas ).to.deep.equal( deltas.slice( 1 ) );
  63. } );
  64. it( 'should return only history deltas to given base version', () => {
  65. const historyDeltas = Array.from( history.getDeltas( 3, 6 ) );
  66. expect( historyDeltas ).to.deep.equal( deltas.slice( 1, 2 ) );
  67. } );
  68. it( 'should return empty (finished) iterator if given history point is too high or negative', () => {
  69. expect( Array.from( history.getDeltas( 20 ) ).length ).to.equal( 0 );
  70. expect( Array.from( history.getDeltas( -1 ) ).length ).to.equal( 0 );
  71. } );
  72. it( 'should throw if given history point is "inside" delta', () => {
  73. expect( () => {
  74. Array.from( history.getDeltas( 2 ) );
  75. } ).to.throw( CKEditorError, /history-wrong-version/ );
  76. } );
  77. } );
  78. describe( 'getDelta', () => {
  79. beforeEach( () => {
  80. for ( let delta of getDeltaSet() ) {
  81. history.addDelta( delta );
  82. }
  83. } );
  84. it( 'should return delta from history that has given base version', () => {
  85. let delta = history.getDelta( 3 );
  86. expect( delta.baseVersion ).to.equal( 3 );
  87. } );
  88. it( 'should return null if delta has not been found in history', () => {
  89. expect( history.getDelta( -1 ) ).to.be.null;
  90. expect( history.getDelta( 2 ) ).to.be.null;
  91. expect( history.getDelta( 20 ) ).to.be.null;
  92. } );
  93. } );
  94. } );
  95. function getDeltaSet() {
  96. const deltas = [];
  97. deltas.push( getDelta( 0 ) );
  98. deltas.push( getDelta( 3 ) );
  99. deltas.push( getDelta( 6 ) );
  100. return deltas;
  101. }
  102. function getDelta( baseVersion ) {
  103. const delta = new Delta();
  104. for ( let i = 0; i < 3; i++ ) {
  105. delta.addOperation( new Operation( i + baseVersion ) );
  106. }
  107. return delta;
  108. }