markeroperation.js 7.1 KB

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