weakinsertdelta.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 Position from '../../../src/model/position';
  7. import Text from '../../../src/model/text';
  8. import WeakInsertDelta from '../../../src/model/delta/weakinsertdelta';
  9. describe( 'Batch', () => {
  10. let doc, root, batch, chain, attrs;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot();
  14. root.insertChildren( 0, new Text( 'abc' ) );
  15. batch = doc.batch();
  16. attrs = [ [ 'bold', true ], [ 'foo', 'bar' ] ];
  17. doc.selection.setAttributesTo( attrs );
  18. chain = batch.weakInsert( new Position( root, [ 2 ] ), 'xyz' );
  19. } );
  20. describe( 'weakInsert', () => {
  21. it( 'should insert given nodes at given position', () => {
  22. expect( root.maxOffset ).to.equal( 6 );
  23. expect( root.getChild( 0 ).data ).to.equal( 'ab' );
  24. expect( root.getChild( 1 ).data ).to.equal( 'xyz' );
  25. expect( root.getChild( 2 ).data ).to.equal( 'c' );
  26. } );
  27. it( 'should set inserted nodes attributes to same as current selection attributes', () => {
  28. expect( Array.from( root.getChild( 1 ).getAttributes() ) ).to.deep.equal( attrs );
  29. } );
  30. it( 'should be chainable', () => {
  31. expect( chain ).to.equal( batch );
  32. } );
  33. it( 'should add delta to batch and operation to delta before applying operation', () => {
  34. sinon.spy( doc, 'applyOperation' );
  35. batch.weakInsert( new Position( root, [ 2 ] ), 'xyz' );
  36. const correctDeltaMatcher = sinon.match( operation => {
  37. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  38. } );
  39. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  40. } );
  41. } );
  42. } );
  43. describe( 'WeakInsertDelta', () => {
  44. it( 'should provide proper className', () => {
  45. expect( WeakInsertDelta.className ).to.equal( 'engine.model.delta.WeakInsertDelta' );
  46. } );
  47. } );