markeroperation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Text from '../../../src/model/text';
  7. import Range from '../../../src/model/range';
  8. import MarkerOperation from '../../../src/model/operation/markeroperation';
  9. import { jsonParseStringify, wrapInDelta } from '../../model/_utils/utils';
  10. function matchRange( range ) {
  11. return sinon.match( rangeToMatch => rangeToMatch.isEqual( range ) );
  12. }
  13. describe( 'MarkerOperation', () => {
  14. let model, doc, root, range;
  15. beforeEach( () => {
  16. model = new Model();
  17. doc = model.document;
  18. root = doc.createRoot();
  19. root.appendChildren( new Text( 'foo' ) );
  20. range = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  21. } );
  22. it( 'should have property type equal to "marker"', () => {
  23. const op = new MarkerOperation( 'name', null, range, model.markers, 0 );
  24. expect( op.type ).to.equal( 'marker' );
  25. } );
  26. it( 'should add marker to document marker collection', () => {
  27. sinon.spy( model.markers, 'set' );
  28. sinon.spy( doc, 'fire' );
  29. doc.on( 'change', ( evt, type, changes ) => {
  30. expect( type ).to.equal( 'marker' );
  31. expect( changes.name ).to.equal( 'name' );
  32. expect( changes.type ).to.equal( 'set' );
  33. } );
  34. model.applyOperation( wrapInDelta(
  35. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  36. ) );
  37. expect( doc.version ).to.equal( 1 );
  38. expect( model.markers.set.calledWith( 'name', matchRange( range ) ) );
  39. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  40. expect( doc.fire.called ).to.be.true;
  41. } );
  42. it( 'should update marker in document marker collection', () => {
  43. model.applyOperation( wrapInDelta(
  44. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  45. ) );
  46. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  47. sinon.spy( model.markers, 'set' );
  48. sinon.spy( doc, 'fire' );
  49. model.applyOperation( wrapInDelta(
  50. new MarkerOperation( 'name', range, range2, model.markers, doc.version )
  51. ) );
  52. expect( doc.version ).to.equal( 2 );
  53. expect( model.markers.set.calledWith( 'name', matchRange( range2 ) ) );
  54. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  55. expect( doc.fire.called ).to.be.true;
  56. } );
  57. it( 'should remove marker from document marker collection', () => {
  58. model.applyOperation( wrapInDelta(
  59. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  60. ) );
  61. sinon.spy( model.markers, 'remove' );
  62. sinon.spy( doc, 'fire' );
  63. doc.on( 'change', ( evt, type, changes ) => {
  64. expect( type ).to.equal( 'marker' );
  65. expect( changes.name ).to.equal( 'name' );
  66. expect( changes.type ).to.equal( 'remove' );
  67. } );
  68. model.applyOperation( wrapInDelta(
  69. new MarkerOperation( 'name', range, null, model.markers, doc.version )
  70. ) );
  71. expect( doc.version ).to.equal( 2 );
  72. expect( model.markers.remove.calledWith( 'name' ) );
  73. expect( model.markers.get( 'name' ) ).to.be.null;
  74. expect( doc.fire.called ).to.be.true;
  75. } );
  76. it( 'should fire document change event but not document markers remove event if removing non-existing range', () => {
  77. sinon.spy( doc, 'fire' );
  78. sinon.spy( model.markers, 'fire' );
  79. doc.on( 'change', ( evt, type, changes ) => {
  80. expect( type ).to.equal( 'marker' );
  81. expect( changes.name ).to.equal( 'name' );
  82. expect( changes.type ).to.equal( 'remove' );
  83. } );
  84. model.applyOperation( wrapInDelta(
  85. new MarkerOperation( 'name', null, null, model.markers, doc.version )
  86. ) );
  87. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  88. expect( model.markers.fire.notCalled ).to.be.true;
  89. } );
  90. it( 'should fire document change event but not document markers set event if newRange is same as current marker range', () => {
  91. model.markers.set( 'name', range );
  92. sinon.spy( doc, 'fire' );
  93. sinon.spy( model.markers, 'fire' );
  94. doc.on( 'change', ( evt, type, changes ) => {
  95. expect( type ).to.equal( 'marker' );
  96. expect( changes.name ).to.equal( 'name' );
  97. expect( changes.type ).to.equal( 'set' );
  98. } );
  99. model.applyOperation( wrapInDelta(
  100. new MarkerOperation( 'name', range, range, model.markers, doc.version )
  101. ) );
  102. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  103. expect( model.markers.fire.notCalled ).to.be.true;
  104. } );
  105. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  106. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  107. const op1 = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  108. const reversed1 = op1.getReversed();
  109. const op2 = new MarkerOperation( 'name', range, range2, model.markers, doc.version );
  110. const reversed2 = op2.getReversed();
  111. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  112. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  113. expect( reversed1.name ).to.equal( 'name' );
  114. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  115. expect( reversed1.newRange ).to.be.null;
  116. expect( reversed1.baseVersion ).to.equal( 1 );
  117. expect( reversed2.name ).to.equal( 'name' );
  118. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  119. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  120. expect( reversed2.baseVersion ).to.equal( 1 );
  121. } );
  122. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  123. const op = new MarkerOperation( 'name', null, range, model.markers, 0 );
  124. const clone = op.clone();
  125. expect( clone ).to.be.an.instanceof( MarkerOperation );
  126. expect( clone ).to.deep.equal( op );
  127. } );
  128. describe( 'isDocumentOperation', () => {
  129. it( 'should return true when new marker range is added to the document', () => {
  130. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  131. expect( op.isDocumentOperation ).to.true;
  132. } );
  133. it( 'should return false when marker range is removed from the document', () => {
  134. const op = new MarkerOperation( 'name', range, null, model.markers, doc.version );
  135. expect( op.isDocumentOperation ).to.true;
  136. } );
  137. it( 'should return true when non-existing marker range is removed from the document', () => {
  138. const op = new MarkerOperation( 'name', null, null, model.markers, doc.version );
  139. expect( op.isDocumentOperation ).to.true;
  140. } );
  141. } );
  142. describe( 'toJSON', () => {
  143. it( 'should create proper serialized object', () => {
  144. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  145. const serialized = jsonParseStringify( op );
  146. expect( serialized ).to.deep.equal( {
  147. __className: 'engine.model.operation.MarkerOperation',
  148. baseVersion: 0,
  149. name: 'name',
  150. oldRange: null,
  151. newRange: jsonParseStringify( range )
  152. } );
  153. } );
  154. } );
  155. describe( 'fromJSON', () => {
  156. it( 'should create proper MarkerOperation from json object #1', () => {
  157. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  158. const serialized = jsonParseStringify( op );
  159. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  160. expect( deserialized ).to.deep.equal( op );
  161. } );
  162. it( 'should create proper MarkerOperation from json object #2', () => {
  163. // Gotta love 100% CC.
  164. const op = new MarkerOperation( 'name', range, null, model.markers, doc.version );
  165. const serialized = jsonParseStringify( op );
  166. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  167. expect( deserialized ).to.deep.equal( op );
  168. } );
  169. } );
  170. } );