splitdelta.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Element from '../../../src/model/element';
  8. import MergeDelta from '../../../src/model/delta/mergedelta';
  9. import SplitDelta from '../../../src/model/delta/splitdelta';
  10. import InsertOperation from '../../../src/model/operation/insertoperation';
  11. import MoveOperation from '../../../src/model/operation/moveoperation';
  12. import NoOperation from '../../../src/model/operation/nooperation';
  13. import RemoveOperation from '../../../src/model/operation/removeoperation';
  14. describe( 'SplitDelta', () => {
  15. let splitDelta, doc, root;
  16. beforeEach( () => {
  17. const model = new Model();
  18. doc = model.document;
  19. root = doc.createRoot();
  20. splitDelta = new SplitDelta();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should create split delta with no operations added', () => {
  24. expect( splitDelta.operations.length ).to.equal( 0 );
  25. } );
  26. } );
  27. describe( 'type', () => {
  28. it( 'should be equal to split', () => {
  29. expect( splitDelta.type ).to.equal( 'split' );
  30. } );
  31. } );
  32. describe( 'position', () => {
  33. it( 'should be null if there are no operations in delta', () => {
  34. expect( splitDelta.position ).to.be.null;
  35. } );
  36. it( 'should be equal to the position where node is split', () => {
  37. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  38. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  39. expect( splitDelta.position.root ).to.equal( root );
  40. expect( splitDelta.position.path ).to.deep.equal( [ 1, 1, 4 ] );
  41. } );
  42. } );
  43. describe( 'getReversed', () => {
  44. it( 'should return empty MergeDelta if there are no operations in delta', () => {
  45. const reversed = splitDelta.getReversed();
  46. expect( reversed ).to.be.instanceof( MergeDelta );
  47. expect( reversed.operations.length ).to.equal( 0 );
  48. } );
  49. it( 'should return correct SplitDelta', () => {
  50. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  51. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  52. const reversed = splitDelta.getReversed();
  53. expect( reversed ).to.be.instanceof( MergeDelta );
  54. expect( reversed.operations.length ).to.equal( 2 );
  55. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  56. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  57. expect( reversed.operations[ 0 ].howMany ).to.equal( 4 );
  58. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  59. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  60. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  61. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  62. } );
  63. } );
  64. describe( '_cloneOperation', () => {
  65. it( 'should return null if delta has no operations', () => {
  66. expect( splitDelta._cloneOperation ).to.be.null;
  67. } );
  68. it( 'should return the first operation in the delta, which is InsertOperation or ReinsertOperation', () => {
  69. const p = new Element( 'p' );
  70. const insert = new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 );
  71. splitDelta.operations.push( insert );
  72. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  73. expect( splitDelta._cloneOperation ).to.equal( insert );
  74. } );
  75. } );
  76. describe( '_moveOperation', () => {
  77. it( 'should return null if delta has no operations', () => {
  78. expect( splitDelta._moveOperation ).to.be.null;
  79. } );
  80. it( 'should return null if second operation is NoOperation', () => {
  81. const p = new Element( 'p' );
  82. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  83. splitDelta.operations.push( new NoOperation( 1 ) );
  84. expect( splitDelta._moveOperation ).to.be.null;
  85. } );
  86. it( 'should return second operation if it is MoveOperation', () => {
  87. const p = new Element( 'p' );
  88. const move = new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 );
  89. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  90. splitDelta.operations.push( move );
  91. expect( splitDelta._moveOperation ).to.equal( move );
  92. } );
  93. } );
  94. it( 'should provide proper className', () => {
  95. expect( SplitDelta.className ).to.equal( 'engine.model.delta.SplitDelta' );
  96. } );
  97. } );