renamedelta.js 2.8 KB

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