8
0

deltareplayer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import DeltaReplayer from '../../src/dev-utils/deltareplayer';
  6. import Document from '../../src/model/document';
  7. describe( 'DeltaReplayer', () => {
  8. describe( 'constructor()', () => {
  9. it( 'should be able to initialize replayer without deltas', () => {
  10. const doc = getDocument();
  11. const stringifiedDeltas = '';
  12. const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
  13. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
  14. } );
  15. it( 'should be able to initialize replayer with deltas', () => {
  16. const doc = getDocument();
  17. const delta = getFirstDelta();
  18. const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
  19. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ delta ] );
  20. } );
  21. } );
  22. describe( 'applyNextDelta()', () => {
  23. it( 'should remove first delta from stack', () => {
  24. const doc = getDocument();
  25. const delta = getFirstDelta();
  26. const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
  27. return deltaReplayer.applyNextDelta().then( () => {
  28. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
  29. } );
  30. } );
  31. it( 'should apply first delta on the document', () => {
  32. const doc = getDocument();
  33. const delta = getFirstDelta();
  34. const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
  35. return deltaReplayer.applyNextDelta().then( () => {
  36. expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 1 );
  37. } );
  38. } );
  39. it( 'should throw an error if 0 deltas are provided', () => {
  40. const doc = getDocument();
  41. const deltaReplayer = new DeltaReplayer( doc, '---', '' );
  42. return deltaReplayer.applyNextDelta().then( () => {
  43. throw new Error( 'This should throw an error' );
  44. }, ( err ) => {
  45. expect( err instanceof Error ).to.equal( true );
  46. expect( err.message ).to.equal( 'No deltas to replay' );
  47. } );
  48. } );
  49. } );
  50. describe( 'applyAllDeltas', () => {
  51. it( 'should apply all deltas on the document', () => {
  52. const doc = getDocument();
  53. const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
  54. .map( d => JSON.stringify( d ) )
  55. .join( '---' );
  56. const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
  57. return deltaReplayer.applyAllDeltas().then( () => {
  58. expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
  59. expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
  60. } );
  61. } );
  62. } );
  63. describe( 'applyDeltas', () => {
  64. it( 'should apply certain number of deltas on the document', () => {
  65. const doc = getDocument();
  66. const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
  67. .map( d => JSON.stringify( d ) )
  68. .join( '---' );
  69. const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
  70. return deltaReplayer.applyDeltas( 1 ).then( () => {
  71. expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 1 );
  72. expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 1 );
  73. } );
  74. } );
  75. it( 'should not throw an error if the number of deltas is lower than number of expected deltas to replay', () => {
  76. const doc = getDocument();
  77. const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
  78. .map( d => JSON.stringify( d ) )
  79. .join( '---' );
  80. const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
  81. return deltaReplayer.applyDeltas( 3 ).then( () => {
  82. expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
  83. expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
  84. } );
  85. } );
  86. } );
  87. describe( 'play', () => {
  88. it( 'should play deltas with time interval', ( done ) => {
  89. const doc = getDocument();
  90. const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
  91. .map( d => JSON.stringify( d ) )
  92. .join( '---' );
  93. const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
  94. deltaReplayer.play( 0, () => {
  95. expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
  96. done();
  97. } );
  98. } );
  99. } );
  100. } );
  101. function getDocument() {
  102. const doc = new Document();
  103. doc.createRoot( 'main' );
  104. return doc;
  105. }
  106. function getFirstDelta() {
  107. return {
  108. operations: [ {
  109. baseVersion: 0,
  110. position: {
  111. root: 'main',
  112. path: [ 0 ]
  113. },
  114. nodes: [ {
  115. name: 'heading1',
  116. children: [ {
  117. data: 'The great world of open Web standards'
  118. } ]
  119. } ],
  120. __className: 'engine.model.operation.InsertOperation'
  121. } ],
  122. __className: 'engine.model.delta.InsertDelta'
  123. };
  124. }
  125. function getSecondDelta() {
  126. return {
  127. operations: [ {
  128. baseVersion: 1,
  129. position: {
  130. root: 'main',
  131. path: [ 1 ]
  132. },
  133. nodes: [ {
  134. name: 'heading2',
  135. children: [ {
  136. data: 'The great world of open Web standards'
  137. } ]
  138. } ],
  139. __className: 'engine.model.operation.InsertOperation'
  140. } ],
  141. __className: 'engine.model.delta.InsertDelta'
  142. };
  143. }