markeroperation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. model.applyOperation( wrapInDelta(
  29. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  30. ) );
  31. expect( doc.version ).to.equal( 1 );
  32. expect( model.markers.set.calledWith( 'name', matchRange( range ) ) );
  33. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  34. } );
  35. it( 'should update marker in document marker collection', () => {
  36. model.applyOperation( wrapInDelta(
  37. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  38. ) );
  39. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  40. sinon.spy( model.markers, 'set' );
  41. model.applyOperation( wrapInDelta(
  42. new MarkerOperation( 'name', range, range2, model.markers, doc.version )
  43. ) );
  44. expect( doc.version ).to.equal( 2 );
  45. expect( model.markers.set.calledWith( 'name', matchRange( range2 ) ) );
  46. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  47. } );
  48. it( 'should remove marker from document marker collection', () => {
  49. model.applyOperation( wrapInDelta(
  50. new MarkerOperation( 'name', null, range, model.markers, doc.version )
  51. ) );
  52. sinon.spy( model.markers, 'remove' );
  53. model.applyOperation( wrapInDelta(
  54. new MarkerOperation( 'name', range, null, model.markers, doc.version )
  55. ) );
  56. expect( doc.version ).to.equal( 2 );
  57. expect( model.markers.remove.calledWith( 'name' ) );
  58. expect( model.markers.get( 'name' ) ).to.be.null;
  59. } );
  60. it( 'should not fire document markers remove event if removing non-existing range', () => {
  61. sinon.spy( model.markers, 'fire' );
  62. model.applyOperation( wrapInDelta(
  63. new MarkerOperation( 'name', null, null, model.markers, doc.version )
  64. ) );
  65. expect( model.markers.fire.notCalled ).to.be.true;
  66. } );
  67. it( 'should not fire document markers set event if newRange is same as current marker range', () => {
  68. model.markers.set( 'name', range );
  69. sinon.spy( model.markers, 'fire' );
  70. model.applyOperation( wrapInDelta(
  71. new MarkerOperation( 'name', range, range, model.markers, doc.version )
  72. ) );
  73. expect( model.markers.fire.notCalled ).to.be.true;
  74. } );
  75. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  76. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  77. const op1 = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  78. const reversed1 = op1.getReversed();
  79. const op2 = new MarkerOperation( 'name', range, range2, model.markers, doc.version );
  80. const reversed2 = op2.getReversed();
  81. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  82. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  83. expect( reversed1.name ).to.equal( 'name' );
  84. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  85. expect( reversed1.newRange ).to.be.null;
  86. expect( reversed1.baseVersion ).to.equal( 1 );
  87. expect( reversed2.name ).to.equal( 'name' );
  88. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  89. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  90. expect( reversed2.baseVersion ).to.equal( 1 );
  91. } );
  92. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  93. const op = new MarkerOperation( 'name', null, range, model.markers, 0 );
  94. const clone = op.clone();
  95. expect( clone ).to.be.an.instanceof( MarkerOperation );
  96. expect( clone ).to.deep.equal( op );
  97. } );
  98. describe( 'isDocumentOperation', () => {
  99. it( 'should return true when new marker range is added to the document', () => {
  100. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  101. expect( op.isDocumentOperation ).to.true;
  102. } );
  103. it( 'should return false when marker range is removed from the document', () => {
  104. const op = new MarkerOperation( 'name', range, null, model.markers, doc.version );
  105. expect( op.isDocumentOperation ).to.true;
  106. } );
  107. it( 'should return true when non-existing marker range is removed from the document', () => {
  108. const op = new MarkerOperation( 'name', null, null, model.markers, doc.version );
  109. expect( op.isDocumentOperation ).to.true;
  110. } );
  111. } );
  112. describe( 'toJSON', () => {
  113. it( 'should create proper serialized object', () => {
  114. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  115. const serialized = jsonParseStringify( op );
  116. expect( serialized ).to.deep.equal( {
  117. __className: 'engine.model.operation.MarkerOperation',
  118. baseVersion: 0,
  119. name: 'name',
  120. oldRange: null,
  121. newRange: jsonParseStringify( range )
  122. } );
  123. } );
  124. } );
  125. describe( 'fromJSON', () => {
  126. it( 'should create proper MarkerOperation from json object #1', () => {
  127. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version );
  128. const serialized = jsonParseStringify( op );
  129. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  130. expect( deserialized ).to.deep.equal( op );
  131. } );
  132. it( 'should create proper MarkerOperation from json object #2', () => {
  133. // Gotta love 100% CC.
  134. const op = new MarkerOperation( 'name', range, null, model.markers, doc.version );
  135. const serialized = jsonParseStringify( op );
  136. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  137. expect( deserialized ).to.deep.equal( op );
  138. } );
  139. } );
  140. } );