reinsertoperation.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 proper type', () => {
  28. expect( operation.type ).to.equal( 'reinsert' );
  29. } );
  30. it( 'should not be sticky', () => {
  31. expect( operation.isSticky ).to.be.false;
  32. } );
  33. it( 'should extend MoveOperation class', () => {
  34. expect( operation ).to.be.instanceof( MoveOperation );
  35. } );
  36. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  37. let clone = operation.clone();
  38. expect( clone ).to.be.instanceof( ReinsertOperation );
  39. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  40. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  41. expect( clone.howMany ).to.equal( operation.howMany );
  42. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  43. } );
  44. it( 'should create a RemoveOperation as a reverse', () => {
  45. let reverse = operation.getReversed();
  46. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  47. expect( reverse.baseVersion ).to.equal( 1 );
  48. expect( reverse.howMany ).to.equal( 2 );
  49. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  50. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  51. } );
  52. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  53. let reverse = operation.getReversed();
  54. graveyard.insertChildren( 0, 'bar' );
  55. doc.applyOperation( operation );
  56. expect( doc.version ).to.equal( 1 );
  57. expect( root.getChildCount() ).to.equal( 2 );
  58. doc.applyOperation( reverse );
  59. expect( doc.version ).to.equal( 2 );
  60. expect( root.getChildCount() ).to.equal( 0 );
  61. expect( graveyard.getChildCount() ).to.equal( 3 );
  62. expect( graveyard.getChild( 0 ).character ).to.equal( 'b' );
  63. expect( graveyard.getChild( 1 ).character ).to.equal( 'a' );
  64. expect( graveyard.getChild( 2 ).character ).to.equal( 'r' );
  65. } );
  66. } );