document.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import RootElement from '/ckeditor5/core/treemodel/rootelement.js';
  9. import Batch from '/ckeditor5/core/treemodel/batch.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. describe( 'Document', () => {
  12. let doc;
  13. beforeEach( () => {
  14. doc = new Document();
  15. } );
  16. describe( 'constructor', () => {
  17. it( 'should create Document with no data, empty graveyard and empty selection', () => {
  18. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map );
  19. expect( doc.roots.size ).to.equal( 1 );
  20. expect( doc.graveyard ).to.be.instanceof( RootElement );
  21. expect( doc.graveyard.getChildCount() ).to.equal( 0 );
  22. expect( doc.selection.getRanges().length ).to.equal( 0 );
  23. } );
  24. } );
  25. describe( 'createRoot', () => {
  26. it( 'should create a new RootElement, add it to roots map and return it', () => {
  27. let root = doc.createRoot( 'root', 'root' );
  28. expect( doc.roots.size ).to.equal( 2 );
  29. expect( root ).to.be.instanceof( RootElement );
  30. expect( root.getChildCount() ).to.equal( 0 );
  31. } );
  32. it( 'should throw an error when trying to create a second root with the same id', () => {
  33. doc.createRoot( 'root', 'root' );
  34. expect(
  35. () => {
  36. doc.createRoot( 'root', 'root' );
  37. }
  38. ).to.throw( CKEditorError, /document-createRoot-id-exists/ );
  39. } );
  40. } );
  41. describe( 'getRoot', () => {
  42. it( 'should return a RootElement previously created with given id', () => {
  43. let newRoot = doc.createRoot( 'root' );
  44. let getRoot = doc.getRoot( 'root' );
  45. expect( getRoot ).to.equal( newRoot );
  46. } );
  47. it( 'should throw an error when trying to get non-existent root', () => {
  48. expect(
  49. () => {
  50. doc.getRoot( 'root' );
  51. }
  52. ).to.throw( CKEditorError, /document-getRoot-root-not-exist/ );
  53. } );
  54. } );
  55. describe( 'applyOperation', () => {
  56. it( 'should increase document version, execute operation and fire event with proper data', () => {
  57. const changeCallback = sinon.spy();
  58. const type = 't';
  59. const data = { data: 'x' };
  60. const batch = 'batch';
  61. let operation = {
  62. type: type,
  63. delta: { batch: batch },
  64. baseVersion: 0,
  65. _execute: sinon.stub().returns( data )
  66. };
  67. doc.on( 'change', changeCallback );
  68. doc.applyOperation( operation );
  69. expect( doc.version ).to.equal( 1 );
  70. sinon.assert.calledOnce( operation._execute );
  71. sinon.assert.calledOnce( changeCallback );
  72. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  73. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  74. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  75. } );
  76. it( 'should throw an error on the operation base version and the document version is different', () => {
  77. let operation = {
  78. baseVersion: 1
  79. };
  80. expect(
  81. () => {
  82. doc.applyOperation( operation );
  83. }
  84. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  85. } );
  86. } );
  87. describe( 'batch', () => {
  88. it( 'should create a new batch with the document property', () => {
  89. const batch = doc.batch();
  90. expect( batch ).to.be.instanceof( Batch );
  91. expect( batch ).to.have.property( 'doc' ).that.equals( doc );
  92. } );
  93. } );
  94. describe( 'enqueue', () => {
  95. it( 'should be executed immediately and fire changesDone event', () => {
  96. let order = [];
  97. doc.on( 'changesDone', () => order.push( 'done' ) );
  98. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  99. expect( order ).to.have.length( 2 );
  100. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  101. expect( order[ 1 ] ).to.equal( 'done' );
  102. } );
  103. it( 'should fire done every time queue is empty', () => {
  104. let order = [];
  105. doc.on( 'changesDone', () => order.push( 'done' ) );
  106. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  107. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  108. expect( order ).to.have.length( 4 );
  109. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  110. expect( order[ 1 ] ).to.equal( 'done' );
  111. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  112. expect( order[ 3 ] ).to.equal( 'done' );
  113. } );
  114. it( 'should put callbacks in the proper order', () => {
  115. let order = [];
  116. doc.on( 'changesDone', () => order.push( 'done' ) );
  117. doc.enqueueChanges( () => {
  118. order.push( 'enqueue1 start' );
  119. doc.enqueueChanges( () => {
  120. order.push( 'enqueue2 start' );
  121. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  122. order.push( 'enqueue2 end' );
  123. } );
  124. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  125. order.push( 'enqueue1 end' );
  126. } );
  127. expect( order ).to.have.length( 7 );
  128. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  129. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  130. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  131. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  132. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  133. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  134. expect( order[ 6 ] ).to.equal( 'done' );
  135. } );
  136. } );
  137. it( 'should update selection attributes whenever selection gets updated', () => {
  138. sinon.spy( doc.selection, '_updateAttributes' );
  139. doc.selection.fire( 'update' );
  140. expect( doc.selection._updateAttributes.called ).to.be.true;
  141. } );
  142. it( 'should update selection attributes whenever changes to the document are applied', () => {
  143. sinon.spy( doc.selection, '_updateAttributes' );
  144. doc.fire( 'changesDone' );
  145. expect( doc.selection._updateAttributes.called ).to.be.true;
  146. } );
  147. } );