renamedelta.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Element from '/ckeditor5/engine/model/element.js';
  8. import RenameDelta from '/ckeditor5/engine/model/delta/renamedelta.js';
  9. describe( 'Batch', () => {
  10. let doc, root, batch, chain;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot();
  14. const p = new Element( 'p', null, 'abc' );
  15. root.appendChildren( p );
  16. batch = doc.batch();
  17. chain = batch.rename( 'h', p );
  18. } );
  19. describe( 'rename', () => {
  20. it( 'should rename given element', () => {
  21. expect( root.getChildCount() ).to.equal( 1 );
  22. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  23. expect( root.getChild( 0 ).getText() ).to.equal( 'abc' );
  24. } );
  25. it( 'should be chainable', () => {
  26. expect( chain ).to.equal( batch );
  27. } );
  28. it( 'should add delta to batch and operation to delta before applying operation', () => {
  29. sinon.spy( doc, 'applyOperation' );
  30. batch.rename( 'p', root.getChild( 0 ) );
  31. const correctDeltaMatcher = sinon.match( operation => {
  32. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  33. } );
  34. expect( doc.applyOperation.alwaysCalledWith( correctDeltaMatcher ) ).to.be.true;
  35. } );
  36. } );
  37. } );
  38. describe( 'RenameDelta', () => {
  39. let renameDelta, doc, root;
  40. beforeEach( () => {
  41. doc = new Document();
  42. root = doc.createRoot();
  43. renameDelta = new RenameDelta();
  44. } );
  45. describe( 'constructor', () => {
  46. it( 'should create rename delta with no operations added', () => {
  47. expect( renameDelta.operations.length ).to.equal( 0 );
  48. } );
  49. } );
  50. describe( 'getReversed', () => {
  51. it( 'should return instance of RenameDelta', () => {
  52. let reversed = renameDelta.getReversed();
  53. expect( reversed ).to.be.instanceof( RenameDelta );
  54. } );
  55. it( 'should return correct RenameDelta', () => {
  56. root.appendChildren( new Element( 'p', null, 'abc' ) );
  57. const batch = doc.batch();
  58. batch.rename( 'h', root.getChild( 0 ) );
  59. const reversed = batch.deltas[ 0 ].getReversed();
  60. reversed.operations.forEach( operation => {
  61. doc.applyOperation( operation );
  62. } );
  63. expect( root.getChildCount() ).to.equal( 1 );
  64. expect( root.getChild( 0 ) ).to.have.property( 'name', 'p' );
  65. expect( root.getChild( 0 ).getText() ).to.equal( 'abc' );
  66. } );
  67. } );
  68. it( 'should provide proper className', () => {
  69. expect( RenameDelta.className ).to.equal( 'engine.model.delta.RenameDelta' );
  70. } );
  71. } );