reinsertoperation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  7. import RemoveOperation from '../../../src/model/operation/removeoperation';
  8. import MoveOperation from '../../../src/model/operation/moveoperation';
  9. import Position from '../../../src/model/position';
  10. import Element from '../../../src/model/element';
  11. import Text from '../../../src/model/text';
  12. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  13. describe( 'ReinsertOperation', () => {
  14. let doc, root, graveyard, operation, graveyardPosition, rootPosition;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. graveyard = doc.graveyard;
  19. graveyardPosition = new Position( graveyard, [ 0, 0 ] );
  20. rootPosition = new Position( root, [ 0 ] );
  21. operation = new ReinsertOperation(
  22. graveyardPosition,
  23. 2,
  24. rootPosition,
  25. doc.version
  26. );
  27. } );
  28. it( 'should have position property equal to the position where node will be reinserted', () => {
  29. expect( operation.position.isEqual( rootPosition ) ).to.be.true;
  30. // Setting also works:
  31. operation.position = new Position( root, [ 1 ] );
  32. expect( operation.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  33. } );
  34. it( 'should have proper type', () => {
  35. expect( operation.type ).to.equal( 'reinsert' );
  36. } );
  37. it( 'should not be sticky', () => {
  38. expect( operation.isSticky ).to.be.false;
  39. } );
  40. it( 'should extend MoveOperation class', () => {
  41. expect( operation ).to.be.instanceof( MoveOperation );
  42. } );
  43. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  44. let clone = operation.clone();
  45. expect( clone ).to.be.instanceof( ReinsertOperation );
  46. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  47. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  48. expect( clone.howMany ).to.equal( operation.howMany );
  49. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  50. } );
  51. it( 'should create a correct RemoveOperation as a reverse', () => {
  52. // Test reversed operation's target position.
  53. graveyard.appendChildren( new Element( '$graveyardHolder' ) );
  54. let reverse = operation.getReversed();
  55. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  56. expect( reverse.baseVersion ).to.equal( 1 );
  57. expect( reverse.howMany ).to.equal( 2 );
  58. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  59. // Reversed `ReinsertOperation` should target back to the same graveyard holder.
  60. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  61. // Reversed `ReinsertOperation` should not create new graveyard holder.
  62. expect( reverse._needsHolderElement ).to.be.false;
  63. } );
  64. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  65. let reverse = operation.getReversed();
  66. const element = new Element();
  67. element.insertChildren( 0, new Text( 'xx' ) );
  68. graveyard.insertChildren( 0, element );
  69. doc.applyOperation( wrapInDelta( operation ) );
  70. expect( doc.version ).to.equal( 1 );
  71. expect( root.maxOffset ).to.equal( 2 );
  72. expect( element.maxOffset ).to.equal( 0 );
  73. doc.applyOperation( wrapInDelta( reverse ) );
  74. expect( doc.version ).to.equal( 2 );
  75. expect( root.maxOffset ).to.equal( 0 );
  76. // Don't check `element` - nodes are moved to new holder element.
  77. } );
  78. describe( 'toJSON', () => {
  79. it( 'should create proper json object', () => {
  80. const serialized = jsonParseStringify( operation );
  81. expect( serialized ).to.deep.equal( {
  82. __className: 'engine.model.operation.ReinsertOperation',
  83. baseVersion: 0,
  84. howMany: 2,
  85. isSticky: false,
  86. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  87. targetPosition: jsonParseStringify( operation.targetPosition )
  88. } );
  89. } );
  90. } );
  91. describe( 'fromJSON', () => {
  92. it( 'should create proper ReinsertOperation from json object', () => {
  93. const serialized = jsonParseStringify( operation );
  94. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  95. expect( deserialized ).to.deep.equal( operation );
  96. } );
  97. } );
  98. } );