8
0

movedelta.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Position from '../../../src/model/position';
  7. import MoveDelta from '../../../src/model/delta/movedelta';
  8. import MoveOperation from '../../../src/model/operation/moveoperation';
  9. describe( 'MoveDelta', () => {
  10. let moveDelta, doc, root;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot();
  14. moveDelta = new MoveDelta();
  15. } );
  16. describe( 'constructor()', () => {
  17. it( 'should create move delta with no operations added', () => {
  18. expect( moveDelta.operations.length ).to.equal( 0 );
  19. } );
  20. } );
  21. describe( 'type', () => {
  22. it( 'should be equal to move', () => {
  23. expect( moveDelta.type ).to.equal( 'move' );
  24. } );
  25. } );
  26. describe( 'sourcePosition', () => {
  27. it( 'should be null if there are no operations in delta', () => {
  28. expect( moveDelta.sourcePosition ).to.be.null;
  29. } );
  30. it( 'should be equal to the position where node is inserted', () => {
  31. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  32. expect( moveDelta.sourcePosition.root ).to.equal( root );
  33. expect( moveDelta.sourcePosition.path ).to.deep.equal( [ 1, 1 ] );
  34. } );
  35. } );
  36. describe( 'howMany', () => {
  37. it( 'should be null if there are no operations in delta', () => {
  38. expect( moveDelta.howMany ).to.be.null;
  39. } );
  40. it( 'should be equal to the position where node is inserted', () => {
  41. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  42. expect( moveDelta.howMany ).to.equal( 2 );
  43. } );
  44. } );
  45. describe( 'targetPosition', () => {
  46. it( 'should be null if there are no operations in delta', () => {
  47. expect( moveDelta.targetPosition ).to.be.null;
  48. } );
  49. it( 'should be equal to the move operation\'s target position', () => {
  50. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  51. expect( moveDelta.targetPosition.root ).to.equal( root );
  52. expect( moveDelta.targetPosition.path ).to.deep.equal( [ 2, 2 ] );
  53. } );
  54. } );
  55. describe( 'getReversed', () => {
  56. it( 'should return empty MoveDelta if there are no operations in delta', () => {
  57. const reversed = moveDelta.getReversed();
  58. expect( reversed ).to.be.instanceof( MoveDelta );
  59. expect( reversed.operations.length ).to.equal( 0 );
  60. } );
  61. it( 'should return correct MoveDelta', () => {
  62. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  63. const reversed = moveDelta.getReversed();
  64. expect( reversed ).to.be.instanceof( MoveDelta );
  65. expect( reversed.operations.length ).to.equal( 1 );
  66. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  67. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 2, 2 ] );
  68. expect( reversed.operations[ 0 ].howMany ).to.equal( 2 );
  69. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  70. } );
  71. } );
  72. it( 'should provide proper className', () => {
  73. expect( MoveDelta.className ).to.equal( 'engine.model.delta.MoveDelta' );
  74. } );
  75. } );