changebuffer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. describe( 'ChangeBuffer', () => {
  11. const CHANGE_LIMIT = 3;
  12. let doc, buffer, root;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot();
  16. buffer = new ChangeBuffer( doc, CHANGE_LIMIT );
  17. } );
  18. describe( 'constructor', () => {
  19. it( 'sets all properties', () => {
  20. expect( buffer ).to.have.property( 'document', doc );
  21. expect( buffer ).to.have.property( 'limit', CHANGE_LIMIT );
  22. expect( buffer ).to.have.property( 'size', 0 );
  23. } );
  24. it( 'sets limit property according to default value', () => {
  25. buffer = new ChangeBuffer( doc );
  26. expect( buffer ).to.have.property( 'limit', 20 );
  27. } );
  28. } );
  29. describe( 'batch', () => {
  30. it( 'it is set initially', () => {
  31. expect( buffer ).to.have.property( 'batch' );
  32. expect( buffer.batch ).to.be.instanceof( Batch );
  33. } );
  34. it( 'is reset once changes reaches the limit', () => {
  35. const batch1 = buffer.batch;
  36. buffer.input( CHANGE_LIMIT - 1 );
  37. expect( buffer.batch ).to.equal( batch1 );
  38. buffer.input( 1 );
  39. const batch2 = buffer.batch;
  40. expect( batch2 ).to.be.instanceof( Batch );
  41. expect( batch2 ).to.not.equal( batch1 );
  42. expect( buffer.size ).to.equal( 0 );
  43. } );
  44. it( 'is reset once changes exceedes the limit', () => {
  45. const batch1 = buffer.batch;
  46. // Exceed the limit with one big jump to ensure that >= operator was used.
  47. buffer.input( CHANGE_LIMIT + 1 );
  48. expect( buffer.batch ).to.not.equal( batch1 );
  49. expect( buffer.size ).to.equal( 0 );
  50. } );
  51. it( 'is reset once a new batch appears in the document', () => {
  52. const batch1 = buffer.batch;
  53. // Ensure that size is reset too.
  54. buffer.input( 1 );
  55. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  56. expect( buffer.batch ).to.not.equal( batch1 );
  57. expect( buffer.size ).to.equal( 0 );
  58. } );
  59. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  60. const batch1 = buffer.batch;
  61. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  62. expect( buffer.batch ).to.equal( batch1 );
  63. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  64. expect( buffer.batch ).to.equal( batch1 );
  65. } );
  66. it( 'is not reset when changes are added to batch which existed previously', () => {
  67. const externalBatch = doc.batch();
  68. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  69. const bufferBatch = buffer.batch;
  70. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  71. expect( buffer.batch ).to.equal( bufferBatch );
  72. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  73. expect( buffer.batch ).to.not.equal( bufferBatch );
  74. } );
  75. it( 'is not reset when changes are applied in transparent batch', () => {
  76. const bufferBatch = buffer.batch;
  77. doc.batch( 'transparent' ).insert( Position.createAt( root, 0 ), 'a' );
  78. expect( buffer.batch ).to.equal( bufferBatch );
  79. } );
  80. } );
  81. describe( 'destroy', () => {
  82. it( 'offs the buffer from the document', () => {
  83. const batch1 = buffer.batch;
  84. buffer.destroy();
  85. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  86. expect( buffer.batch ).to.equal( batch1 );
  87. } );
  88. } );
  89. } );