8
0

reinsertoperation.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import ReinsertOperation from '/ckeditor5/core/treemodel/operation/reinsertoperation.js';
  9. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  10. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. describe( 'ReinsertOperation', () => {
  13. let doc, root, graveyard, operation, graveyardPosition, rootPosition;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. graveyard = doc.graveyard;
  18. graveyardPosition = new Position( graveyard, [ 0 ] );
  19. rootPosition = new Position( root, [ 0 ] );
  20. operation = new ReinsertOperation(
  21. graveyardPosition,
  22. 2,
  23. rootPosition,
  24. doc.version
  25. );
  26. } );
  27. it( 'should have position property equal to the position where node will be reinserted', () => {
  28. expect( operation.position.isEqual( rootPosition ) ).to.be.true;
  29. // Setting also works:
  30. operation.position = new Position( root, [ 1 ] );
  31. expect( operation.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  32. } );
  33. it( 'should have proper type', () => {
  34. expect( operation.type ).to.equal( 'reinsert' );
  35. } );
  36. it( 'should not be sticky', () => {
  37. expect( operation.isSticky ).to.be.false;
  38. } );
  39. it( 'should extend MoveOperation class', () => {
  40. expect( operation ).to.be.instanceof( MoveOperation );
  41. } );
  42. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  43. let clone = operation.clone();
  44. expect( clone ).to.be.instanceof( ReinsertOperation );
  45. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  46. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  47. expect( clone.howMany ).to.equal( operation.howMany );
  48. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  49. } );
  50. it( 'should create a RemoveOperation as a reverse', () => {
  51. let reverse = operation.getReversed();
  52. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  53. expect( reverse.baseVersion ).to.equal( 1 );
  54. expect( reverse.howMany ).to.equal( 2 );
  55. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  56. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  57. } );
  58. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  59. let reverse = operation.getReversed();
  60. graveyard.insertChildren( 0, 'bar' );
  61. doc.applyOperation( operation );
  62. expect( doc.version ).to.equal( 1 );
  63. expect( root.getChildCount() ).to.equal( 2 );
  64. doc.applyOperation( reverse );
  65. expect( doc.version ).to.equal( 2 );
  66. expect( root.getChildCount() ).to.equal( 0 );
  67. expect( graveyard.getChildCount() ).to.equal( 3 );
  68. expect( graveyard.getChild( 0 ).character ).to.equal( 'b' );
  69. expect( graveyard.getChild( 1 ).character ).to.equal( 'a' );
  70. expect( graveyard.getChild( 2 ).character ).to.equal( 'r' );
  71. } );
  72. } );