transaction.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. var modules = bender.amd.require(
  8. 'document/transaction',
  9. 'document/deltas/delta' );
  10. describe( 'Transaction', () => {
  11. var Transaction, Delta;
  12. before( () => {
  13. Transaction = modules[ 'document/transaction' ];
  14. Delta = modules[ 'document/deltas/delta' ];
  15. } );
  16. it( 'should have registered basic methods', () => {
  17. var transaction = new Transaction();
  18. expect( transaction.setAttr ).to.be.a( 'function' );
  19. expect( transaction.removeAttr ).to.be.a( 'function' );
  20. } );
  21. describe( 'Transaction.register', () => {
  22. var executeCount = 0;
  23. var TestDelta;
  24. before( () => {
  25. TestDelta = class extends Delta {
  26. constructor( transaction ) {
  27. super( transaction, [] );
  28. }
  29. _execute() {
  30. executeCount++;
  31. }
  32. };
  33. } );
  34. afterEach( () => {
  35. delete Transaction.prototype.foo;
  36. executeCount = 0;
  37. } );
  38. it( 'should register function which return an delta', () => {
  39. Transaction.register( 'foo', ( doc, t ) => {
  40. return new TestDelta( t );
  41. } );
  42. var transaction = new Transaction();
  43. transaction.foo();
  44. expect( transaction.deltas.length ).to.equal( 1 );
  45. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  46. expect( executeCount ).to.equal( 1 );
  47. } );
  48. it( 'should register function which return an multiple deltas', () => {
  49. Transaction.register( 'foo', ( doc, transaction ) => {
  50. return [ new TestDelta( transaction ), new TestDelta( transaction ) ];
  51. } );
  52. var transaction = new Transaction();
  53. transaction.foo();
  54. expect( transaction.deltas.length ).to.equal( 2 );
  55. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  56. expect( transaction.deltas[ 1 ] ).to.be.instanceof( TestDelta );
  57. expect( executeCount ).to.equal( 2 );
  58. } );
  59. } );
  60. } );