transaction.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 TestDelta;
  23. before( () => {
  24. TestDelta = class extends Delta {
  25. constructor( transaction ) {
  26. super( transaction, [] );
  27. }
  28. };
  29. } );
  30. afterEach( () => {
  31. delete Transaction.prototype.foo;
  32. } );
  33. it( 'should register function which return an delta', () => {
  34. Transaction.register( 'foo', ( doc, t ) => {
  35. t.addDelta( new TestDelta() );
  36. } );
  37. var transaction = new Transaction();
  38. transaction.foo();
  39. expect( transaction.deltas.length ).to.equal( 1 );
  40. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  41. } );
  42. it( 'should register function which return an multiple deltas', () => {
  43. Transaction.register( 'foo', ( doc, t ) => {
  44. t.addDelta( new TestDelta() );
  45. t.addDelta( new TestDelta() );
  46. } );
  47. var transaction = new Transaction();
  48. transaction.foo();
  49. expect( transaction.deltas.length ).to.equal( 2 );
  50. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  51. expect( transaction.deltas[ 1 ] ).to.be.instanceof( TestDelta );
  52. } );
  53. it( 'should pass arguments properly', () => {
  54. var doc = 'doc';
  55. var arg = 'arg';
  56. var transaction = new Transaction( doc );
  57. var stub = sinon.stub().returns( new TestDelta( transaction ) );
  58. Transaction.register( 'foo', stub );
  59. transaction.foo( arg );
  60. sinon.assert.calledWith( stub, doc, transaction, arg );
  61. } );
  62. } );
  63. } );