transaction.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 'document/transaction',
  9. 'document/delta/delta',
  10. 'ckeditorerror' );
  11. describe( 'Transaction', () => {
  12. let Transaction, Delta, CKEditorError;
  13. before( () => {
  14. Transaction = modules[ 'document/transaction' ];
  15. Delta = modules[ 'document/delta/delta' ];
  16. CKEditorError = modules.ckeditorerror;
  17. } );
  18. it( 'should have registered basic methods', () => {
  19. const transaction = new Transaction();
  20. expect( transaction.setAttr ).to.be.a( 'function' );
  21. expect( transaction.removeAttr ).to.be.a( 'function' );
  22. } );
  23. describe( 'Transaction.register', () => {
  24. let TestDelta;
  25. before( () => {
  26. TestDelta = class extends Delta {
  27. constructor( transaction ) {
  28. super( transaction, [] );
  29. }
  30. };
  31. } );
  32. afterEach( () => {
  33. delete Transaction.prototype.foo;
  34. } );
  35. it( 'should register function which return an delta', () => {
  36. Transaction.register( 'foo', ( doc, t ) => {
  37. t.addDelta( new TestDelta() );
  38. } );
  39. const transaction = new Transaction();
  40. transaction.foo();
  41. expect( transaction.deltas.length ).to.equal( 1 );
  42. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  43. } );
  44. it( 'should register function which return an multiple deltas', () => {
  45. Transaction.register( 'foo', ( doc, t ) => {
  46. t.addDelta( new TestDelta() );
  47. t.addDelta( new TestDelta() );
  48. } );
  49. const transaction = new Transaction();
  50. transaction.foo();
  51. expect( transaction.deltas.length ).to.equal( 2 );
  52. expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
  53. expect( transaction.deltas[ 1 ] ).to.be.instanceof( TestDelta );
  54. } );
  55. it( 'should pass arguments properly', () => {
  56. const doc = 'doc';
  57. const arg = 'arg';
  58. const transaction = new Transaction( doc );
  59. const stub = sinon.stub();
  60. Transaction.register( 'foo', stub );
  61. transaction.foo( arg );
  62. sinon.assert.calledWith( stub, doc, transaction, arg );
  63. } );
  64. it( 'should throw if one try to register the same transaction twice', () => {
  65. Transaction.register( 'foo', () => {} );
  66. expect( () => {
  67. Transaction.register( 'foo', () => {} );
  68. } ).to.throw( CKEditorError, /^transaction-register-taken/ );
  69. } );
  70. } );
  71. } );