markerdelta.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Range from '../../../src/model/range';
  7. import Text from '../../../src/model/text';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import MarkerDelta from '../../../src/model/delta/markerdelta';
  10. import MarkerOperation from '../../../src/model/operation/markeroperation';
  11. describe( 'Batch', () => {
  12. let doc, root, range;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot();
  16. root.appendChildren( new Text( 'foo' ) );
  17. range = Range.createIn( root );
  18. } );
  19. describe( 'setMarker', () => {
  20. it( 'should add marker to the document marker collection', () => {
  21. doc.batch().setMarker( 'name', range );
  22. expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  23. } );
  24. it( 'should update marker in the document marker collection', () => {
  25. doc.batch().setMarker( 'name', range );
  26. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  27. doc.batch().setMarker( 'name', range2 );
  28. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  29. } );
  30. it( 'should accept marker instance', () => {
  31. doc.batch().setMarker( 'name', range );
  32. const marker = doc.markers.get( 'name' );
  33. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  34. doc.batch().setMarker( marker, range2 );
  35. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  36. } );
  37. it( 'should accept empty range parameter if marker instance is passed', () => {
  38. doc.markers.set( 'name', range );
  39. const marker = doc.markers.get( 'name' );
  40. sinon.spy( doc, 'fire' );
  41. doc.on( 'change', ( evt, type, changes ) => {
  42. if ( type == 'marker' ) {
  43. expect( changes.type ).to.equal( 'set' );
  44. expect( changes.name ).to.equal( 'name' );
  45. }
  46. } );
  47. doc.batch().setMarker( marker );
  48. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  49. } );
  50. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  51. expect( () => {
  52. doc.batch().setMarker( 'name' );
  53. } ).to.throw( CKEditorError, /^batch-setMarker-no-range/ );
  54. } );
  55. } );
  56. describe( 'removeMarker', () => {
  57. it( 'should remove marker from the document marker collection', () => {
  58. doc.batch().setMarker( 'name', range );
  59. doc.batch().removeMarker( 'name' );
  60. expect( doc.markers.get( 'name' ) ).to.be.null;
  61. } );
  62. it( 'should throw when trying to remove non existing marker', () => {
  63. expect( () => {
  64. doc.batch().removeMarker( 'name' );
  65. } ).to.throw( CKEditorError, /^batch-removeMarker-no-marker/ );
  66. } );
  67. it( 'should accept marker instance', () => {
  68. doc.batch().setMarker( 'name', range );
  69. const marker = doc.markers.get( 'name' );
  70. doc.batch().removeMarker( marker );
  71. expect( doc.markers.get( 'name' ) ).to.be.null;
  72. } );
  73. } );
  74. it( 'should be chainable', () => {
  75. const batch = doc.batch();
  76. const chain = batch.setMarker( 'name', range );
  77. expect( chain ).to.equal( batch );
  78. } );
  79. it( 'should add delta to batch and operation to delta before applying operation', () => {
  80. sinon.spy( doc, 'applyOperation' );
  81. const batch = doc.batch().setMarker( 'name', range );
  82. const correctDeltaMatcher = sinon.match( ( operation ) => {
  83. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  84. } );
  85. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  86. } );
  87. } );
  88. describe( 'MarkerDelta', () => {
  89. let markerDelta, doc, root, range;
  90. beforeEach( () => {
  91. doc = new Document();
  92. root = doc.createRoot();
  93. range = Range.createIn( root );
  94. markerDelta = new MarkerDelta();
  95. } );
  96. describe( 'constructor()', () => {
  97. it( 'should create merge delta with no operations added', () => {
  98. expect( markerDelta.operations.length ).to.equal( 0 );
  99. } );
  100. } );
  101. describe( 'getReversed', () => {
  102. it( 'should return correct MarkerDelta', () => {
  103. markerDelta.addOperation( new MarkerOperation( 'name', null, range, 0 ) );
  104. const reversed = markerDelta.getReversed();
  105. expect( reversed ).to.be.instanceof( MarkerDelta );
  106. expect( reversed.operations.length ).to.equal( 1 );
  107. const op = reversed.operations[ 0 ];
  108. expect( op ).to.be.an.instanceof( MarkerOperation );
  109. expect( op.oldRange.isEqual( range ) ).to.be.true;
  110. expect( op.newRange ).to.be.null;
  111. } );
  112. } );
  113. it( 'should provide proper className', () => {
  114. expect( MarkerDelta.className ).to.equal( 'engine.model.delta.MarkerDelta' );
  115. } );
  116. } );