weakinsertdelta.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Position from '/ckeditor5/core/treemodel/position.js';
  9. describe( 'Batch', () => {
  10. let doc, root, batch, chain, attrs;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot( 'root' );
  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. } );