8
0

deltareplayer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. } );
  64. function getDocument() {
  65. const doc = new Document();
  66. doc.createRoot( 'main' );
  67. return doc;
  68. }
  69. function getFirstDelta() {
  70. return {
  71. operations: [ {
  72. baseVersion: 0,
  73. position: {
  74. root: 'main',
  75. path: [ 0 ]
  76. },
  77. nodes: [ {
  78. name: 'heading1',
  79. children: [ {
  80. data: 'The great world of open Web standards'
  81. } ]
  82. } ],
  83. __className: 'engine.model.operation.InsertOperation'
  84. } ],
  85. __className: 'engine.model.delta.InsertDelta'
  86. };
  87. }
  88. function getSecondDelta() {
  89. return {
  90. operations: [ {
  91. baseVersion: 1,
  92. position: {
  93. root: 'main',
  94. path: [ 1 ]
  95. },
  96. nodes: [ {
  97. name: 'heading1',
  98. children: [ {
  99. data: 'The great world of open Web standards'
  100. } ]
  101. } ],
  102. __className: 'engine.model.operation.InsertOperation'
  103. } ],
  104. __className: 'engine.model.delta.InsertDelta'
  105. };
  106. }