8
0

wrapdelta.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 WrapDelta from '../../../src/model/delta/wrapdelta';
  9. import UnwrapDelta from '../../../src/model/delta/unwrapdelta';
  10. import InsertOperation from '../../../src/model/operation/insertoperation';
  11. import MoveOperation from '../../../src/model/operation/moveoperation';
  12. import RemoveOperation from '../../../src/model/operation/removeoperation';
  13. describe( 'WrapDelta', () => {
  14. let wrapDelta, doc, root;
  15. beforeEach( () => {
  16. const model = new Model();
  17. doc = model.document;
  18. root = doc.createRoot();
  19. wrapDelta = new WrapDelta();
  20. } );
  21. describe( 'constructor()', () => {
  22. it( 'should create wrap delta with no operations added', () => {
  23. expect( wrapDelta.operations.length ).to.equal( 0 );
  24. } );
  25. } );
  26. describe( 'type', () => {
  27. it( 'should be equal to wrap', () => {
  28. expect( wrapDelta.type ).to.equal( 'wrap' );
  29. } );
  30. } );
  31. describe( 'range', () => {
  32. it( 'should be equal to null if there are no operations in delta', () => {
  33. expect( wrapDelta.range ).to.be.null;
  34. } );
  35. it( 'should be equal to wrapped range', () => {
  36. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 ) );
  37. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  38. expect( wrapDelta.range.start.isEqual( new Position( root, [ 1, 1 ] ) ) ).to.be.true;
  39. expect( wrapDelta.range.end.isEqual( new Position( root, [ 1, 6 ] ) ) ).to.be.true;
  40. } );
  41. } );
  42. describe( 'howMany', () => {
  43. it( 'should be equal to 0 if there are no operations in delta', () => {
  44. expect( wrapDelta.howMany ).to.equal( 0 );
  45. } );
  46. it( 'should be equal to the number of wrapped elements', () => {
  47. const howMany = 5;
  48. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 ) );
  49. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), howMany, new Position( root, [ 1, 6, 0 ] ) ) );
  50. expect( wrapDelta.howMany ).to.equal( 5 );
  51. } );
  52. } );
  53. describe( 'getReversed', () => {
  54. it( 'should return empty UnwrapDelta if there are no operations in delta', () => {
  55. const reversed = wrapDelta.getReversed();
  56. expect( reversed ).to.be.instanceof( UnwrapDelta );
  57. expect( reversed.operations.length ).to.equal( 0 );
  58. } );
  59. it( 'should return correct UnwrapDelta', () => {
  60. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), new Element( 'p' ), 1 ) );
  61. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  62. const reversed = wrapDelta.getReversed();
  63. expect( reversed ).to.be.instanceof( UnwrapDelta );
  64. expect( reversed.operations.length ).to.equal( 2 );
  65. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  66. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 1, 0 ] );
  67. expect( reversed.operations[ 0 ].howMany ).to.equal( 5 );
  68. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  69. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  70. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 6 ] );
  71. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  72. } );
  73. } );
  74. describe( '_insertOperation', () => {
  75. it( 'should be null if there are no operations in the delta', () => {
  76. expect( wrapDelta._insertOperation ).to.be.null;
  77. } );
  78. it( 'should be equal to the first operation in the delta', () => {
  79. const insertOperation = new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 );
  80. wrapDelta.operations.push( insertOperation );
  81. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  82. expect( wrapDelta._insertOperation ).to.equal( insertOperation );
  83. } );
  84. } );
  85. it( 'should provide proper className', () => {
  86. expect( WrapDelta.className ).to.equal( 'engine.model.delta.WrapDelta' );
  87. } );
  88. } );