8
0

batch.js 1.8 KB

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