8
0

weakinsertdelta.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Position from '/ckeditor5/engine/model/position.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( 'root' );
  15. root.insertChildren( 0, '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.getChildCount() ).to.equal( 6 );
  24. expect( root.getChild( 2 ).character ).to.equal( 'x' );
  25. expect( root.getChild( 3 ).character ).to.equal( 'y' );
  26. expect( root.getChild( 4 ).character ).to.equal( 'z' );
  27. } );
  28. it( 'should set inserted nodes attributes to same as current selection attributes', () => {
  29. expect( Array.from( root.getChild( 2 )._attrs ) ).to.deep.equal( attrs );
  30. expect( Array.from( root.getChild( 3 )._attrs ) ).to.deep.equal( attrs );
  31. expect( Array.from( root.getChild( 4 )._attrs ) ).to.deep.equal( attrs );
  32. } );
  33. it( 'should be chainable', () => {
  34. expect( chain ).to.equal( batch );
  35. } );
  36. it( 'should add delta to batch and operation to delta before applying operation', () => {
  37. sinon.spy( doc, 'applyOperation' );
  38. batch.weakInsert( new Position( root, [ 2 ] ), 'xyz' );
  39. const correctDeltaMatcher = sinon.match( ( operation ) => {
  40. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  41. } );
  42. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  43. } );
  44. } );
  45. } );
  46. describe( 'WeakInsertDelta', ()=> {
  47. it( 'should provide proper className', () => {
  48. expect( WeakInsertDelta.className ).to.equal( 'engine.model.delta.WeakInsertDelta' );
  49. } );
  50. } );