changebuffer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/treemodel/document.js';
  8. import Batch from '/ckeditor5/engine/treemodel/batch.js';
  9. import Position from '/ckeditor5/engine/treemodel/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( 'main' );
  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. } );
  25. describe( 'batch', () => {
  26. it( 'it is set initially', () => {
  27. expect( buffer ).to.have.property( 'batch' );
  28. expect( buffer.batch ).to.be.instanceof( Batch );
  29. } );
  30. it( 'is reset once changes exceed the limit', () => {
  31. const batch1 = buffer.batch;
  32. buffer.input( CHANGE_LIMIT );
  33. expect( buffer.batch ).to.equal( batch1 );
  34. buffer.input( 1 );
  35. const batch2 = buffer.batch;
  36. expect( batch2 ).to.be.instanceof( Batch );
  37. expect( batch2 ).to.not.equal( batch1 );
  38. } );
  39. it( 'is reset once a new batch appears in the document', () => {
  40. const batch1 = buffer.batch;
  41. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  42. expect( buffer.batch ).to.not.equal( batch1 );
  43. } );
  44. it( 'is not reset when changes are added to the buffer\'s batch', () => {
  45. const batch1 = buffer.batch;
  46. buffer.batch.insert( Position.createAt( root, 0 ), 'a' );
  47. expect( buffer.batch ).to.equal( batch1 );
  48. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  49. expect( buffer.batch ).to.equal( batch1 );
  50. } );
  51. it( 'is not reset when changes are added to batch which existed previously', () => {
  52. const externalBatch = doc.batch();
  53. externalBatch.insert( Position.createAt( root, 0 ), 'a' );
  54. const bufferBatch = buffer.batch;
  55. buffer.batch.insert( Position.createAt( root, 0 ), 'b' );
  56. expect( buffer.batch ).to.equal( bufferBatch );
  57. doc.batch().insert( Position.createAt( root, 0 ), 'c' );
  58. expect( buffer.batch ).to.not.equal( bufferBatch );
  59. } );
  60. } );
  61. describe( 'destory', () => {
  62. it( 'offs the buffer from the document', () => {
  63. const batch1 = buffer.batch;
  64. buffer.destroy();
  65. doc.batch().insert( Position.createAt( root, 0 ), 'a' );
  66. expect( buffer.batch ).to.equal( batch1 );
  67. } );
  68. } );
  69. } );