8
0

changebuffer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. describe( 'ChangeBuffer', () => {
  9. const CHANGE_LIMIT = 3;
  10. let doc, buffer, root;
  11. beforeEach( () => {
  12. doc = new Document();
  13. root = doc.createRoot();
  14. buffer = new ChangeBuffer( doc, CHANGE_LIMIT );
  15. } );
  16. describe( 'constructor()', () => {
  17. it( 'sets all properties', () => {
  18. expect( buffer ).to.have.property( 'document', doc );
  19. expect( buffer ).to.have.property( 'limit', CHANGE_LIMIT );
  20. expect( buffer ).to.have.property( 'size', 0 );
  21. expect( buffer ).to.have.property( 'isLocked', false );
  22. } );
  23. it( 'sets limit property according to default value', () => {
  24. buffer = new ChangeBuffer( doc );
  25. expect( buffer ).to.have.property( 'limit', 20 );
  26. } );
  27. } );
  28. describe( 'locking', () => {
  29. it( 'is unlocked by default', () => {
  30. expect( buffer.isLocked ).to.be.false;
  31. } );
  32. it( 'is locked by lock method', () => {
  33. buffer.lock();
  34. expect( buffer.isLocked ).to.be.true;
  35. } );
  36. it( 'is unlocked by unlock method', () => {
  37. buffer.isLocked = true;
  38. buffer.unlock();
  39. expect( buffer.isLocked ).to.be.false;
  40. } );
  41. } );
  42. describe( 'batch', () => {
  43. it( 'it is set initially', () => {
  44. expect( buffer ).to.have.property( 'batch' );
  45. expect( buffer.batch ).to.be.instanceof( Batch );
  46. } );
  47. it( 'is reset once changes reaches the limit', () => {
  48. const batch1 = buffer.batch;
  49. buffer.input( CHANGE_LIMIT - 1 );
  50. expect( buffer.batch ).to.equal( batch1 );
  51. buffer.input( 1 );
  52. const batch2 = buffer.batch;
  53. expect( batch2 ).to.be.instanceof( Batch );
  54. expect( batch2 ).to.not.equal( batch1 );
  55. expect( buffer.size ).to.equal( 0 );
  56. } );
  57. it( 'is reset once changes exceedes the limit', () => {
  58. const batch1 = buffer.batch;
  59. // Exceed the limit with one big jump to ensure that >= operator was used.
  60. buffer.input( CHANGE_LIMIT + 1 );
  61. expect( buffer.batch ).to.not.equal( batch1 );
  62. expect( buffer.size ).to.equal( 0 );
  63. } );
  64. it( 'is reset once a new batch appears in the document', () => {
  65. const batch1 = buffer.batch;
  66. // Ensure that size is reset too.
  67. buffer.input( 1 );
  68. doc.batch().insertText( 'a', root );
  69. expect( buffer.batch ).to.not.equal( batch1 );
  70. expect( buffer.size ).to.equal( 0 );
  71. } );
  72. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  73. const batch1 = buffer.batch;
  74. buffer.batch.insert( 'a', root );
  75. expect( buffer.batch ).to.equal( batch1 );
  76. buffer.batch.insert( 'b', root );
  77. expect( buffer.batch ).to.equal( batch1 );
  78. } );
  79. it( 'is not reset when changes are added to batch which existed previously', () => {
  80. const externalBatch = doc.batch();
  81. externalBatch.insertText( 'a', root );
  82. const bufferBatch = buffer.batch;
  83. bufferBatch.insertText( 'b', root );
  84. expect( buffer.batch ).to.equal( bufferBatch );
  85. doc.batch().insertText( 'c', root );
  86. expect( buffer.batch ).to.not.equal( bufferBatch );
  87. } );
  88. it( 'is not reset when changes are applied in transparent batch', () => {
  89. const bufferBatch = buffer.batch;
  90. doc.batch( 'transparent' ).insert( 'a', root );
  91. expect( buffer.batch ).to.equal( bufferBatch );
  92. } );
  93. it( 'is not reset while locked', () => {
  94. const initialBatch = buffer.batch;
  95. buffer.lock();
  96. buffer.input( 1 );
  97. buffer._reset();
  98. buffer.unlock();
  99. expect( buffer.batch ).to.be.equal( initialBatch );
  100. expect( buffer.size ).to.equal( 1 );
  101. } );
  102. it( 'is reset while locked with ignoreLock used', () => {
  103. const initialBatch = buffer.batch;
  104. buffer.lock();
  105. buffer.input( 1 );
  106. buffer._reset( true );
  107. buffer.unlock();
  108. expect( buffer.batch ).to.not.equal( initialBatch );
  109. expect( buffer.size ).to.equal( 0 );
  110. } );
  111. it( 'is reset while locked and limit exceeded', () => {
  112. const initialBatch = buffer.batch;
  113. buffer.lock();
  114. buffer.input( CHANGE_LIMIT + 1 );
  115. buffer.unlock();
  116. expect( buffer.batch ).to.not.equal( initialBatch );
  117. expect( buffer.size ).to.equal( 0 );
  118. } );
  119. it( 'is reset while locked and new batch is applied', () => {
  120. const initialBatch = buffer.batch;
  121. buffer.lock();
  122. doc.batch().insertText( 'a', root );
  123. buffer.unlock();
  124. expect( buffer.batch ).to.not.equal( initialBatch );
  125. expect( buffer.size ).to.equal( 0 );
  126. } );
  127. it( 'is reset on selection change:range', () => {
  128. const initialBatch = buffer.batch;
  129. doc.selection.fire( 'change:range' );
  130. expect( buffer.batch ).to.not.equal( initialBatch );
  131. expect( buffer.size ).to.equal( 0 );
  132. } );
  133. it( 'is reset on selection change:attribute', () => {
  134. const initialBatch = buffer.batch;
  135. doc.selection.fire( 'change:attribute' );
  136. expect( buffer.batch ).to.not.equal( initialBatch );
  137. expect( buffer.size ).to.equal( 0 );
  138. } );
  139. it( 'is not reset on selection change:range while locked', () => {
  140. const initialBatch = buffer.batch;
  141. buffer.size = 1;
  142. buffer.lock();
  143. doc.selection.fire( 'change:range' );
  144. buffer.unlock();
  145. expect( buffer.batch ).to.be.equal( initialBatch );
  146. expect( buffer.size ).to.equal( 1 );
  147. } );
  148. it( 'is not reset on selection change:attribute while locked', () => {
  149. const initialBatch = buffer.batch;
  150. buffer.size = 1;
  151. buffer.lock();
  152. doc.selection.fire( 'change:attribute' );
  153. buffer.unlock();
  154. expect( buffer.batch ).to.be.equal( initialBatch );
  155. expect( buffer.size ).to.equal( 1 );
  156. } );
  157. } );
  158. describe( 'destroy()', () => {
  159. it( 'offs the buffer from the document', () => {
  160. const batch1 = buffer.batch;
  161. buffer.destroy();
  162. doc.batch().insertText( 'a', root );
  163. expect( buffer.batch ).to.equal( batch1 );
  164. } );
  165. it( 'offs the buffer from the selection change:range', () => {
  166. const batch1 = buffer.batch;
  167. buffer.destroy();
  168. doc.selection.fire( 'change:attribute' );
  169. expect( buffer.batch ).to.equal( batch1 );
  170. } );
  171. it( 'offs the buffer from the selection change:attribute', () => {
  172. const batch1 = buffer.batch;
  173. buffer.destroy();
  174. doc.selection.fire( 'change:range' );
  175. expect( buffer.batch ).to.equal( batch1 );
  176. } );
  177. } );
  178. } );