batch.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 'use strict';
  7. import deltas from '/ckeditor5/engine/model/delta/basic-deltas.js'; // jshint ignore:line
  8. import Document from '/ckeditor5/engine/model/document.js';
  9. import Batch from '/ckeditor5/engine/model/batch.js';
  10. import { register } from '/ckeditor5/engine/model/batch.js';
  11. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  12. import Operation from '/ckeditor5/engine/model/operation/operation.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. describe( 'Batch', () => {
  15. it( 'should have registered basic methods', () => {
  16. const batch = new Batch( new Document() );
  17. expect( batch.setAttr ).to.be.a( 'function' );
  18. expect( batch.removeAttr ).to.be.a( 'function' );
  19. } );
  20. describe( 'register', () => {
  21. afterEach( () => {
  22. delete Batch.prototype.foo;
  23. } );
  24. it( 'should register function to the batch prototype', () => {
  25. const spy = sinon.spy();
  26. register( 'foo', spy );
  27. const batch = new Batch( new Document() );
  28. batch.foo();
  29. expect( spy.calledOnce ).to.be.true;
  30. } );
  31. it( 'should throw if one try to register the same batch twice', () => {
  32. register( 'foo', () => {} );
  33. expect( () => {
  34. register( 'foo', () => {} );
  35. } ).to.throw( CKEditorError, /^batch-register-taken/ );
  36. } );
  37. } );
  38. describe( 'addDelta', () => {
  39. it( 'should add delta to the batch', () => {
  40. const batch = new Batch( new Document() );
  41. const deltaA = new Delta();
  42. const deltaB = new Delta();
  43. batch.addDelta( deltaA );
  44. batch.addDelta( deltaB );
  45. expect( batch.deltas.length ).to.equal( 2 );
  46. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  47. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  48. } );
  49. } );
  50. describe( 'getOperations', () => {
  51. it( 'should return collection of operations from all deltas', () => {
  52. const doc = new Document();
  53. const batch = new Batch( doc );
  54. const deltaA = new Delta();
  55. const deltaB = new Delta();
  56. const ops = [
  57. new Operation( doc.version ),
  58. new Operation( doc.version + 1 ),
  59. new Operation( doc.version + 2 )
  60. ];
  61. batch.addDelta( deltaA );
  62. deltaA.addOperation( ops[ 0 ] );
  63. batch.addDelta( deltaB );
  64. deltaA.addOperation( ops[ 1 ] );
  65. deltaA.addOperation( ops[ 2 ] );
  66. expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
  67. expect( batch.getOperations() ).to.have.property( 'next' );
  68. } );
  69. } );
  70. } );