changebuffer.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ChangeBuffer from '/ckeditor5/typing/changebuffer.js';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Batch from '/ckeditor5/engine/model/batch.js';
  9. import Position from '/ckeditor5/engine/model/position.js';
  10. import InsertDelta from '/ckeditor5/engine/model/delta/insertdelta.js';
  11. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  12. describe( 'ChangeBuffer', () => {
  13. const CHANGE_LIMIT = 3;
  14. let doc, buffer, root;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. buffer = new ChangeBuffer( doc, CHANGE_LIMIT );
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'sets all properties', () => {
  22. expect( buffer ).to.have.property( 'document', doc );
  23. expect( buffer ).to.have.property( 'limit', CHANGE_LIMIT );
  24. expect( buffer ).to.have.property( 'size', 0 );
  25. } );
  26. } );
  27. describe( 'batch', () => {
  28. it( 'it is set initially', () => {
  29. expect( buffer ).to.have.property( 'batch' );
  30. expect( buffer.batch ).to.be.instanceof( Batch );
  31. } );
  32. it( 'is reset once changes reaches the limit', () => {
  33. const batch1 = buffer.batch;
  34. buffer.input( CHANGE_LIMIT - 1 );
  35. expect( buffer.batch ).to.equal( batch1 );
  36. buffer.input( 1 );
  37. const batch2 = buffer.batch;
  38. expect( batch2 ).to.be.instanceof( Batch );
  39. expect( batch2 ).to.not.equal( batch1 );
  40. expect( buffer.size ).to.equal( 0 );
  41. } );
  42. it( 'is reset once changes exceedes the limit', () => {
  43. const batch1 = buffer.batch;
  44. // Exceed the limit with one big jump to ensure that >= operator was used.
  45. buffer.input( CHANGE_LIMIT + 1 );
  46. expect( buffer.batch ).to.not.equal( batch1 );
  47. expect( buffer.size ).to.equal( 0 );
  48. } );
  49. it( 'is reset once a new batch appears in the document', () => {
  50. const batch1 = buffer.batch;
  51. // Ensure that size is reset too.
  52. buffer.input( 1 );
  53. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  54. expect( buffer.batch ).to.not.equal( batch1 );
  55. expect( buffer.size ).to.equal( 0 );
  56. } );
  57. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  58. const batch1 = buffer.batch;
  59. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  60. expect( buffer.batch ).to.equal( batch1 );
  61. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  62. expect( buffer.batch ).to.equal( batch1 );
  63. } );
  64. it( 'is not reset when changes are added to batch which existed previously', () => {
  65. const externalBatch = doc.batch();
  66. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  67. const bufferBatch = buffer.batch;
  68. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  69. expect( buffer.batch ).to.equal( bufferBatch );
  70. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  71. expect( buffer.batch ).to.not.equal( bufferBatch );
  72. } );
  73. // See #7.
  74. it( 'is not reset when changes are applied without a batch', () => {
  75. const bufferBatch = buffer.batch;
  76. const delta = new InsertDelta();
  77. const insert = new InsertOperation( Position.createAt( root, 0 ), 'a', doc.version );
  78. delta.addOperation( insert );
  79. doc.applyOperation( insert );
  80. expect( buffer.batch ).to.equal( bufferBatch );
  81. } );
  82. } );
  83. describe( 'destory', () => {
  84. it( 'offs the buffer from the document', () => {
  85. const batch1 = buffer.batch;
  86. buffer.destroy();
  87. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  88. expect( buffer.batch ).to.equal( batch1 );
  89. } );
  90. } );
  91. } );