changebuffer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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( 'main' );
  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. } );
  41. it( 'is reset once changes exceedes the limit', () => {
  42. const batch1 = buffer.batch;
  43. // Exceed the limit with one big jump to ensure that >= operator was used.
  44. buffer.input( CHANGE_LIMIT + 1 );
  45. expect( buffer.batch ).to.not.equal( batch1 );
  46. } );
  47. it( 'is reset once a new batch appears in the document', () => {
  48. const batch1 = buffer.batch;
  49. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  50. expect( buffer.batch ).to.not.equal( batch1 );
  51. } );
  52. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  53. const batch1 = buffer.batch;
  54. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  55. expect( buffer.batch ).to.equal( batch1 );
  56. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  57. expect( buffer.batch ).to.equal( batch1 );
  58. } );
  59. it( 'is not reset when changes are added to batch which existed previously', () => {
  60. const externalBatch = doc.batch();
  61. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  62. const bufferBatch = buffer.batch;
  63. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  64. expect( buffer.batch ).to.equal( bufferBatch );
  65. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  66. expect( buffer.batch ).to.not.equal( bufferBatch );
  67. } );
  68. // See #7.
  69. it( 'is not reset when changes are applied without a batch', () => {
  70. const bufferBatch = buffer.batch;
  71. const delta = new InsertDelta();
  72. const insert = new InsertOperation( Position.createAt( root, 0 ), 'a', doc.version );
  73. delta.addOperation( insert );
  74. doc.applyOperation( insert );
  75. expect( buffer.batch ).to.equal( bufferBatch );
  76. } );
  77. } );
  78. describe( 'destory', () => {
  79. it( 'offs the buffer from the document', () => {
  80. const batch1 = buffer.batch;
  81. buffer.destroy();
  82. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  83. expect( buffer.batch ).to.equal( batch1 );
  84. } );
  85. } );
  86. } );