markerscollection.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import MarkersCollection from '../../src/model/markerscollection';
  6. import Range from '../../src/model/range';
  7. import LiveRange from '../../src/model/liverange';
  8. import Document from '../../src/model/document';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. describe( 'MarkersCollection', () => {
  11. let markers, live, doc, root;
  12. beforeEach( () => {
  13. doc = new Document();
  14. markers = new MarkersCollection();
  15. root = doc.createRoot();
  16. live = LiveRange.createFromParentsAndOffsets( root, 0, root, 4 );
  17. } );
  18. afterEach( () => {
  19. markers.destroy();
  20. live.detach();
  21. } );
  22. describe( 'add', () => {
  23. it( 'should throw if passed parameter is not a LiveRange', () => {
  24. const range = Range.createFromParentsAndOffsets( root, 1, root, 3 );
  25. expect( () => {
  26. markers.add( 'name', range );
  27. } ).to.throw( CKEditorError, /^markers-collection-add-range-not-live-range/ );
  28. } );
  29. it( 'should fire add event when range is added', () => {
  30. sinon.spy( markers, 'fire' );
  31. markers.on( 'add', ( evt, name, range ) => {
  32. expect( name ).to.equal( 'name' );
  33. expect( range.isEqual( live ) ).to.be.true;
  34. expect( range ).not.to.equal( live );
  35. } );
  36. markers.add( 'name', live );
  37. expect( markers.fire.calledWith( 'add' ) ).to.be.true;
  38. } );
  39. it( 'should throw if given name was already added', () => {
  40. const other = LiveRange.createFromParentsAndOffsets( root, 0, root, 4 );
  41. markers.add( 'name', other );
  42. other.detach();
  43. expect( () => {
  44. markers.add( 'name', live );
  45. } ).to.throw( CKEditorError, /^markers-collection-add-name-exists/ );
  46. } );
  47. } );
  48. describe( 'get', () => {
  49. it( 'should return range added to the collection with given name', () => {
  50. markers.add( 'name', live );
  51. expect( markers.get( 'name' ) ).to.equal( live );
  52. } );
  53. it( 'should return null if range with given name has not been found', () => {
  54. expect( markers.get( 'name' ) ).to.be.null;
  55. } );
  56. } );
  57. describe( 'remove', () => {
  58. it( 'should return true and fire remove event if range is removed', () => {
  59. markers.add( 'name', live );
  60. sinon.spy( markers, 'fire' );
  61. markers.on( 'remove', ( evt, name, range ) => {
  62. expect( name ).to.equal( 'name' );
  63. expect( range.isEqual( live ) ).to.be.true;
  64. expect( range ).not.to.equal( live );
  65. } );
  66. const result = markers.remove( 'name' );
  67. expect( result ).to.be.true;
  68. expect( markers.fire.calledWith( 'remove' ) ).to.be.true;
  69. } );
  70. it( 'should return false if name has not been found in collection', () => {
  71. markers.add( 'name', live );
  72. const result = markers.remove( 'other' );
  73. expect( result ).to.be.false;
  74. } );
  75. } );
  76. describe( 'update', () => {
  77. let newLive;
  78. beforeEach( () => {
  79. newLive = LiveRange.createFromParentsAndOffsets( root, 1, root, 5 );
  80. } );
  81. afterEach( () => {
  82. newLive.detach();
  83. } );
  84. it( 'should return true and use remove and add methods if range was found in collection', () => {
  85. const newLive = LiveRange.createFromParentsAndOffsets( root, 1, root, 5 );
  86. markers.add( 'name', live );
  87. sinon.spy( markers, 'remove' );
  88. sinon.spy( markers, 'add' );
  89. const result = markers.update( 'name', newLive );
  90. expect( markers.remove.calledWith( 'name' ) ).to.be.true;
  91. expect( markers.add.calledWith( 'name', newLive ) ).to.be.true;
  92. expect( result ).to.be.true;
  93. newLive.detach();
  94. } );
  95. it( 'should return false if given name was not found in collection', () => {
  96. const result = markers.update( 'name', newLive );
  97. expect( result ).to.be.false;
  98. } );
  99. } );
  100. describe( 'destroy', () => {
  101. it( 'should make MarkersCollection stop listening to all events', () => {
  102. sinon.spy( markers, 'stopListening' );
  103. markers.destroy();
  104. expect( markers.stopListening.calledWithExactly() ).to.be.true;
  105. } );
  106. } );
  107. } );