8
0

unwrapdelta.js 3.3 KB

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