reinsertoperation.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ] );
  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. const 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. graveyard.appendChildren( new Element( 'x' ) );
  53. const reverse = operation.getReversed();
  54. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  55. expect( reverse.baseVersion ).to.equal( 1 );
  56. expect( reverse.howMany ).to.equal( 2 );
  57. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  58. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  59. } );
  60. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  61. const reverse = operation.getReversed();
  62. graveyard.insertChildren( 0, new Text( 'xx' ) );
  63. doc.applyOperation( wrapInDelta( operation ) );
  64. expect( doc.version ).to.equal( 1 );
  65. expect( root.maxOffset ).to.equal( 2 );
  66. expect( graveyard.maxOffset ).to.equal( 0 );
  67. doc.applyOperation( wrapInDelta( reverse ) );
  68. expect( doc.version ).to.equal( 2 );
  69. expect( root.maxOffset ).to.equal( 0 );
  70. expect( graveyard.maxOffset ).to.equal( 2 );
  71. } );
  72. describe( 'toJSON', () => {
  73. it( 'should create proper json object', () => {
  74. const serialized = jsonParseStringify( operation );
  75. expect( serialized ).to.deep.equal( {
  76. __className: 'engine.model.operation.ReinsertOperation',
  77. baseVersion: 0,
  78. howMany: 2,
  79. isSticky: false,
  80. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  81. targetPosition: jsonParseStringify( operation.targetPosition )
  82. } );
  83. } );
  84. } );
  85. describe( 'fromJSON', () => {
  86. it( 'should create proper ReinsertOperation from json object', () => {
  87. const serialized = jsonParseStringify( operation );
  88. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  89. expect( deserialized ).to.deep.equal( operation );
  90. } );
  91. } );
  92. } );