document.js 5.0 KB

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