batch.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Batch from '../../src/model/batch';
  6. import Delta from '../../src/model/delta/delta';
  7. import Operation from '../../src/model/operation/operation';
  8. describe( 'Batch', () => {
  9. describe( 'type', () => {
  10. it( 'should default be "default"', () => {
  11. const batch = new Batch();
  12. expect( batch.type ).to.equal( 'default' );
  13. } );
  14. it( 'should be set to the value set in constructor', () => {
  15. const batch = new Batch( 'ignore' );
  16. expect( batch.type ).to.equal( 'ignore' );
  17. } );
  18. } );
  19. describe( 'baseVersion', () => {
  20. it( 'should return base version of first delta from the batch', () => {
  21. const batch = new Batch();
  22. const delta = new Delta();
  23. const operation = new Operation( 2 );
  24. delta.addOperation( operation );
  25. batch.addDelta( delta );
  26. expect( batch.baseVersion ).to.equal( 2 );
  27. } );
  28. it( 'should return null if there are no deltas in batch', () => {
  29. const batch = new Batch();
  30. expect( batch.baseVersion ).to.be.null;
  31. } );
  32. it( 'should return null if all deltas in batch have base version set to null', () => {
  33. const batch = new Batch();
  34. const deltaA = new Delta();
  35. deltaA.addOperation( new Operation( null ) );
  36. const deltaB = new Delta();
  37. deltaB.addOperation( new Operation( null ) );
  38. batch.addDelta( deltaA );
  39. batch.addDelta( deltaB );
  40. expect( batch.baseVersion ).to.equal( null );
  41. } );
  42. } );
  43. describe( 'addDelta()', () => {
  44. it( 'should add delta to the batch', () => {
  45. const batch = new Batch();
  46. const deltaA = new Delta();
  47. const deltaB = new Delta();
  48. batch.addDelta( deltaA );
  49. batch.addDelta( deltaB );
  50. expect( batch.deltas.length ).to.equal( 2 );
  51. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  52. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  53. } );
  54. } );
  55. describe( 'getOperations()', () => {
  56. it( 'should return collection of operations from all deltas', () => {
  57. const batch = new Batch();
  58. const deltaA = new Delta();
  59. const deltaB = new Delta();
  60. const ops = [
  61. new Operation( 0 ),
  62. new Operation( 1 ),
  63. new Operation( 2 )
  64. ];
  65. batch.addDelta( deltaA );
  66. deltaA.addOperation( ops[ 0 ] );
  67. batch.addDelta( deltaB );
  68. deltaA.addOperation( ops[ 1 ] );
  69. deltaA.addOperation( ops[ 2 ] );
  70. expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
  71. expect( batch.getOperations() ).to.have.property( 'next' );
  72. } );
  73. } );
  74. } );