8
0

changebuffer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. it( 'sets limit property according to default value', () => {
  27. buffer = new ChangeBuffer( doc );
  28. expect( buffer ).to.have.property( 'limit', 20 );
  29. } );
  30. } );
  31. describe( 'batch', () => {
  32. it( 'it is set initially', () => {
  33. expect( buffer ).to.have.property( 'batch' );
  34. expect( buffer.batch ).to.be.instanceof( Batch );
  35. } );
  36. it( 'is reset once changes reaches the limit', () => {
  37. const batch1 = buffer.batch;
  38. buffer.input( CHANGE_LIMIT - 1 );
  39. expect( buffer.batch ).to.equal( batch1 );
  40. buffer.input( 1 );
  41. const batch2 = buffer.batch;
  42. expect( batch2 ).to.be.instanceof( Batch );
  43. expect( batch2 ).to.not.equal( batch1 );
  44. expect( buffer.size ).to.equal( 0 );
  45. } );
  46. it( 'is reset once changes exceedes the limit', () => {
  47. const batch1 = buffer.batch;
  48. // Exceed the limit with one big jump to ensure that >= operator was used.
  49. buffer.input( CHANGE_LIMIT + 1 );
  50. expect( buffer.batch ).to.not.equal( batch1 );
  51. expect( buffer.size ).to.equal( 0 );
  52. } );
  53. it( 'is reset once a new batch appears in the document', () => {
  54. const batch1 = buffer.batch;
  55. // Ensure that size is reset too.
  56. buffer.input( 1 );
  57. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  58. expect( buffer.batch ).to.not.equal( batch1 );
  59. expect( buffer.size ).to.equal( 0 );
  60. } );
  61. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  62. const batch1 = buffer.batch;
  63. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  64. expect( buffer.batch ).to.equal( batch1 );
  65. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  66. expect( buffer.batch ).to.equal( batch1 );
  67. } );
  68. it( 'is not reset when changes are added to batch which existed previously', () => {
  69. const externalBatch = doc.batch();
  70. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  71. const bufferBatch = buffer.batch;
  72. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  73. expect( buffer.batch ).to.equal( bufferBatch );
  74. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  75. expect( buffer.batch ).to.not.equal( bufferBatch );
  76. } );
  77. // See #7.
  78. it( 'is not reset when changes are applied without a batch', () => {
  79. const bufferBatch = buffer.batch;
  80. const delta = new InsertDelta();
  81. const insert = new InsertOperation( Position.createAt( root, 0 ), 'a', doc.version );
  82. delta.addOperation( insert );
  83. doc.applyOperation( insert );
  84. expect( buffer.batch ).to.equal( bufferBatch );
  85. } );
  86. } );
  87. describe( 'destory', () => {
  88. it( 'offs the buffer from the document', () => {
  89. const batch1 = buffer.batch;
  90. buffer.destroy();
  91. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  92. expect( buffer.batch ).to.equal( batch1 );
  93. } );
  94. } );
  95. } );