markeroperation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. function matchRange( range ) {
  10. return sinon.match( rangeToMatch => rangeToMatch.isEqual( range ) );
  11. }
  12. describe( 'MarkerOperation', () => {
  13. let model, doc, root, range;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. root._appendChild( 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, model.markers, true, 0 );
  23. expect( op.type ).to.equal( 'marker' );
  24. } );
  25. it( 'should add marker to document marker collection', () => {
  26. sinon.spy( model.markers, '_set' );
  27. model.applyOperation(
  28. new MarkerOperation( 'name', null, range, model.markers, true, doc.version )
  29. );
  30. expect( doc.version ).to.equal( 1 );
  31. expect( model.markers._set.calledWith( 'name', matchRange( range ) ) );
  32. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  33. } );
  34. it( 'should update marker in document marker collection', () => {
  35. model.applyOperation(
  36. new MarkerOperation( 'name', null, range, model.markers, true, doc.version )
  37. );
  38. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  39. sinon.spy( model.markers, '_set' );
  40. model.applyOperation(
  41. new MarkerOperation( 'name', range, range2, model.markers, true, doc.version )
  42. );
  43. expect( doc.version ).to.equal( 2 );
  44. expect( model.markers._set.calledWith( 'name', matchRange( range2 ) ) );
  45. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  46. } );
  47. it( 'should remove marker from document marker collection', () => {
  48. model.applyOperation(
  49. new MarkerOperation( 'name', null, range, model.markers, true, doc.version )
  50. );
  51. sinon.spy( model.markers, '_remove' );
  52. model.applyOperation(
  53. new MarkerOperation( 'name', range, null, model.markers, true, doc.version )
  54. );
  55. expect( doc.version ).to.equal( 2 );
  56. expect( model.markers._remove.calledWith( 'name' ) );
  57. expect( model.markers.get( 'name' ) ).to.be.null;
  58. } );
  59. it( 'should not fire document markers remove event if removing non-existing range', () => {
  60. sinon.spy( model.markers, 'fire' );
  61. model.applyOperation(
  62. new MarkerOperation( 'name', null, null, model.markers, true, doc.version )
  63. );
  64. expect( model.markers.fire.notCalled ).to.be.true;
  65. } );
  66. it( 'should not fire document markers set event if newRange is same as current marker range', () => {
  67. model.change( writer => {
  68. writer.addMarker( 'name', { range, usingOperation: true } );
  69. } );
  70. sinon.spy( model.markers, 'fire' );
  71. model.applyOperation(
  72. new MarkerOperation( 'name', range, range, model.markers, false, doc.version )
  73. );
  74. expect( model.markers.fire.notCalled ).to.be.true;
  75. } );
  76. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  77. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  78. const op1 = new MarkerOperation( 'name', null, range, model.markers, true, doc.version );
  79. const reversed1 = op1.getReversed();
  80. const op2 = new MarkerOperation( 'name', range, range2, model.markers, true, doc.version );
  81. const reversed2 = op2.getReversed();
  82. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  83. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  84. expect( reversed1.name ).to.equal( 'name' );
  85. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  86. expect( reversed1.newRange ).to.be.null;
  87. expect( reversed1.baseVersion ).to.equal( 1 );
  88. expect( reversed1.affectsData ).to.be.true;
  89. expect( reversed2.name ).to.equal( 'name' );
  90. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  91. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  92. expect( reversed2.baseVersion ).to.equal( 1 );
  93. expect( reversed2.affectsData ).to.be.true;
  94. } );
  95. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  96. const op = new MarkerOperation( 'name', null, range, model.markers, true, 0 );
  97. const clone = op.clone();
  98. expect( clone ).to.be.an.instanceof( MarkerOperation );
  99. expect( clone ).to.deep.equal( op );
  100. } );
  101. describe( 'toJSON', () => {
  102. it( 'should create proper serialized object', () => {
  103. const op = new MarkerOperation( 'name', null, range, model.markers, true, doc.version );
  104. const serialized = op.toJSON();
  105. expect( serialized ).to.deep.equal( {
  106. __className: 'MarkerOperation',
  107. baseVersion: 0,
  108. name: 'name',
  109. oldRange: null,
  110. newRange: range.toJSON(),
  111. affectsData: true
  112. } );
  113. } );
  114. } );
  115. describe( 'fromJSON', () => {
  116. it( 'should create proper MarkerOperation from json object #1', () => {
  117. const op = new MarkerOperation( 'name', null, range, model.markers, true, doc.version );
  118. const serialized = op.toJSON();
  119. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  120. expect( deserialized ).to.deep.equal( op );
  121. } );
  122. it( 'should create proper MarkerOperation from json object #2', () => {
  123. // Gotta love 100% CC.
  124. const op = new MarkerOperation( 'name', range, null, model.markers, true, doc.version );
  125. const serialized = op.toJSON();
  126. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  127. expect( deserialized ).to.deep.equal( op );
  128. } );
  129. } );
  130. } );