renamedelta.js 2.6 KB

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