8
0

splitdelta.js 4.6 KB

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