delta.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transformations from '../../../../src/model/delta/basic-transformations'; // eslint-disable-line no-unused-vars
  6. import deltaTransform from '../../../../src/model/delta/transform';
  7. const transform = deltaTransform.transform;
  8. import Position from '../../../../src/model/position';
  9. import MoveOperation from '../../../../src/model/operation/moveoperation';
  10. import Delta from '../../../../src/model/delta/delta';
  11. import {
  12. expectDelta,
  13. getFilledDocument,
  14. } from '../../../../tests/model/delta/transform/_utils/utils';
  15. describe( 'Delta', () => {
  16. let doc, root, baseVersion;
  17. beforeEach( () => {
  18. doc = getFilledDocument();
  19. root = doc.getRoot();
  20. baseVersion = doc.version;
  21. } );
  22. it( 'should have baseVersion property, equal to the baseVersion of first operation in Delta or null', () => {
  23. const deltaA = new Delta();
  24. expect( deltaA.baseVersion ).to.be.null;
  25. const version = 5;
  26. deltaA.addOperation( new MoveOperation( new Position( root, [ 1, 2, 3 ] ), 4, new Position( root, [ 4, 0 ] ), version ) );
  27. expect( deltaA.baseVersion ).to.equal( 5 );
  28. } );
  29. it( 'should be transformable by another Delta', () => {
  30. const deltaA = new Delta();
  31. const deltaB = new Delta();
  32. const context = {
  33. isStrong: false
  34. };
  35. deltaA.addOperation( new MoveOperation( new Position( root, [ 1, 2, 3 ] ), 4, new Position( root, [ 4, 0 ] ), baseVersion ) );
  36. deltaB.addOperation( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 2, new Position( root, [ 4, 1 ] ), baseVersion ) );
  37. const deltaAbyB = transform( deltaA, deltaB, context );
  38. const deltaBbyA = transform( deltaB, deltaA, context );
  39. expect( deltaAbyB.length ).to.equal( 1 );
  40. expectDelta( deltaAbyB[ 0 ], {
  41. type: Delta,
  42. operations: [
  43. {
  44. type: MoveOperation,
  45. sourcePosition: new Position( root, [ 1, 2, 1 ] ),
  46. howMany: 4,
  47. targetPosition: new Position( root, [ 4, 0 ] ),
  48. baseVersion: 1
  49. }
  50. ]
  51. } );
  52. expect( deltaBbyA.length ).to.equal( 1 );
  53. expectDelta( deltaBbyA[ 0 ], {
  54. type: Delta,
  55. operations: [
  56. {
  57. type: MoveOperation,
  58. sourcePosition: new Position( root, [ 1, 2, 0 ] ),
  59. howMany: 2,
  60. targetPosition: new Position( root, [ 4, 5 ] ),
  61. baseVersion: 1
  62. }
  63. ]
  64. } );
  65. } );
  66. } );