delta.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import utils from '/ckeditor5/utils/utils.js';
  8. import Delta from '/ckeditor5/core/treemodel/delta/delta.js';
  9. import Operation from '/ckeditor5/core/treemodel/operation/operation.js';
  10. // Some test examples of operations.
  11. class FooOperation extends Operation {
  12. constructor( string, baseVersion ) {
  13. super( baseVersion );
  14. this.string = string;
  15. }
  16. getReversed() {
  17. /* jshint ignore:start */
  18. return new BarOperation( this.string, this.baseVersion );
  19. /* jshint ignore:end */
  20. }
  21. }
  22. class BarOperation extends FooOperation {
  23. getReversed() {
  24. return new FooOperation( this.string, this.baseVersion );
  25. }
  26. }
  27. describe( 'Delta', () => {
  28. describe( 'constructor', () => {
  29. it( 'should create an delta with empty properties', () => {
  30. const delta = new Delta();
  31. expect( delta ).to.have.property( 'batch' ).that.is.null;
  32. expect( delta ).to.have.property( 'operations' ).that.a( 'array' ).and.have.length( 0 );
  33. } );
  34. } );
  35. describe( 'addOperation', () => {
  36. it( 'should add operation to the delta', () => {
  37. const delta = new Delta();
  38. const operation = {};
  39. delta.addOperation( operation );
  40. expect( delta.operations ).to.have.length( 1 );
  41. expect( delta.operations[ 0 ] ).to.equal( operation );
  42. } );
  43. it( 'should add delta property to the operation', () => {
  44. const delta = new Delta();
  45. const operation = {};
  46. delta.addOperation( operation );
  47. expect( operation.delta ).to.equal( delta );
  48. } );
  49. } );
  50. describe( 'iterator', () => {
  51. it( 'should iterate over delta operations', () => {
  52. const delta = new Delta();
  53. delta.addOperation( {} );
  54. delta.addOperation( {} );
  55. delta.addOperation( {} );
  56. const count = utils.count( delta.operations );
  57. expect( count ).to.equal( 3 );
  58. } );
  59. } );
  60. describe( 'getReversed', () => {
  61. it( 'should return empty Delta if there are no operations in delta', () => {
  62. const delta = new Delta();
  63. let reversed = delta.getReversed();
  64. expect( reversed ).to.be.instanceof( Delta );
  65. expect( reversed.operations.length ).to.equal( 0 );
  66. } );
  67. it( 'should return Delta with all operations reversed and their order reversed', () => {
  68. const delta = new Delta();
  69. delta.addOperation( new FooOperation( 'a', 1 ) );
  70. delta.addOperation( new BarOperation( 'b', 2 ) );
  71. let reversed = delta.getReversed();
  72. expect( reversed ).to.be.instanceof( Delta );
  73. expect( reversed.operations.length ).to.equal( 2 );
  74. expect( reversed.operations[ 0 ] ).to.be.instanceOf( FooOperation );
  75. expect( reversed.operations[ 0 ].string ).to.equal( 'b' );
  76. expect( reversed.operations[ 1 ] ).to.be.instanceOf( BarOperation );
  77. expect( reversed.operations[ 1 ].string ).to.equal( 'a' );
  78. } );
  79. } );
  80. } );