insertdelta.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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. doc = new Document();
  16. root = doc.createRoot();
  17. insertDelta = new InsertDelta();
  18. } );
  19. describe( 'constructor()', () => {
  20. it( 'should create insert delta with no operations added', () => {
  21. expect( insertDelta.operations.length ).to.equal( 0 );
  22. } );
  23. } );
  24. describe( 'type', () => {
  25. it( 'should be equal to insert', () => {
  26. expect( insertDelta.type ).to.equal( 'insert' );
  27. } );
  28. } );
  29. describe( 'position', () => {
  30. it( 'should be null if there are no operations in delta', () => {
  31. expect( insertDelta.position ).to.be.null;
  32. } );
  33. it( 'should be equal to the position where node is inserted', () => {
  34. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  35. expect( insertDelta.position.root ).to.equal( root );
  36. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  37. } );
  38. } );
  39. describe( 'nodes', () => {
  40. it( 'should be null if there are no operations in delta', () => {
  41. expect( insertDelta.nodes ).to.be.null;
  42. } );
  43. it( 'should be equal to the nodes inserted by the delta', () => {
  44. const elementX = new Element( 'x' );
  45. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  46. expect( insertDelta.nodes.length ).to.equal( 1 );
  47. expect( insertDelta.nodes.getNode( 0 ) ).to.equal( elementX );
  48. } );
  49. } );
  50. describe( 'getReversed', () => {
  51. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  52. const reversed = insertDelta.getReversed();
  53. expect( reversed ).to.be.instanceof( RemoveDelta );
  54. expect( reversed.operations.length ).to.equal( 0 );
  55. } );
  56. it( 'should return correct RemoveDelta', () => {
  57. const position = new Position( root, [ 1, 2, 3 ] );
  58. const elementX = new Element( 'x' );
  59. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  60. const reversed = insertDelta.getReversed();
  61. expect( reversed ).to.be.instanceof( RemoveDelta );
  62. expect( reversed.operations.length ).to.equal( 1 );
  63. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  64. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  65. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  66. } );
  67. } );
  68. it( 'should provide proper className', () => {
  69. expect( InsertDelta.className ).to.equal( 'engine.model.delta.InsertDelta' );
  70. } );
  71. } );