8
0

batch.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import deltas from '../../src/model/delta/basic-deltas'; // eslint-disable-line no-unused-vars
  6. import Document from '../../src/model/document';
  7. import { default as Batch, register } from '../../src/model/batch';
  8. import Delta from '../../src/model/delta/delta';
  9. import Operation from '../../src/model/operation/operation';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. describe( 'Batch', () => {
  12. it( 'should have registered basic methods', () => {
  13. const batch = new Batch( new Document() );
  14. expect( batch.setAttribute ).to.be.a( 'function' );
  15. expect( batch.removeAttribute ).to.be.a( 'function' );
  16. } );
  17. describe( 'type', () => {
  18. it( 'should default to "default"', () => {
  19. const batch = new Batch( new Document() );
  20. expect( batch.type ).to.equal( 'default' );
  21. } );
  22. it( 'should be set to the value set in constructor', () => {
  23. const batch = new Batch( new Document(), 'ignore' );
  24. expect( batch.type ).to.equal( 'ignore' );
  25. } );
  26. } );
  27. describe( 'register', () => {
  28. afterEach( () => {
  29. delete Batch.prototype.foo;
  30. } );
  31. it( 'should register function to the batch prototype', () => {
  32. const spy = sinon.spy();
  33. register( 'foo', spy );
  34. const batch = new Batch( new Document() );
  35. batch.foo();
  36. expect( spy.calledOnce ).to.be.true;
  37. } );
  38. it( 'should throw if one try to register the same batch twice', () => {
  39. register( 'foo', () => {} );
  40. expect( () => {
  41. register( 'foo', () => {} );
  42. } ).to.throw( CKEditorError, /^model-batch-register-taken/ );
  43. } );
  44. } );
  45. describe( 'addDelta', () => {
  46. it( 'should add delta to the batch', () => {
  47. const batch = new Batch( new Document() );
  48. const deltaA = new Delta();
  49. const deltaB = new Delta();
  50. batch.addDelta( deltaA );
  51. batch.addDelta( deltaB );
  52. expect( batch.deltas.length ).to.equal( 2 );
  53. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  54. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  55. } );
  56. } );
  57. describe( 'getOperations', () => {
  58. it( 'should return collection of operations from all deltas', () => {
  59. const doc = new Document();
  60. const batch = new Batch( doc );
  61. const deltaA = new Delta();
  62. const deltaB = new Delta();
  63. const ops = [
  64. new Operation( doc.version ),
  65. new Operation( doc.version + 1 ),
  66. new Operation( doc.version + 2 )
  67. ];
  68. batch.addDelta( deltaA );
  69. deltaA.addOperation( ops[ 0 ] );
  70. batch.addDelta( deltaB );
  71. deltaA.addOperation( ops[ 1 ] );
  72. deltaA.addOperation( ops[ 2 ] );
  73. expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
  74. expect( batch.getOperations() ).to.have.property( 'next' );
  75. } );
  76. } );
  77. describe( 'baseVersion', () => {
  78. it( 'should return base version of first delta from the batch', () => {
  79. const batch = new Batch( new Document() );
  80. const delta = new Delta();
  81. const operation = new Operation( 2 );
  82. delta.addOperation( operation );
  83. batch.addDelta( delta );
  84. expect( batch.baseVersion ).to.equal( 2 );
  85. } );
  86. it( 'should return null if there are no deltas in batch', () => {
  87. const batch = new Batch( new Document() );
  88. expect( batch.baseVersion ).to.be.null;
  89. } );
  90. } );
  91. } );