weakinsertdelta.js 2.0 KB

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