batch.js 3.3 KB

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