insertdelta.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Element from '../../../src/model/element';
  7. import Position from '../../../src/model/position';
  8. import InsertOperation from '../../../src/model/operation/insertoperation';
  9. import InsertDelta from '../../../src/model/delta/insertdelta';
  10. import RemoveDelta from '../../../src/model/delta/removedelta';
  11. import RemoveOperation from '../../../src/model/operation/removeoperation';
  12. describe( 'InsertDelta', () => {
  13. let insertDelta, doc, root;
  14. beforeEach( () => {
  15. const model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. insertDelta = new InsertDelta();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create insert delta with no operations added', () => {
  22. expect( insertDelta.operations.length ).to.equal( 0 );
  23. } );
  24. } );
  25. describe( 'type', () => {
  26. it( 'should be equal to insert', () => {
  27. expect( insertDelta.type ).to.equal( 'insert' );
  28. } );
  29. } );
  30. describe( 'position', () => {
  31. it( 'should be null if there are no operations in delta', () => {
  32. expect( insertDelta.position ).to.be.null;
  33. } );
  34. it( 'should be equal to the position where node is inserted', () => {
  35. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  36. expect( insertDelta.position.root ).to.equal( root );
  37. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  38. } );
  39. } );
  40. describe( 'nodes', () => {
  41. it( 'should be null if there are no operations in delta', () => {
  42. expect( insertDelta.nodes ).to.be.null;
  43. } );
  44. it( 'should be equal to the nodes inserted by the delta', () => {
  45. const elementX = new Element( 'x' );
  46. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  47. expect( insertDelta.nodes.length ).to.equal( 1 );
  48. expect( insertDelta.nodes.getNode( 0 ) ).to.equal( elementX );
  49. } );
  50. } );
  51. describe( 'getReversed', () => {
  52. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  53. const reversed = insertDelta.getReversed();
  54. expect( reversed ).to.be.instanceof( RemoveDelta );
  55. expect( reversed.operations.length ).to.equal( 0 );
  56. } );
  57. it( 'should return correct RemoveDelta', () => {
  58. const position = new Position( root, [ 1, 2, 3 ] );
  59. const elementX = new Element( 'x' );
  60. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  61. const reversed = insertDelta.getReversed();
  62. expect( reversed ).to.be.instanceof( RemoveDelta );
  63. expect( reversed.operations.length ).to.equal( 1 );
  64. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  65. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  66. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  67. } );
  68. } );
  69. it( 'should provide proper className', () => {
  70. expect( InsertDelta.className ).to.equal( 'engine.model.delta.InsertDelta' );
  71. } );
  72. } );