weakinsertdelta.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 Text from '/ckeditor5/engine/model/text.js';
  10. import WeakInsertDelta from '/ckeditor5/engine/model/delta/weakinsertdelta.js';
  11. describe( 'Batch', () => {
  12. let doc, root, batch, chain, attrs;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot();
  16. root.insertChildren( 0, new Text( 'abc' ) );
  17. batch = doc.batch();
  18. attrs = [ [ 'bold', true ], [ 'foo', 'bar' ] ];
  19. doc.selection.setAttributesTo( attrs );
  20. chain = batch.weakInsert( new Position( root, [ 2 ] ), 'xyz' );
  21. } );
  22. describe( 'weakInsert', () => {
  23. it( 'should insert given nodes at given position', () => {
  24. expect( root.getMaxOffset() ).to.equal( 6 );
  25. expect( root.getChild( 0 ).data ).to.equal( 'ab' );
  26. expect( root.getChild( 1 ).data ).to.equal( 'xyz' );
  27. expect( root.getChild( 2 ).data ).to.equal( 'c' );
  28. } );
  29. it( 'should set inserted nodes attributes to same as current selection attributes', () => {
  30. expect( Array.from( root.getChild( 1 ).getAttributes() ) ).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. } );