8
0

movedelta.js 3.3 KB

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