renamedelta.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Element from '../../../src/model/element';
  7. import Text from '../../../src/model/text';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import RenameDelta from '../../../src/model/delta/renamedelta';
  10. describe( 'Batch', () => {
  11. let doc, root, batch, chain;
  12. beforeEach( () => {
  13. doc = new Document();
  14. root = doc.createRoot();
  15. const p = new Element( 'p', null, new Text( 'abc' ) );
  16. root.appendChildren( p );
  17. batch = doc.batch();
  18. chain = batch.rename( p, 'h' );
  19. } );
  20. describe( 'rename', () => {
  21. it( 'should rename given element', () => {
  22. expect( root.maxOffset ).to.equal( 1 );
  23. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  24. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  25. } );
  26. it( 'should throw if not an Element instance is passed', () => {
  27. expect( () => {
  28. batch.rename( new Text( 'abc' ), 'h' );
  29. } ).to.throw( CKEditorError, /^batch-rename-not-element-instance/ );
  30. } );
  31. it( 'should be chainable', () => {
  32. expect( chain ).to.equal( batch );
  33. } );
  34. it( 'should add delta to batch and operation to delta before applying operation', () => {
  35. sinon.spy( doc, 'applyOperation' );
  36. batch.rename( root.getChild( 0 ), 'p' );
  37. const correctDeltaMatcher = sinon.match( operation => {
  38. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  39. } );
  40. expect( doc.applyOperation.alwaysCalledWith( correctDeltaMatcher ) ).to.be.true;
  41. } );
  42. } );
  43. } );
  44. describe( 'RenameDelta', () => {
  45. let renameDelta, doc, root;
  46. beforeEach( () => {
  47. doc = new Document();
  48. root = doc.createRoot();
  49. renameDelta = new RenameDelta();
  50. } );
  51. describe( 'constructor()', () => {
  52. it( 'should create rename delta with no operations added', () => {
  53. expect( renameDelta.operations.length ).to.equal( 0 );
  54. } );
  55. } );
  56. describe( 'type', () => {
  57. it( 'should be equal to rename', () => {
  58. expect( renameDelta.type ).to.equal( 'rename' );
  59. } );
  60. } );
  61. describe( 'getReversed', () => {
  62. it( 'should return instance of RenameDelta', () => {
  63. const reversed = renameDelta.getReversed();
  64. expect( reversed ).to.be.instanceof( RenameDelta );
  65. } );
  66. it( 'should return correct RenameDelta', () => {
  67. root.appendChildren( new Element( 'p', null, new Text( 'abc' ) ) );
  68. const batch = doc.batch();
  69. batch.rename( root.getChild( 0 ), 'h' );
  70. const reversed = batch.deltas[ 0 ].getReversed();
  71. reversed.operations.forEach( operation => {
  72. doc.applyOperation( operation );
  73. } );
  74. expect( root.maxOffset ).to.equal( 1 );
  75. expect( root.getChild( 0 ) ).to.have.property( 'name', 'p' );
  76. } );
  77. } );
  78. it( 'should provide proper className', () => {
  79. expect( RenameDelta.className ).to.equal( 'engine.model.delta.RenameDelta' );
  80. } );
  81. } );