8
0

deltareplayer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. } );
  88. function getDocument() {
  89. const doc = new Document();
  90. doc.createRoot( 'main' );
  91. return doc;
  92. }
  93. function getFirstDelta() {
  94. return {
  95. operations: [ {
  96. baseVersion: 0,
  97. position: {
  98. root: 'main',
  99. path: [ 0 ]
  100. },
  101. nodes: [ {
  102. name: 'heading1',
  103. children: [ {
  104. data: 'The great world of open Web standards'
  105. } ]
  106. } ],
  107. __className: 'engine.model.operation.InsertOperation'
  108. } ],
  109. __className: 'engine.model.delta.InsertDelta'
  110. };
  111. }
  112. function getSecondDelta() {
  113. return {
  114. operations: [ {
  115. baseVersion: 1,
  116. position: {
  117. root: 'main',
  118. path: [ 1 ]
  119. },
  120. nodes: [ {
  121. name: 'heading2',
  122. children: [ {
  123. data: 'The great world of open Web standards'
  124. } ]
  125. } ],
  126. __className: 'engine.model.operation.InsertOperation'
  127. } ],
  128. __className: 'engine.model.delta.InsertDelta'
  129. };
  130. }