8
0

markerscollection.js 3.9 KB

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