8
0

changebuffer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ChangeBuffer from 'ckeditor5/typing/changebuffer.js';
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import Batch from 'ckeditor5/engine/model/batch.js';
  8. import Position from 'ckeditor5/engine/model/position.js';
  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. } );
  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( 'batch', () => {
  29. it( 'it is set initially', () => {
  30. expect( buffer ).to.have.property( 'batch' );
  31. expect( buffer.batch ).to.be.instanceof( Batch );
  32. } );
  33. it( 'is reset once changes reaches the limit', () => {
  34. const batch1 = buffer.batch;
  35. buffer.input( CHANGE_LIMIT - 1 );
  36. expect( buffer.batch ).to.equal( batch1 );
  37. buffer.input( 1 );
  38. const batch2 = buffer.batch;
  39. expect( batch2 ).to.be.instanceof( Batch );
  40. expect( batch2 ).to.not.equal( batch1 );
  41. expect( buffer.size ).to.equal( 0 );
  42. } );
  43. it( 'is reset once changes exceedes the limit', () => {
  44. const batch1 = buffer.batch;
  45. // Exceed the limit with one big jump to ensure that >= operator was used.
  46. buffer.input( CHANGE_LIMIT + 1 );
  47. expect( buffer.batch ).to.not.equal( batch1 );
  48. expect( buffer.size ).to.equal( 0 );
  49. } );
  50. it( 'is reset once a new batch appears in the document', () => {
  51. const batch1 = buffer.batch;
  52. // Ensure that size is reset too.
  53. buffer.input( 1 );
  54. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  55. expect( buffer.batch ).to.not.equal( batch1 );
  56. expect( buffer.size ).to.equal( 0 );
  57. } );
  58. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  59. const batch1 = buffer.batch;
  60. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  61. expect( buffer.batch ).to.equal( batch1 );
  62. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  63. expect( buffer.batch ).to.equal( batch1 );
  64. } );
  65. it( 'is not reset when changes are added to batch which existed previously', () => {
  66. const externalBatch = doc.batch();
  67. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  68. const bufferBatch = buffer.batch;
  69. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  70. expect( buffer.batch ).to.equal( bufferBatch );
  71. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  72. expect( buffer.batch ).to.not.equal( bufferBatch );
  73. } );
  74. it( 'is not reset when changes are applied in transparent batch', () => {
  75. const bufferBatch = buffer.batch;
  76. doc.batch( 'transparent' ).insert( Position.createAt( root, 0 ), 'a' );
  77. expect( buffer.batch ).to.equal( bufferBatch );
  78. } );
  79. } );
  80. describe( 'destroy', () => {
  81. it( 'offs the buffer from the document', () => {
  82. const batch1 = buffer.batch;
  83. buffer.destroy();
  84. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  85. expect( buffer.batch ).to.equal( batch1 );
  86. } );
  87. } );
  88. } );