8
0

wrapdelta.js 4.1 KB

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