batch.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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( 'type', () => {
  21. it( 'should default to "default"', () => {
  22. const batch = new Batch( new Document() );
  23. expect( batch.type ).to.equal( 'default' );
  24. } );
  25. it( 'should be set to the value set in constructor', () => {
  26. const batch = new Batch( new Document(), 'ignore' );
  27. expect( batch.type ).to.equal( 'ignore' );
  28. } );
  29. } );
  30. describe( 'register', () => {
  31. afterEach( () => {
  32. delete Batch.prototype.foo;
  33. } );
  34. it( 'should register function to the batch prototype', () => {
  35. const spy = sinon.spy();
  36. register( 'foo', spy );
  37. const batch = new Batch( new Document() );
  38. batch.foo();
  39. expect( spy.calledOnce ).to.be.true;
  40. } );
  41. it( 'should throw if one try to register the same batch twice', () => {
  42. register( 'foo', () => {} );
  43. expect( () => {
  44. register( 'foo', () => {} );
  45. } ).to.throw( CKEditorError, /^batch-register-taken/ );
  46. } );
  47. } );
  48. describe( 'addDelta', () => {
  49. it( 'should add delta to the batch', () => {
  50. const batch = new Batch( new Document() );
  51. const deltaA = new Delta();
  52. const deltaB = new Delta();
  53. batch.addDelta( deltaA );
  54. batch.addDelta( deltaB );
  55. expect( batch.deltas.length ).to.equal( 2 );
  56. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  57. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  58. } );
  59. } );
  60. describe( 'getOperations', () => {
  61. it( 'should return collection of operations from all deltas', () => {
  62. const doc = new Document();
  63. const batch = new Batch( doc );
  64. const deltaA = new Delta();
  65. const deltaB = new Delta();
  66. const ops = [
  67. new Operation( doc.version ),
  68. new Operation( doc.version + 1 ),
  69. new Operation( doc.version + 2 )
  70. ];
  71. batch.addDelta( deltaA );
  72. deltaA.addOperation( ops[ 0 ] );
  73. batch.addDelta( deltaB );
  74. deltaA.addOperation( ops[ 1 ] );
  75. deltaA.addOperation( ops[ 2 ] );
  76. expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
  77. expect( batch.getOperations() ).to.have.property( 'next' );
  78. } );
  79. } );
  80. } );