changebuffer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ChangeBuffer from '../src/changebuffer';
  6. import Document from '@ckeditor/ckeditor5-engine/src/model/document';
  7. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  8. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  9. describe( 'ChangeBuffer', () => {
  10. const CHANGE_LIMIT = 3;
  11. let doc, buffer, root;
  12. beforeEach( () => {
  13. doc = new Document();
  14. root = doc.createRoot();
  15. buffer = new ChangeBuffer( doc, CHANGE_LIMIT );
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'sets all properties', () => {
  19. expect( buffer ).to.have.property( 'document', doc );
  20. expect( buffer ).to.have.property( 'limit', CHANGE_LIMIT );
  21. expect( buffer ).to.have.property( 'size', 0 );
  22. expect( buffer ).to.have.property( 'isLocked', false );
  23. } );
  24. it( 'sets limit property according to default value', () => {
  25. buffer = new ChangeBuffer( doc );
  26. expect( buffer ).to.have.property( 'limit', 20 );
  27. } );
  28. } );
  29. describe( 'locking', () => {
  30. it( 'is unlocked by default', () => {
  31. expect( buffer.isLocked ).to.be.false;
  32. } );
  33. it( 'is locked by lock method', () => {
  34. buffer.lock();
  35. expect( buffer.isLocked ).to.be.true;
  36. } );
  37. it( 'is unlocked by unlock method', () => {
  38. buffer.isLocked = true;
  39. buffer.unlock();
  40. expect( buffer.isLocked ).to.be.false;
  41. } );
  42. } );
  43. describe( 'batch', () => {
  44. it( 'it is set initially', () => {
  45. expect( buffer ).to.have.property( 'batch' );
  46. expect( buffer.batch ).to.be.instanceof( Batch );
  47. } );
  48. it( 'is reset once changes reaches the limit', () => {
  49. const batch1 = buffer.batch;
  50. buffer.input( CHANGE_LIMIT - 1 );
  51. expect( buffer.batch ).to.equal( batch1 );
  52. buffer.input( 1 );
  53. const batch2 = buffer.batch;
  54. expect( batch2 ).to.be.instanceof( Batch );
  55. expect( batch2 ).to.not.equal( batch1 );
  56. expect( buffer.size ).to.equal( 0 );
  57. } );
  58. it( 'is reset once changes exceedes the limit', () => {
  59. const batch1 = buffer.batch;
  60. // Exceed the limit with one big jump to ensure that >= operator was used.
  61. buffer.input( CHANGE_LIMIT + 1 );
  62. expect( buffer.batch ).to.not.equal( batch1 );
  63. expect( buffer.size ).to.equal( 0 );
  64. } );
  65. it( 'is reset once a new batch appears in the document', () => {
  66. const batch1 = buffer.batch;
  67. // Ensure that size is reset too.
  68. buffer.input( 1 );
  69. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  70. expect( buffer.batch ).to.not.equal( batch1 );
  71. expect( buffer.size ).to.equal( 0 );
  72. } );
  73. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  74. const batch1 = buffer.batch;
  75. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  76. expect( buffer.batch ).to.equal( batch1 );
  77. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  78. expect( buffer.batch ).to.equal( batch1 );
  79. } );
  80. it( 'is not reset when changes are added to batch which existed previously', () => {
  81. const externalBatch = doc.batch();
  82. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  83. const bufferBatch = buffer.batch;
  84. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  85. expect( buffer.batch ).to.equal( bufferBatch );
  86. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  87. expect( buffer.batch ).to.not.equal( bufferBatch );
  88. } );
  89. it( 'is not reset when changes are applied in transparent batch', () => {
  90. const bufferBatch = buffer.batch;
  91. doc.batch( 'transparent' ).insert( Position.createAt( root, 0 ), 'a' );
  92. expect( buffer.batch ).to.equal( bufferBatch );
  93. } );
  94. it( 'is not reset while locked', () => {
  95. const initialBatch = buffer.batch;
  96. buffer.lock();
  97. buffer.input( 1 );
  98. buffer._reset();
  99. buffer.unlock();
  100. expect( buffer.batch ).to.be.equal( initialBatch );
  101. expect( buffer.size ).to.equal( 1 );
  102. } );
  103. it( 'is reset while locked with ignoreLock used', () => {
  104. const initialBatch = buffer.batch;
  105. buffer.lock();
  106. buffer.input( 1 );
  107. buffer._reset( true );
  108. buffer.unlock();
  109. expect( buffer.batch ).to.not.equal( initialBatch );
  110. expect( buffer.size ).to.equal( 0 );
  111. } );
  112. it( 'is reset while locked and limit exceeded', () => {
  113. const initialBatch = buffer.batch;
  114. buffer.lock();
  115. buffer.input( CHANGE_LIMIT + 1 );
  116. buffer.unlock();
  117. expect( buffer.batch ).to.not.equal( initialBatch );
  118. expect( buffer.size ).to.equal( 0 );
  119. } );
  120. it( 'is reset while locked and new batch is applied', () => {
  121. const initialBatch = buffer.batch;
  122. buffer.lock();
  123. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  124. buffer.unlock();
  125. expect( buffer.batch ).to.not.equal( initialBatch );
  126. expect( buffer.size ).to.equal( 0 );
  127. } );
  128. } );
  129. describe( 'destroy', () => {
  130. it( 'offs the buffer from the document', () => {
  131. const batch1 = buffer.batch;
  132. buffer.destroy();
  133. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  134. expect( buffer.batch ).to.equal( batch1 );
  135. } );
  136. it( 'offs the buffer from the selection change:range', () => {
  137. const batch1 = buffer.batch;
  138. buffer.destroy();
  139. doc.selection.fire( 'change:attribute' );
  140. expect( buffer.batch ).to.equal( batch1 );
  141. } );
  142. it( 'offs the buffer from the selection change:attribute', () => {
  143. const batch1 = buffer.batch;
  144. buffer.destroy();
  145. doc.selection.fire( 'change:range' );
  146. expect( buffer.batch ).to.equal( batch1 );
  147. } );
  148. } );
  149. } );