insertdelta.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Element from '../../../src/model/element';
  7. import Position from '../../../src/model/position';
  8. import Text from '../../../src/model/text';
  9. import InsertOperation from '../../../src/model/operation/insertoperation';
  10. import InsertDelta from '../../../src/model/delta/insertdelta';
  11. import RemoveDelta from '../../../src/model/delta/removedelta';
  12. import RemoveOperation from '../../../src/model/operation/removeoperation';
  13. describe( 'Batch', () => {
  14. let doc, root, batch, p, ul, chain;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. root.insertChildren( 0, new Text( 'abc' ) );
  19. batch = doc.batch();
  20. p = new Element( 'p' );
  21. ul = new Element( 'ul' );
  22. chain = batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  23. } );
  24. describe( 'insert', () => {
  25. it( 'should insert given nodes at given position', () => {
  26. expect( root.childCount ).to.equal( 4 );
  27. expect( root.maxOffset ).to.equal( 5 );
  28. expect( root.getChild( 1 ) ).to.equal( p );
  29. expect( root.getChild( 2 ) ).to.equal( ul );
  30. } );
  31. it( 'should be chainable', () => {
  32. expect( chain ).to.equal( batch );
  33. } );
  34. it( 'should add delta to batch and operation to delta before applying operation', () => {
  35. sinon.spy( doc, 'applyOperation' );
  36. batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  37. const correctDeltaMatcher = sinon.match( ( operation ) => {
  38. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  39. } );
  40. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  41. } );
  42. } );
  43. } );
  44. describe( 'InsertDelta', () => {
  45. let insertDelta, doc, root;
  46. beforeEach( () => {
  47. doc = new Document();
  48. root = doc.createRoot();
  49. insertDelta = new InsertDelta();
  50. } );
  51. describe( 'constructor()', () => {
  52. it( 'should create insert delta with no operations added', () => {
  53. expect( insertDelta.operations.length ).to.equal( 0 );
  54. } );
  55. } );
  56. describe( 'type', () => {
  57. it( 'should be equal to insert', () => {
  58. expect( insertDelta.type ).to.equal( 'insert' );
  59. } );
  60. } );
  61. describe( 'position', () => {
  62. it( 'should be null if there are no operations in delta', () => {
  63. expect( insertDelta.position ).to.be.null;
  64. } );
  65. it( 'should be equal to the position where node is inserted', () => {
  66. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  67. expect( insertDelta.position.root ).to.equal( root );
  68. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  69. } );
  70. } );
  71. describe( 'nodes', () => {
  72. it( 'should be null if there are no operations in delta', () => {
  73. expect( insertDelta.nodes ).to.be.null;
  74. } );
  75. it( 'should be equal to the nodes inserted by the delta', () => {
  76. let elementX = new Element( 'x' );
  77. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  78. expect( insertDelta.nodes.length ).to.equal( 1 );
  79. expect( insertDelta.nodes.getNode( 0 ) ).to.equal( elementX );
  80. } );
  81. } );
  82. describe( 'getReversed', () => {
  83. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  84. let reversed = insertDelta.getReversed();
  85. expect( reversed ).to.be.instanceof( RemoveDelta );
  86. expect( reversed.operations.length ).to.equal( 0 );
  87. } );
  88. it( 'should return correct RemoveDelta', () => {
  89. let position = new Position( root, [ 1, 2, 3 ] );
  90. let elementX = new Element( 'x' );
  91. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  92. let reversed = insertDelta.getReversed();
  93. expect( reversed ).to.be.instanceof( RemoveDelta );
  94. expect( reversed.operations.length ).to.equal( 1 );
  95. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  96. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  97. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  98. } );
  99. } );
  100. it( 'should provide proper className', () => {
  101. expect( InsertDelta.className ).to.equal( 'engine.model.delta.InsertDelta' );
  102. } );
  103. } );