batch.js 3.2 KB

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