batch.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document, delta */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/batch',
  9. 'treemodel/delta/delta',
  10. 'ckeditorerror'
  11. );
  12. describe( 'Batch', () => {
  13. let Batch, Delta, CKEditorError;
  14. before( () => {
  15. Batch = modules[ 'treemodel/batch' ];
  16. Delta = modules[ 'treemodel/delta/delta' ];
  17. CKEditorError = modules.ckeditorerror;
  18. } );
  19. it( 'should have registered basic methods', () => {
  20. const batch = new Batch();
  21. expect( batch.setAttr ).to.be.a( 'function' );
  22. expect( batch.removeAttr ).to.be.a( 'function' );
  23. } );
  24. describe( 'Batch.register', () => {
  25. let TestDelta;
  26. before( () => {
  27. TestDelta = class extends Delta {
  28. constructor( batch ) {
  29. super( batch, [] );
  30. }
  31. };
  32. } );
  33. afterEach( () => {
  34. delete Batch.prototype.foo;
  35. } );
  36. it( 'should register function which return an delta', () => {
  37. Batch.register( 'foo', function() {
  38. this.addDelta( new TestDelta() );
  39. } );
  40. const batch = new Batch();
  41. batch.foo();
  42. expect( batch.deltas.length ).to.equal( 1 );
  43. expect( batch.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  44. } );
  45. it( 'should register function which return an multiple deltas', () => {
  46. Batch.register( 'foo', function() {
  47. this.addDelta( new TestDelta() );
  48. this.addDelta( new TestDelta() );
  49. } );
  50. const batch = new Batch();
  51. batch.foo();
  52. expect( batch.deltas.length ).to.equal( 2 );
  53. expect( batch.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  54. expect( batch.deltas[ 1 ] ).to.be.instanceof( TestDelta );
  55. } );
  56. it( 'should throw if one try to register the same batch twice', () => {
  57. Batch.register( 'foo', () => {} );
  58. expect( () => {
  59. Batch.register( 'foo', () => {} );
  60. } ).to.throw( CKEditorError, /^batch-register-taken/ );
  61. } );
  62. } );
  63. } );