8
0

unwrapdelta.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 UnwrapDelta from '../../../src/model/delta/unwrapdelta';
  8. import WrapDelta from '../../../src/model/delta/wrapdelta';
  9. import MoveOperation from '../../../src/model/operation/moveoperation';
  10. import RemoveOperation from '../../../src/model/operation/removeoperation';
  11. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  12. describe( 'UnwrapDelta', () => {
  13. let unwrapDelta, doc, root;
  14. beforeEach( () => {
  15. const model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. unwrapDelta = new UnwrapDelta();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create unwrap delta with no operations added', () => {
  22. expect( unwrapDelta.operations.length ).to.equal( 0 );
  23. } );
  24. } );
  25. describe( 'type', () => {
  26. it( 'should be equal to unwrap', () => {
  27. expect( unwrapDelta.type ).to.equal( 'unwrap' );
  28. } );
  29. } );
  30. describe( 'position', () => {
  31. it( 'should be null if there are no operations in delta', () => {
  32. expect( unwrapDelta.position ).to.be.null;
  33. } );
  34. it( 'should be equal to the position before unwrapped node', () => {
  35. unwrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 2 ] ) ) );
  36. unwrapDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 6 ] ), 1, new Position( doc.graveyard, [ 0 ] ) ) );
  37. expect( unwrapDelta.position.root ).to.equal( root );
  38. expect( unwrapDelta.position.path ).to.deep.equal( [ 1, 2 ] );
  39. } );
  40. } );
  41. describe( 'getReversed', () => {
  42. it( 'should return empty WrapDelta if there are no operations in delta', () => {
  43. const reversed = unwrapDelta.getReversed();
  44. expect( reversed ).to.be.instanceof( WrapDelta );
  45. expect( reversed.operations.length ).to.equal( 0 );
  46. } );
  47. it( 'should return correct WrapDelta', () => {
  48. unwrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 2 ] ) ) );
  49. unwrapDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 6 ] ), 1, new Position( doc.graveyard, [ 0 ] ) ) );
  50. const reversed = unwrapDelta.getReversed();
  51. expect( reversed ).to.be.instanceof( WrapDelta );
  52. expect( reversed.operations.length ).to.equal( 2 );
  53. // WrapDelta which is an effect of reversing UnwrapDelta has ReinsertOperation instead of InsertOperation.
  54. // This is because we will "wrap" nodes into the element in which they were in the first place.
  55. // That element has been removed so we reinsert it from the graveyard.
  56. expect( reversed.operations[ 0 ] ).to.be.instanceof( ReinsertOperation );
  57. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  58. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 6 ] );
  59. expect( reversed.operations[ 1 ] ).to.be.instanceof( MoveOperation );
  60. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  61. expect( reversed.operations[ 1 ].howMany ).to.equal( 4 );
  62. expect( reversed.operations[ 1 ].targetPosition.path ).to.deep.equal( [ 1, 6, 0 ] );
  63. } );
  64. } );
  65. it( 'should provide proper className', () => {
  66. expect( UnwrapDelta.className ).to.equal( 'engine.model.delta.UnwrapDelta' );
  67. } );
  68. } );