removedelta.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { getNodesAndText } from '../../../tests/model/_utils/utils';
  6. import Document from '../../../src/model/document';
  7. import Position from '../../../src/model/position';
  8. import Range from '../../../src/model/range';
  9. import Element from '../../../src/model/element';
  10. import Text from '../../../src/model/text';
  11. import RemoveDelta from '../../../src/model/delta/removedelta';
  12. describe( 'Batch', () => {
  13. let doc, root, div, p, batch, chain, range;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot();
  17. div = new Element( 'div', [], new Text( 'foobar' ) );
  18. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  19. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  20. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  21. root.insertChildren( 0, [ div, p ] );
  22. batch = doc.batch();
  23. // Range starts in ROOT > DIV > P > gg|gg.
  24. // Range ends in ROOT > DIV > ...|ar.
  25. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  26. } );
  27. describe( 'remove', () => {
  28. it( 'should remove specified node', () => {
  29. batch.remove( div );
  30. expect( root.maxOffset ).to.equal( 1 );
  31. expect( root.childCount ).to.equal( 1 );
  32. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  33. } );
  34. it( 'should remove any range of nodes', () => {
  35. batch.remove( range );
  36. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  37. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  38. } );
  39. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  40. batch.remove( range );
  41. expect( batch.deltas.length ).to.equal( 2 );
  42. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  43. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  44. } );
  45. it( 'should be chainable', () => {
  46. chain = batch.remove( range );
  47. expect( chain ).to.equal( batch );
  48. } );
  49. it( 'should add delta to batch and operation to delta before applying operation', () => {
  50. sinon.spy( doc, 'applyOperation' );
  51. batch.remove( div );
  52. const correctDeltaMatcher = sinon.match( operation => {
  53. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  54. } );
  55. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  56. } );
  57. it( 'should create permanent RemoveOperation', () => {
  58. batch.remove( div, true );
  59. const operation = batch.deltas[ 0 ].operations[ 0 ];
  60. expect( operation.isPermanent ).to.be.true;
  61. } );
  62. } );
  63. } );
  64. describe( 'RemoveDelta', () => {
  65. it( 'should provide proper className', () => {
  66. expect( RemoveDelta.className ).to.equal( 'engine.model.delta.RemoveDelta' );
  67. } );
  68. } );