watchdog.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'Watchdog', () => {
  11. afterEach( () => sinon.restore() );
  12. describe( 'create()', () => {
  13. it( 'should create an editor instance', () => {
  14. const watchdog = new Watchdog();
  15. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  16. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  17. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  18. watchdog.setDestructor( editor => editor.destroy() );
  19. return watchdog.create( document.createElement( 'div' ), {} )
  20. .then( () => {
  21. sinon.assert.calledOnce( editorCreateSpy );
  22. sinon.assert.notCalled( editorDestroySpy );
  23. return watchdog.destroy();
  24. } )
  25. .then( () => {
  26. sinon.assert.calledOnce( editorCreateSpy );
  27. sinon.assert.calledOnce( editorDestroySpy );
  28. } );
  29. } );
  30. it( 'should throw an error when the creator is not defined', () => {
  31. const watchdog = new Watchdog();
  32. watchdog.setDestructor( editor => editor.destroy() );
  33. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-creator-not-defined/ );
  34. } );
  35. it( 'should throw an error when the destructor is not defined', () => {
  36. const watchdog = new Watchdog();
  37. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  38. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-destructor-not-defined/ );
  39. } );
  40. } );
  41. describe( 'restart()', () => {
  42. it( 'should restart the editor', () => {
  43. const watchdog = new Watchdog();
  44. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  45. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  46. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  47. watchdog.setDestructor( editor => editor.destroy() );
  48. return watchdog.create( document.createElement( 'div' ), {} )
  49. .then( () => {
  50. sinon.assert.calledOnce( editorCreateSpy );
  51. sinon.assert.notCalled( editorDestroySpy );
  52. return watchdog.restart();
  53. } )
  54. .then( () => {
  55. sinon.assert.calledTwice( editorCreateSpy );
  56. sinon.assert.calledOnce( editorDestroySpy );
  57. return watchdog.destroy();
  58. } );
  59. } );
  60. it( 'should restart the editor with the same data', () => {
  61. const watchdog = new Watchdog();
  62. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  63. watchdog.setDestructor( editor => editor.destroy() );
  64. return watchdog.create( document.createElement( 'div' ), {
  65. initialData: '<p>foo</p>',
  66. plugins: [ Paragraph ]
  67. } )
  68. .then( () => {
  69. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  70. return watchdog.restart();
  71. } )
  72. .then( () => {
  73. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  74. return watchdog.destroy();
  75. } );
  76. } );
  77. it( 'should support editor data passed as the `Editor.create()` as the first argument', () => {
  78. const watchdog = new Watchdog();
  79. sinon.stub( FakeEditor, 'create' ).callsFake( ( data, config ) => {
  80. return new Promise( resolve => {
  81. const editor = new FakeEditor( config );
  82. resolve(
  83. editor.initPlugins()
  84. .then( () => {
  85. editor.data.init( data );
  86. editor.data.fire( 'ready' );
  87. editor.fire( 'ready' );
  88. } )
  89. .then( () => editor )
  90. );
  91. } );
  92. } );
  93. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  94. watchdog.setDestructor( editor => editor.destroy() );
  95. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  96. .then( () => {
  97. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  98. return watchdog.restart();
  99. } )
  100. .then( () => {
  101. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  102. return watchdog.destroy();
  103. } );
  104. } );
  105. } );
  106. describe( 'editor', () => {
  107. it( 'should be the current editor instance', () => {
  108. const watchdog = new Watchdog();
  109. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  110. watchdog.setDestructor( editor => editor.destroy() );
  111. expect( watchdog.editor ).to.be.null;
  112. let oldEditor;
  113. return watchdog.create( document.createElement( 'div' ), {} )
  114. .then( () => {
  115. oldEditor = watchdog.editor;
  116. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  117. return watchdog.restart();
  118. } )
  119. .then( () => {
  120. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  121. expect( watchdog.editor ).to.not.equal( oldEditor );
  122. return watchdog.destroy();
  123. } )
  124. .then( () => {
  125. expect( watchdog.editor ).to.be.null;
  126. } );
  127. } );
  128. } );
  129. describe( 'error handling', () => {
  130. it( 'Watchdog should not restart editor during the initialization', () => {
  131. const watchdog = new Watchdog();
  132. watchdog.setCreator( el =>
  133. FakeEditor.create( el )
  134. .then( () => Promise.reject( new Error( 'foo' ) ) )
  135. );
  136. watchdog.setDestructor( editor => editor.destroy() );
  137. return watchdog.create( document.createElement( 'div' ) ).then(
  138. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  139. err => {
  140. expect( err ).to.be.instanceOf( Error );
  141. expect( err.message ).to.equal( 'foo' );
  142. }
  143. );
  144. } );
  145. it( 'Watchdog should not restart editor during the destroy', () => {
  146. const watchdog = new Watchdog();
  147. watchdog.setCreator( el => FakeEditor.create( el ) );
  148. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  149. return Promise.resolve()
  150. .then( () => watchdog.create( document.createElement( 'div' ) ) )
  151. .then( () => watchdog.destroy() )
  152. .then(
  153. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  154. err => {
  155. expect( err ).to.be.instanceOf( Error );
  156. expect( err.message ).to.equal( 'foo' );
  157. }
  158. );
  159. } );
  160. it( 'Watchdog should not hide intercepted errors', () => {
  161. const watchdog = new Watchdog();
  162. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  163. watchdog.setDestructor( editor => editor.destroy() );
  164. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  165. const originalErrorHandler = window.onerror;
  166. const windowErrorSpy = sinon.spy();
  167. window.onerror = windowErrorSpy;
  168. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  169. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  170. return new Promise( res => {
  171. watchdog.on( 'restart', () => {
  172. window.onerror = originalErrorHandler;
  173. sinon.assert.calledOnce( windowErrorSpy );
  174. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: foo' );
  175. watchdog.destroy().then( res );
  176. } );
  177. } );
  178. } );
  179. } );
  180. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  181. const watchdog = new Watchdog();
  182. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  183. watchdog.setDestructor( editor => editor.destroy() );
  184. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  185. const originalErrorHandler = window.onerror;
  186. window.onerror = undefined;
  187. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  188. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  189. return new Promise( res => {
  190. watchdog.on( 'restart', () => {
  191. window.onerror = originalErrorHandler;
  192. watchdog.destroy().then( res );
  193. } );
  194. } );
  195. } );
  196. } );
  197. it( 'Watchdog should not intercept non-editor errors', () => {
  198. const watchdog = new Watchdog();
  199. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  200. watchdog.setDestructor( editor => editor.destroy() );
  201. const editorErrorSpy = sinon.spy();
  202. watchdog.on( 'error', editorErrorSpy );
  203. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  204. const originalErrorHandler = window.onerror;
  205. window.onerror = undefined;
  206. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  207. setTimeout( () => {
  208. throw new Error( 'foo' );
  209. } );
  210. return new Promise( res => {
  211. setTimeout( () => {
  212. window.onerror = originalErrorHandler;
  213. sinon.assert.notCalled( editorErrorSpy );
  214. watchdog.destroy().then( res );
  215. } );
  216. } );
  217. } );
  218. } );
  219. it( 'Watchdog should not intercept other editor errors', () => {
  220. const watchdog1 = new Watchdog();
  221. const watchdog2 = new Watchdog();
  222. watchdog1.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  223. watchdog1.setDestructor( editor => editor.destroy() );
  224. watchdog2.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  225. watchdog2.setDestructor( editor => editor.destroy() );
  226. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  227. const originalErrorHandler = window.onerror;
  228. window.onerror = undefined;
  229. return Promise.all( [
  230. watchdog1.create( document.createElement( 'div' ) ),
  231. watchdog2.create( document.createElement( 'div' ) )
  232. ] ).then( () => {
  233. return new Promise( res => {
  234. const watchdog1ErrorSpy = sinon.spy();
  235. const watchdog2ErrorSpy = sinon.spy();
  236. watchdog1.on( 'restart', watchdog1ErrorSpy );
  237. watchdog2.on( 'restart', watchdog2ErrorSpy );
  238. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  239. setTimeout( () => {
  240. window.onerror = originalErrorHandler;
  241. sinon.assert.notCalled( watchdog1ErrorSpy );
  242. sinon.assert.calledOnce( watchdog2ErrorSpy );
  243. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  244. .then( res );
  245. } );
  246. } );
  247. } );
  248. } );
  249. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  250. const watchdog = new Watchdog();
  251. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  252. watchdog.setDestructor( editor => editor.destroy() );
  253. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  254. const originalErrorHandler = window.onerror;
  255. window.onerror = undefined;
  256. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  257. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  258. return new Promise( res => {
  259. watchdog.on( 'restart', () => {
  260. window.onerror = originalErrorHandler;
  261. watchdog.destroy().then( res );
  262. } );
  263. } );
  264. } );
  265. } );
  266. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  267. const watchdog = new Watchdog();
  268. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  269. watchdog.setDestructor( editor => editor.destroy() );
  270. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  271. const originalErrorHandler = window.onerror;
  272. window.onerror = undefined;
  273. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  274. setTimeout( () => throwCKEditorError( 'foo', {
  275. foo: [ 1, 2, 3, {
  276. bar: new Set( [
  277. new Map( [
  278. [ 'foo', 'bar' ],
  279. [ 0, watchdog.editor ]
  280. ] )
  281. ] )
  282. } ]
  283. } ) );
  284. return new Promise( res => {
  285. watchdog.on( 'restart', () => {
  286. window.onerror = originalErrorHandler;
  287. watchdog.destroy().then( res );
  288. } );
  289. } );
  290. } );
  291. } );
  292. it( 'editor should be restarted up to 3 times by default', () => {
  293. const watchdog = new Watchdog();
  294. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  295. watchdog.setDestructor( editor => editor.destroy() );
  296. const errorSpy = sinon.spy();
  297. watchdog.on( 'error', errorSpy );
  298. const restartSpy = sinon.spy();
  299. watchdog.on( 'restart', restartSpy );
  300. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  301. const originalErrorHandler = window.onerror;
  302. window.onerror = undefined;
  303. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  304. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  305. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  306. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  307. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  308. return new Promise( res => {
  309. setTimeout( () => {
  310. expect( errorSpy.callCount ).to.equal( 4 );
  311. expect( watchdog.crashes.length ).to.equal( 4 );
  312. expect( restartSpy.callCount ).to.equal( 3 );
  313. window.onerror = originalErrorHandler;
  314. watchdog.destroy().then( res );
  315. } );
  316. } );
  317. } );
  318. } );
  319. it( 'editor should be restarted up to `crashNumberLimit` times if the option is set', () => {
  320. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  321. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  322. watchdog.setDestructor( editor => editor.destroy() );
  323. const errorSpy = sinon.spy();
  324. watchdog.on( 'error', errorSpy );
  325. const restartSpy = sinon.spy();
  326. watchdog.on( 'restart', restartSpy );
  327. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  328. const originalErrorHandler = window.onerror;
  329. window.onerror = undefined;
  330. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  331. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  332. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  333. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  334. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  335. return new Promise( res => {
  336. setTimeout( () => {
  337. expect( errorSpy.callCount ).to.equal( 4 );
  338. expect( watchdog.crashes.length ).to.equal( 4 );
  339. expect( restartSpy.callCount ).to.equal( 2 );
  340. window.onerror = originalErrorHandler;
  341. watchdog.destroy().then( res );
  342. } );
  343. } );
  344. } );
  345. } );
  346. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  347. const watchdog = new Watchdog();
  348. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  349. watchdog.setDestructor( editor => editor.destroy() );
  350. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  351. const originalErrorHandler = window.onerror;
  352. window.onerror = undefined;
  353. sinon.stub( console, 'error' );
  354. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  355. setTimeout( () => throwCKEditorError( 'foo' ) );
  356. return new Promise( res => {
  357. setTimeout( () => {
  358. window.onerror = originalErrorHandler;
  359. expect( watchdog.crashes ).to.deep.equal( [] );
  360. sinon.assert.calledOnce( console.error );
  361. sinon.assert.calledWithExactly(
  362. console.error,
  363. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  364. );
  365. watchdog.destroy().then( res );
  366. } );
  367. } );
  368. } );
  369. } );
  370. it( 'editor should be restarted with the data before the crash #1', () => {
  371. const watchdog = new Watchdog();
  372. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  373. watchdog.setDestructor( editor => editor.destroy() );
  374. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  375. const originalErrorHandler = window.onerror;
  376. window.onerror = undefined;
  377. return watchdog.create( document.createElement( 'div' ), {
  378. initialData: '<p>foo</p>',
  379. plugins: [ Paragraph ]
  380. } ).then( () => {
  381. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  382. return new Promise( res => {
  383. watchdog.on( 'restart', () => {
  384. window.onerror = originalErrorHandler;
  385. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  386. watchdog.destroy().then( res );
  387. } );
  388. } );
  389. } );
  390. } );
  391. it( 'editor should be restarted with the data before the crash #2', () => {
  392. const watchdog = new Watchdog();
  393. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  394. watchdog.setDestructor( editor => editor.destroy() );
  395. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  396. const originalErrorHandler = window.onerror;
  397. window.onerror = undefined;
  398. return watchdog.create( document.createElement( 'div' ), {
  399. initialData: '<p>foo</p>',
  400. plugins: [ Paragraph ]
  401. } ).then( () => {
  402. const doc = watchdog.editor.model.document;
  403. watchdog.editor.model.change( writer => {
  404. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  405. } );
  406. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  407. return new Promise( res => {
  408. watchdog.on( 'restart', () => {
  409. window.onerror = originalErrorHandler;
  410. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  411. watchdog.destroy().then( res );
  412. } );
  413. } );
  414. } );
  415. } );
  416. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  417. const watchdog = new Watchdog();
  418. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  419. watchdog.setDestructor( editor => editor.destroy() );
  420. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  421. const originalErrorHandler = window.onerror;
  422. window.onerror = undefined;
  423. return watchdog.create( document.createElement( 'div' ), {
  424. initialData: '<p>foo</p>',
  425. plugins: [ Paragraph ]
  426. } ).then( () => {
  427. const doc = watchdog.editor.model.document;
  428. watchdog.editor.model.document.version = -1000;
  429. watchdog.editor.model.change( writer => {
  430. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  431. } );
  432. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  433. return new Promise( res => {
  434. watchdog.on( 'restart', () => {
  435. window.onerror = originalErrorHandler;
  436. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  437. watchdog.destroy().then( res );
  438. } );
  439. } );
  440. } );
  441. } );
  442. it( 'editor should be restarted with the latest available data before the crash', () => {
  443. const watchdog = new Watchdog();
  444. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  445. watchdog.setDestructor( editor => editor.destroy() );
  446. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  447. const originalErrorHandler = window.onerror;
  448. window.onerror = undefined;
  449. sinon.stub( console, 'error' );
  450. return watchdog.create( document.createElement( 'div' ), {
  451. initialData: '<p>foo</p>',
  452. plugins: [ Paragraph ]
  453. } ).then( () => {
  454. const editorGetDataError = new Error( 'Some error' );
  455. const getDataStub = sinon.stub( watchdog.editor, 'getData' )
  456. .throwsException( editorGetDataError );
  457. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  458. const doc = watchdog.editor.model.document;
  459. watchdog.editor.model.change( writer => {
  460. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  461. } );
  462. return new Promise( res => {
  463. watchdog.on( 'restart', () => {
  464. window.onerror = originalErrorHandler;
  465. sinon.assert.calledOnce( getDataStub );
  466. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  467. sinon.assert.calledWith(
  468. console.error,
  469. editorGetDataError,
  470. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  471. );
  472. watchdog.destroy().then( res );
  473. } );
  474. } );
  475. } );
  476. } );
  477. } );
  478. describe( 'static for()', () => {
  479. it( 'should be a shortcut method for creating the watchdog', () => {
  480. const watchdog = Watchdog.for( FakeEditor );
  481. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  482. const originalErrorHandler = window.onerror;
  483. window.onerror = undefined;
  484. return watchdog.create( document.createElement( 'div' ), {
  485. initialData: '<p>foo</p>',
  486. plugins: [ Paragraph ]
  487. } ).then( () => {
  488. const oldEditor = watchdog.editor;
  489. expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
  490. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  491. return new Promise( res => {
  492. watchdog.on( 'restart', () => {
  493. window.onerror = originalErrorHandler;
  494. expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
  495. expect( watchdog.editor ).to.not.equal( oldEditor );
  496. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  497. watchdog.destroy().then( res );
  498. } );
  499. } );
  500. } );
  501. } );
  502. } );
  503. describe( 'crashes', () => {
  504. it( 'should be an array of caught errors by the watchdog', () => {
  505. const watchdog = new Watchdog();
  506. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  507. watchdog.setDestructor( editor => editor.destroy() );
  508. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  509. const originalErrorHandler = window.onerror;
  510. window.onerror = undefined;
  511. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  512. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  513. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  514. return new Promise( res => {
  515. setTimeout( () => {
  516. window.onerror = originalErrorHandler;
  517. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  518. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  519. watchdog.destroy().then( res );
  520. } );
  521. } );
  522. } );
  523. } );
  524. } );
  525. } );
  526. // Wrap Editor to align the API to the "full" editors.
  527. class FakeEditor extends VirtualTestEditor {
  528. static create( _elementOrData, config ) {
  529. return super.create( config );
  530. }
  531. }
  532. function throwCKEditorError( name, context ) {
  533. throw new CKEditorError( name, context );
  534. }