8
0

batch.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. /* jshint unused: false */
  8. import deltas from '/ckeditor5/core/treemodel/delta/basic-deltas.js';
  9. import Document from '/ckeditor5/core/treemodel/document.js';
  10. import Batch from '/ckeditor5/core/treemodel/batch.js';
  11. import { register } from '/ckeditor5/core/treemodel/batch.js';
  12. import Delta from '/ckeditor5/core/treemodel/delta/delta.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. class TestDelta extends Delta {
  15. constructor( batch ) {
  16. super( batch, [] );
  17. }
  18. }
  19. describe( 'Batch', () => {
  20. it( 'should have registered basic methods', () => {
  21. const batch = new Batch( new Document() );
  22. expect( batch.setAttr ).to.be.a( 'function' );
  23. expect( batch.removeAttr ).to.be.a( 'function' );
  24. } );
  25. describe( 'register', () => {
  26. afterEach( () => {
  27. delete Batch.prototype.foo;
  28. } );
  29. it( 'should register function to the batch prototype', () => {
  30. const spy = sinon.spy();
  31. register( 'foo', spy );
  32. const batch = new Batch( new Document() );
  33. batch.foo();
  34. expect( spy.calledOnce ).to.be.true;
  35. } );
  36. it( 'should throw if one try to register the same batch twice', () => {
  37. register( 'foo', () => {} );
  38. expect( () => {
  39. register( 'foo', () => {} );
  40. } ).to.throw( CKEditorError, /^batch-register-taken/ );
  41. } );
  42. } );
  43. describe( 'addDelta', () => {
  44. it( 'should add delta to the batch', () => {
  45. const batch = new Batch( new Document() );
  46. const deltaA = new Delta();
  47. const deltaB = new Delta();
  48. batch.addDelta( deltaA );
  49. batch.addDelta( deltaB );
  50. expect( batch.deltas.length ).to.equal( 2 );
  51. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  52. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  53. } );
  54. it( 'should fire batch event on it\'s document when first delta is added to the batch', () => {
  55. const doc = new Document();
  56. const batch = new Batch( doc );
  57. const spy = sinon.spy();
  58. doc.on( 'batch', spy );
  59. batch.addDelta( new Delta() );
  60. expect( spy.calledOnce ).to.be.true;
  61. batch.addDelta( new Delta() );
  62. expect( spy.calledOnce ).to.be.true;
  63. } );
  64. } );
  65. } );