batch.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import deltas from '/ckeditor5/core/treemodel/delta/basic-deltas.js';
  8. /*jshint unused: false*/
  9. import Batch from '/ckeditor5/core/treemodel/batch.js';
  10. import { register } from '/ckeditor5/core/treemodel/batch.js';
  11. import Delta from '/ckeditor5/core/treemodel/delta/delta.js';
  12. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  13. describe( 'Batch', () => {
  14. it( 'should have registered basic methods', () => {
  15. const batch = new Batch();
  16. expect( batch.setAttr ).to.be.a( 'function' );
  17. expect( batch.removeAttr ).to.be.a( 'function' );
  18. } );
  19. describe( 'register', () => {
  20. let TestDelta;
  21. before( () => {
  22. TestDelta = class extends Delta {
  23. constructor( batch ) {
  24. super( batch, [] );
  25. }
  26. };
  27. } );
  28. afterEach( () => {
  29. delete Batch.prototype.foo;
  30. } );
  31. it( 'should register function which return an delta', () => {
  32. register( 'foo', function() {
  33. this.addDelta( new TestDelta() );
  34. } );
  35. const batch = new Batch();
  36. batch.foo();
  37. expect( batch.deltas.length ).to.equal( 1 );
  38. expect( batch.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  39. } );
  40. it( 'should register function which return an multiple deltas', () => {
  41. register( 'foo', function() {
  42. this.addDelta( new TestDelta() );
  43. this.addDelta( new TestDelta() );
  44. } );
  45. const batch = new Batch();
  46. batch.foo();
  47. expect( batch.deltas.length ).to.equal( 2 );
  48. expect( batch.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  49. expect( batch.deltas[ 1 ] ).to.be.instanceof( TestDelta );
  50. } );
  51. it( 'should throw if one try to register the same batch twice', () => {
  52. register( 'foo', () => {} );
  53. expect( () => {
  54. register( 'foo', () => {} );
  55. } ).to.throw( CKEditorError, /^batch-register-taken/ );
  56. } );
  57. } );
  58. } );