contextwatchdog.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, setTimeout, window */
  6. import ContextWatchdog from '../src/contextwatchdog';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import Context from '@ckeditor/ckeditor5-core/src/context';
  9. import sinon from 'sinon';
  10. import { expect } from 'chai';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. describe( 'ContextWatchdog', () => {
  13. let element1, element2;
  14. let mainWatchdog;
  15. beforeEach( () => {
  16. element1 = document.createElement( 'div' );
  17. element2 = document.createElement( 'div' );
  18. document.body.appendChild( element1 );
  19. document.body.appendChild( element2 );
  20. } );
  21. afterEach( () => {
  22. element1.remove();
  23. element2.remove();
  24. sinon.restore();
  25. } );
  26. it.skip( 'case: editor, contextItem', async () => {
  27. mainWatchdog = ContextWatchdog.for( Context, {} );
  28. mainWatchdog.add( {
  29. editor1: {
  30. type: 'editor',
  31. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  32. sourceElementOrData: element1,
  33. config: {}
  34. },
  35. annotatedInput: {
  36. type: 'contextItem',
  37. creator: () => { }
  38. }
  39. } );
  40. await mainWatchdog.waitForReady();
  41. await mainWatchdog.destroy();
  42. } );
  43. describe( 'for no items added', () => {
  44. it( 'should create only context', async () => {
  45. mainWatchdog = ContextWatchdog.for( Context, {} );
  46. await mainWatchdog.waitForReady();
  47. expect( mainWatchdog.context ).to.be.instanceOf( Context );
  48. await mainWatchdog.destroy();
  49. } );
  50. it( 'should have proper states', async () => {
  51. mainWatchdog = ContextWatchdog.for( Context, {} );
  52. expect( mainWatchdog.state ).to.equal( 'initializing' );
  53. await mainWatchdog.waitForReady();
  54. expect( mainWatchdog.state ).to.equal( 'ready' );
  55. await mainWatchdog.destroy();
  56. expect( mainWatchdog.state ).to.equal( 'destroyed' );
  57. } );
  58. describe( 'in case of error handling', () => {
  59. it( 'should restart the `Context`', async () => {
  60. mainWatchdog = ContextWatchdog.for( Context, {} );
  61. const errorSpy = sinon.spy();
  62. await mainWatchdog.waitForReady();
  63. const oldContext = mainWatchdog.context;
  64. const originalErrorHandler = window.onerror;
  65. window.onerror = sinon.spy();
  66. mainWatchdog.on( 'restart', errorSpy );
  67. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  68. await waitCycle();
  69. sinon.assert.calledOnce( errorSpy );
  70. expect( mainWatchdog.context ).to.not.equal( oldContext );
  71. window.onerror = originalErrorHandler;
  72. } );
  73. } );
  74. } );
  75. describe( 'for multiple editors', () => {
  76. it( 'should allow adding multiple items without waiting', async () => {
  77. mainWatchdog = ContextWatchdog.for( Context, {} );
  78. mainWatchdog.add( {
  79. editor1: {
  80. type: 'editor',
  81. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  82. sourceElementOrData: element1,
  83. config: {}
  84. },
  85. } );
  86. mainWatchdog.add( {
  87. editor2: {
  88. type: 'editor',
  89. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  90. sourceElementOrData: element2,
  91. config: {}
  92. },
  93. } );
  94. await mainWatchdog.waitForReady();
  95. await mainWatchdog.destroy();
  96. } );
  97. it( 'should allow adding and removing items without waiting', async () => {
  98. mainWatchdog = ContextWatchdog.for( Context, {} );
  99. mainWatchdog.add( {
  100. editor1: {
  101. type: 'editor',
  102. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  103. sourceElementOrData: element1,
  104. config: {}
  105. },
  106. } );
  107. mainWatchdog.add( {
  108. editor2: {
  109. type: 'editor',
  110. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  111. sourceElementOrData: element2,
  112. config: {}
  113. },
  114. } );
  115. await mainWatchdog.waitForReady();
  116. expect( mainWatchdog.state ).to.equal( 'ready' );
  117. mainWatchdog.remove( [ 'editor1' ] );
  118. await mainWatchdog.waitForReady();
  119. mainWatchdog.remove( [ 'editor2' ] );
  120. await mainWatchdog.waitForReady();
  121. await mainWatchdog.destroy();
  122. } );
  123. describe( 'in case of error handling', () => {
  124. it( 'should restart the whole structure of editors if an error happens inside the `Context`', async () => {
  125. mainWatchdog = ContextWatchdog.for( Context, {} );
  126. mainWatchdog.add( {
  127. editor1: {
  128. type: 'editor',
  129. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  130. sourceElementOrData: element1,
  131. config: {}
  132. }
  133. } );
  134. await mainWatchdog.waitForReady();
  135. const oldContext = mainWatchdog.context;
  136. const restartSpy = sinon.spy();
  137. const originalErrorHandler = window.onerror;
  138. window.onerror = sinon.spy();
  139. mainWatchdog.on( 'error', () => {
  140. window.onerror = originalErrorHandler;
  141. } );
  142. mainWatchdog.on( 'restart', restartSpy );
  143. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  144. await waitCycle();
  145. sinon.assert.calledOnce( restartSpy );
  146. expect( mainWatchdog.context ).to.not.equal( oldContext );
  147. window.onerror = originalErrorHandler;
  148. } );
  149. } );
  150. } );
  151. it( 'case: recreating watchdog', async () => {
  152. mainWatchdog = ContextWatchdog.for( Context, {} );
  153. await mainWatchdog.destroy();
  154. let err;
  155. try {
  156. await mainWatchdog.add( {
  157. editor2: {
  158. type: 'editor',
  159. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  160. sourceElementOrData: element1,
  161. config: {}
  162. },
  163. } );
  164. } catch ( _err ) {
  165. err = _err;
  166. }
  167. expect( err ).to.be.instanceOf( Error );
  168. expect( err.message ).to.match( /Cannot add items do destroyed watchdog\./ );
  169. } );
  170. } );
  171. function throwCKEditorError( name, context ) {
  172. throw new CKEditorError( name, context );
  173. }
  174. function waitCycle() {
  175. return new Promise( res => setTimeout( res ) );
  176. }