transaction.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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', function() {
  37. this.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', function() {
  46. this.addDelta( new TestDelta() );
  47. this.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 throw if one try to register the same transaction twice', () => {
  56. Transaction.register( 'foo', () => {} );
  57. expect( () => {
  58. Transaction.register( 'foo', () => {} );
  59. } ).to.throw( CKEditorError, /^transaction-register-taken/ );
  60. } );
  61. } );
  62. } );