8
0

weakinsertdelta.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, delta */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import Position from '/ckeditor5/engine/model/position.js';
  8. import WeakInsertDelta from '/ckeditor5/engine/model/delta/weakinsertdelta.js';
  9. describe( 'Batch', () => {
  10. let doc, root, batch, chain, attrs;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot();
  14. root.insertChildren( 0, '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.getChildCount() ).to.equal( 6 );
  23. expect( root.getChild( 2 ).character ).to.equal( 'x' );
  24. expect( root.getChild( 3 ).character ).to.equal( 'y' );
  25. expect( root.getChild( 4 ).character ).to.equal( 'z' );
  26. } );
  27. it( 'should set inserted nodes attributes to same as current selection attributes', () => {
  28. expect( Array.from( root.getChild( 2 )._attrs ) ).to.deep.equal( attrs );
  29. expect( Array.from( root.getChild( 3 )._attrs ) ).to.deep.equal( attrs );
  30. expect( Array.from( root.getChild( 4 )._attrs ) ).to.deep.equal( attrs );
  31. } );
  32. it( 'should be chainable', () => {
  33. expect( chain ).to.equal( batch );
  34. } );
  35. it( 'should add delta to batch and operation to delta before applying operation', () => {
  36. sinon.spy( doc, 'applyOperation' );
  37. batch.weakInsert( new Position( root, [ 2 ] ), 'xyz' );
  38. const correctDeltaMatcher = sinon.match( ( operation ) => {
  39. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  40. } );
  41. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  42. } );
  43. } );
  44. } );
  45. describe( 'WeakInsertDelta', ()=> {
  46. it( 'should provide proper className', () => {
  47. expect( WeakInsertDelta.className ).to.equal( 'engine.model.delta.WeakInsertDelta' );
  48. } );
  49. } );